DEV Community

Cover image for How to Manage and Use Apache virtual hosts in Ubuntu
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

How to Manage and Use Apache virtual hosts in Ubuntu

What Is Apache Virtual Hosts?

Virtual Host term refers to the method of running over one website, such as yourdomain.com, test.yourdomain.com, or www.yourdomain.com, www.yourdomain2.com on a single system. There are two types of Virtual Hosting in Apache, namely IP-based virtual hosting and name-based virtual hosting. With IP-based virtual hosting, you can host multiple websites or domains on the same system, but each website/domain has a unique IP address. With name-based virtual hosting, you can host multiple websites/domains on the same IP address. Virtual hosting can be helpful if you want to host various websites and domains from a single physical server or VPS. I hope you got the basic idea of Apache virtual hosts. Today, we are going to see how to configure Apache virtual hosts in Ubuntu.

1. Prerequisites

The operating system running Ubuntu Linux
A root or non-root user with Sudo privileges
Has stable internet connection
Terminal window / Command line

2. Update Local Repositories

Update all system packages to the latest. Run the following command:

#! /bin/bash
sudo apt-get update
sudo apt-get upgrade -y
Enter fullscreen mode Exit fullscreen mode

3. Installing Apache On VPS

Apache Virtual Host allows you to maximize your resources when setting up a website. This powerful software can use a single server and a single IP address to host many domains.

Before you can configure Apache Virtual Hosts, you need to install the Apache webserver. Run the following command:

#! /bin/bash
sudo apt-get install apache2
Enter fullscreen mode Exit fullscreen mode

Type Y for Yes and press Enter.

If you have any problems setting up the webserver, refer to our in-depth tutorial on installing Apache on Ubuntu.

Apache should already be running. To check the status, run the following command:

sudo systemctl status apache2
Enter fullscreen mode Exit fullscreen mode

Verify the installation by visiting the IP address of your server in your browser. You should see Apache2 Ubuntu Default Page will be displayed.

If this is not the case, some problems may have occurred while installing Apache. In this case, uninstalling Apache and proceeding with a new installation are advisable.

To remove Apache, run the following command:

#! /bin/bash
sudo apt-get purge apache2
Enter fullscreen mode Exit fullscreen mode

4. Apache Virtual Host Directory Structure

Go to the directory where all the Apache configuration files are stored, run the following command:

#! /bin/bash
cd /etc/apache2
Enter fullscreen mode Exit fullscreen mode

Once you are in, run the ls command, and you will see a few files and directories. Here are some critical directories and files you will see in the /etc/apache2 directory.

apache2.conf is Apache’s main configuration file.
conf-available holds all the extra configuration files.
conf-enabled contains symlinks of the configuration files that are enabled.
The envvars file contains environment variables.
mods-available contains all the available Apache modules.
mods-enabled contains symlinks of the modules that are allowed.
ports.conf include the port configuration (On which port do you want to listen for requests?).
sites-available contains all the virtual host files we create.
sites-enabled contains symlinks of all the virtual hosts that we want to enable.
We have to focus on sites-available and sites-enabled directories from all these files and directories because these directories will hold all our Virtual Host configuration files.

5. Configuring Virtual Host on Apache

The example of the Virtual Host configuration shown in this tutorial is name-based, since it relies on the website's domain name to distinguish requests. There is another IP-based mode when multiple domains have to be each associated with a unique IP address.
In the example of this tutorial, mypersonaldomain.com will be used as a domain, but you will have to use an existing domain you own.
By accessing the server's IP address where you have just installed Apache, you can generally find the content of your website inside a public folder at var/www/html.
If you want to host multiple websites, it's easy to see how to create as many folders as there are websites provided that, for each website, the virtual host's configuration file, which will make it accessible via the web, is correctly set up.
So, move inside /var/www and create the folder for your site. You may assign the corresponding domain name to each folder:

#! /bin/bash
cd /var/www
sudo mkdir -p yourdomain.com/{public_html,logs}
Enter fullscreen mode Exit fullscreen mode

Add the following sample HTML:

#! /bin/bash
sudo nano /var/www/yourdomain.com/public_html/index.html
Enter fullscreen mode Exit fullscreen mode
<html>
  <head>
    <title>Welcome to YourDomain.com!</title>
  </head>
  <body>
    <h1>Success! The yourdomain.com virtual host is working!</h1>
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Save and close this file as well.

Change file ownership of the directory, run the following command:

#! /bin/bash
sudo chown -R $USER:$USER /var/www/yourdomain.com/public_html
Enter fullscreen mode Exit fullscreen mode

In the above code, we use $USER to grant permission to regular non-root users to access this directory.
We also need to change the file permissions of the Document Root folder to ensure that all files and pages are appropriately served.

#! /bin/bash
sudo chmod -R 755 /var/www
Enter fullscreen mode Exit fullscreen mode

Now, create the first virtual host file in the sites-available directory, run the following command:

#! /bin/bash
sudo nano /etc/apache2/sites-available/yourdomain.conf
Enter fullscreen mode Exit fullscreen mode

Now, Paste the following contents into the virtual host file. I will explain these directives in detail later in this guide.

<VirtualHost *:80>
    ServerAdmin support@yourdomain.com
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com

    DocumentRoot /var/www/yourdomain.com/public_html

    ErrorLog /var/www/yourdomain.com/logs/error.log
    CustomLog /var/www/yourdomain.com/logs/access.log combined
</VirtualHost>
Enter fullscreen mode Exit fullscreen mode

Save and close using the CTRL + X combination, then press Y and confirm by pressing ENTER.

Don't forget to replace yourdomain.com with your domain name. Let's first understand the directives used in this Virtual Host file.

ServerAdmin: The E-mail you provide in this directive will be displayed if the user encounters an Internal server error. So that they can contact you directly via E-mail.
ServerName: It’s the primary domain of your application. Here, I am using domain1.com. You can replace it with whatever domain name you want to host on your server.
ServerAlias: This directive holds all the different domains/subdomains you want to use to access the site. You can add multiple hostnames here, separated by space.
DocumentRoot: In this directive, define the directory in which you want to route the requests coming for this virtual host file if they enable it. It means that the requests coming on domain1.com will be routed to the /var/www/domain1.com/public_html directory.
ErrorLog: In this directive, define the path of the error log file. All the errors encountered in runtime will be logged in the file mentioned here.
CustomLog: This log file path you mention here will hold all the access logs.
And the ... is the Virtual Host block. You can create multiple such blocks in a single yourdomain.conf file for multiple domains, and it will still work!
Then, enable the new site and disable the default Apache configuration:

#! /bin/bash
sudo a2ensite yourdomain
Enter fullscreen mode Exit fullscreen mode

Once done, test the configuration for any syntax errors with:

#! /bin/bash
sudo apachectl configtest
Enter fullscreen mode Exit fullscreen mode

The answer Syntax OK should be returned. Then, restart Apache to apply the changes and have the web server use your configuration file.

#! /bin/bash
sudo systemctl restart apache2
Enter fullscreen mode Exit fullscreen mode

Now that you have your virtual hosts configured, you can test your setup by going to the domains that you configured in your web browser:

http://yourdomain.com

Thank you for reading this blog.

Top comments (1)

Collapse
 
nieuwepixels profile image
Nieuwe Pixels

I have been using a seperate Debian server as an internal server configured like you mention above. It's such a breeze. Being able to always work on multiple sites and keep expanding and/or work on a website together from multiple workplaces.
Internetagencies could surely benefit a lot from this approach.