How to set up Apache Virtual Hosts on Ubuntu 16.04

How to set up Apache Virtual Hosts on Ubuntu 16.04

Spread the love

Apache is a free and open source web server, it is the most popular web server in the world, and is commonly used in Linux servers. It is developed and maintained by Apache Software Foundation, over half of all servers around the world are running this fast and secure web server. In this tutorial, we will show you how to set up Apache virtual hosts on Ubuntu 16.04, to host as many websites as you want, the limit is your server’s resources like RAM, disk space, etc.

What is virtual host?

Virtual host is used to host a number of websites on the same web server. The web server could be Apache, nginx, Litespeed, etc. Apache supports two types of virtual host:

Name based virtual hosts
IP based virtual hosts

The name based virtual host is commonly used to host multiple websites on the same server, whereas in IP based virtual host we can only configure one website on one IP address. In this tutorial, we will show you how to create name based virtual hosts. We will host domainone.com and domaintwo.com on a server, please replace them with your actual domain names.

How to create virtual host

Let’s SSH in to the server and proceed to the next step.

ssh root@Your_IP_Address -p7022

Create directory for domains’ webroot

mkdir -p /var/www/html/{domainone.com,domaintwo.com}/{public_html,logs}
set up Apache Virtual Hosts on Ubuntu 16.04

The command above will create four directories, two of them will be our domains’ document root, and the other tw will be used to store our apache log files:

/var/www/html/domainone.com/public_html
/var/www/html/domaintwo.com/public_html
/var/www/html/domainone.com/logs
/var/www/html/domaintwo.com/logs

Create test pages

Now, let’s create a test page for domainone.com

nano /var/www/html/domainone.com/public_html/index.html

<html>
<body>
<center><h1>This is domainone.com!</h1></center>
</body>
</html>

 

Save and exit nano with Ctrl + O then Ctrl + X

Let’s copy the index test page to domaintwo.com

cp /var/www/html/domainone.com/public_html/index.html /var/www/html/domaintwo.com/public_html/

Okay, the index test page has been copied, but we need to edit the index page for domaintwo.com

sed -i 's/domainone.com/domaintwo.com/g' /var/www/html/domaintwo.com/public_html/index.html

We have successfully created an index test page for both domain names. Let’s give permission to user www-data, the default user who runs the apache.

chown -R www-data: /var/www/html/
Apache Virtual Hosts on Ubuntu 16.04

Create a virtual host

Now, let’s create a virtual host configuration file for domainone.com. The virtual host configuration file should be ended with .conf extension

nano /etc/apache2/sites-available/domainone.com.conf
ServerAdmin [email protected]
ServerName domainone.com
ServerAlias www.domainone.com
DocumentRoot /var/www/html/domainone.com/public_html

ErrorLog /var/www/html/domainone.com/logs/error.log
CustomLog /var/www/html/domainone.com/logs/access.log combined

The virtual host configuration file for domainone.com has been created, now we only need to copy and edit it for domaintwo.com

cp /etc/apache2/sites-available/domainone.com.conf /etc/apache2/sites-available/domaintwo.com.conf
sed -i 's/domainone.com/domaintwo.com/g' /etc/apache2/sites-available/domaintwo.com.conf

We will give you a short explanation about the content of virtual host configuration file.

<VirtualHost *:80>
The virtual host is listening on port 80

ServerAdmin [email protected]

The ServerAdmin sets the contact address that the server includes in any error messages it returns to the client. You can specify your email address here, or even remove the line.

ServerName domainone.com
ServerAlias www.domainone.com

This directive is needed, it tells Apache that Apache will process the domain request. Apache will search for virtual host files and process all request if domain/subdomain match with the one configured on ServerName or ServerAlias directive.

DocumentRoot /var/www/html/domainone.com/public_html

This directive is also important, we need to tell Apache where the document root of domaincone.com is. If we don’t specify it, apache will load the default document root, which is in /var/www/html

ErrorLog and CustomLog are the directives to save the log of a domain name hosted on the server.

Enable virtual host

In Ubuntu, we can simply issue the following commands to enable the virtual hosts

a2ensite domainone.com
a2ensite domaintwo.com

Setting up Apache Virtual Hosts on Ubuntu 16.04

 

 

 

 

 

Those commands will enable the virtual hosts, but we also need to reload/restart Apache after enabling/disabling the virtual host. The command actually creates a symbolic link in /etc/apache2/sites-enabled. You can also run the following commands instead of invoking the ‘a2ensite’ command:

ln -s /etc/apache2/sites-available/domainone.com.conf /etc/apache2/sites-enabled/
ln -s /etc/apache2/sites-available/domaintwo.com.conf /etc/apache2/sites-enabled/

Now, restart apache to activate the new configuration.

systemctl restart apache2

That is all, we successfully created two Apache virtual hosts for the two domains. You can now open your web browser and navigate to the domain name of the newly created virtual hosts.

http://domainone.com

How to Setup Apache Virtual Hosts on Ubuntu 16.04

 

 

 

http://domaintwo.com

How to do I set up Apache Virtual Hosts on Ubuntu 16.04

 


How to set up Apache Virtual Hosts on Ubuntu 16.04Of course, you don’t need to set up Apache Virtual Hosts on Ubuntu 16.04, if you use one of our Linux Server Management Services – in which case, our technical support team will help you creating the virtual hosts immediately. They are available 24/7, and can cater to any questions or requests.

PS. If you liked this post on How to set up Apache Virtual Hosts on Ubuntu 16.04, feel free to share it with your friends by using the social media share shortcuts below, or simply leave a comment. Thanks.

Leave a Reply

Your email address will not be published. Required fields are marked *