Ubuntu 18.04 How to Create a Apache Virtual Host
The Apache web server is capable, flexible, and secure program. Using the server, an administrator could easily create an Apache Virtual Host. An Apache Virtual Host is useful if you are trying to run multiple web sites on one machine.
What is a Virtual Host
According to Apache’s own documentation, “The term Virtual Host refers to the practice of running more than one web site on a single machine.” Using an Apache virtual host, an administrator could purchase 2 different domain names and set both addresses to one machine. The admin could purchase www.example-site-name.com and www.another-domain-name.com, and serve both sites from the same Apache web server on a single machine.
In order to create the Virtual Host, you will need to have the Apache server already installed. This article is specifically written for Ubuntu 18.04, however, it may be similar in previous Ubuntu versions as well. It may also be a close experience for users of other systems.
In Ubuntu, sites served by Apache are stored in the directory /var/www/html. For this demonstration, we’ll need an example website. The next commands will navigate to the server directory and create a new folder to house an example website.
cd /var/www/html sudo mkdir example cd example sudo touch index.html sudo nano index.html
Edit the index.html file to the following example markup:
<!DOCTYPE html> <html lang="en"> <head> <metacharset="UTF-8"> <metaname="viewport"content="width=device-width, initial-scale=1.0"> <metahttp-equiv="X-UA-Compatible"content="ie=edge"> <title>Document</title> </head> <body> <h1>Example</h1> </body> </html>
Save the index.html file and visit the site in your web browser. You can visit this example site by visiting http://localhost/example.
The Virtual Host
We now have an example website, however, it is not yet a virtual host. A virtual host allows us to rename the site and accept traffic from the Internet using a proper domain name instead of using your machine’s local host.
Creating the Configuration File
Every virtual host needs a configuration file. In Ubuntu 18.04, these files are stored in /etc/apache2/sites-available . Here, you will find two files on a clean Apache installation. These two files are called 000-default.conf and default-ssl.conf. This example will not cover ssl connections, therefore, only the file 000-default.conf requires our attention.
Copy the default configuration file
We will copy the default configuration file and save it as example.conf. Use the following commands to navigate to the proper directory to create the new configuration file.
cd /etc/apache2/sites-available sudo cp 000-default.conf example.conf
Now, example.conf can be used to configure the new virtual host that we are establishing. Open the file in Nano (or your preferred text editor).
sudo nano example.conf
You’ll notice that a line that starts with # is a comment. The virtual host can be configured using a wide assortment of Apache directives. However, only a few key directives will be noted in this demonstration.
Servername and documentroot
The main directives that we will configure are ServerName and DocumentRoot. In your editor, you will notice the line:
Remove the # that starts the line and change www.example.com to a proper domain name that you own. For our purposes, www.example.com is fine and we can change our machine’s host file to direct our browser to the proper server name. This means that, instead of visiting the example site we created earlier using http://localhost/example, we can use www.example.com. More than that, our site can now be opened to the Internet using a domain name that has been purchased from a proper provider.
After the ServerName has been configured, go down to the line that reads:
Add the route to the example site that we established earlier. The new configuration should appear as this:
Save the file and move on to the next step.
Enable the Site
After the configuration file has been updated and saved, the site that has been established in the virtual host must be enabled. Then, the server must be restarted or reloaded. Use the following commands to enable the Apache site and to reload the server.
sudo a2ensite example.conf sudo systemctl reload apache2
Visit the Site
At this point, the virtual host is configured and the server has enabled the site. However, because of DNS, we must do some additional system configuration to direct www.example.com to the site that we have created.
You can port-forward to your server from your router if you have a commercial domain name. But, for this example, we will change our machine’s host file in order to visit the site on our local host using the www.example.com domain name.
Edit the hosts file
Open the hosts file in nano using the sudo command.
sudo nano /etc/hosts
You should see a file that looks something like:
127.0.1.1 myhostname 127.0.0.1 localhost # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters ff02::3 ip6-allhosts
We will tell our host to search for the local host when we navigate to www.example.com. To do this, we will add a new ipv4 address under the line
Add the address and domain name as:
127.0.0.1 localhost www.example.com
Save the file and open a web browser. Visit www.example.com in your browser and you should now see the website that we have created. This virtual host can now be configured to connect to the Internet using a valid domain name.