How To Set Up Apache Virtual Hosts on Ubuntu 16.04


How To Set Up Apache Virtual Hosts on Ubuntu 16.04

Apache allows you to run more than one website on a single server with the help of Virtual Hosts. Let us look at how to set up Apache Virtual Hosts on Ubuntu 16.04.

 

How To Set Up Apache Virtual Hosts on Ubuntu 16.04

Before we proceed, please ensure that you have installed Apache on Ubuntu Server or Desktop. Next, connect to your Ubuntu server via SSH or terminal session.

 

1. Create a Directory

Create a directory where you will store all your website’s files. So if your domain is example.com, you can create a directory called example.com in /var/www


$ sudo mkdir /var/www/example.com

 

2. Create an index.html file

Next, create an HTML file called index.html in the directory we just created.


$ sudo vim /var/www/example.com/index.html

Add the following HTML to that file. Save and close it.


<html>
 <head><title>example.com</title></head>
 <body>example.com</body>
</html>

 

3. Create Virtual Hosts File

Apache already has a default Virtual Hosts file called 000-default.conf that we can use to create our virtual hosts file. It handles all requests directed to the domains that are pointed to this server but don’t have virtual hosts assigned to them.

We copy 000-default.conf to create example.com.conf


$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

 

Open the virtual hosts file:


$ sudo vim /etc/apache2/sites-available/example.com.conf

 

It will look like:


<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/html
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

 

In the above code block, you’ll see the DocumentRoot is pointed to /var/www/html. Update it to the directory we created in step 1


DocumentRoot /var/www/example.com

 

Also, we need to the domain name (example.com) to this virtual host file. You can do that with the ServerName and ServerAlias directives


ServerName example.com
ServerAlias www.example.com

 

Your Virtual Hosts file will look like:


<VirtualHost *:80>
 ServerAdmin webmaster@localhost
 DocumentRoot /var/www/example.com
 ServerName example.com
 ServerAlias www.example.com
 ErrorLog ${APACHE_LOG_DIR}/error.log
 CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

 

Save and close the file.

 

4. Enable Virtual Host

Use the a2ensite to enable the virtual host that we have created


$ sudo a2ensite example.com.conf

 

Reload Apache to apply the changes


$ service apache2 reload

 

5. Test the Site

In your domain registrar’s website, ensure that the A record of your domain points to your server IP address. Open a browser and visit http://www.example.com

You will see the contents of your HTML file.

How To Set Up Apache Virtual Hosts on Ubuntu 16.04

 

If you want to access example.com from a local server, you will need to edit the /etc/hosts file and add the following line to it.


127.0.0.1 example.com

 

Conclusion

Now you know how to set up Apache Virtual Hosts on Ubuntu 16.04. If you want to manage more websites (example2.com, example3.com, etc) repeat the above steps for each website.

 

 

 

About Sreeram Sreenivasan

Sreeram Sreenivasan is the Founder of Ubiq, a business dashboard & reporting platform for small & medium businesses. Ubiq makes it easy to build business dashboards & reports for your business. Try it for free today!