Virtual Hosts are used to setup more than one domain or websites using a single IP address. This is very useful if anybody wants to run multiple websites using a single IP address on single VPS.
In this tutorial, let me show how to setup virtual hosts in Apache web server on Ubuntu 15.04 server. Also, this method should work on previous Ubuntu distributions such as Ubuntu 14.10, 14.04 etc.
Scenario
For the purpose of this tutorial, I will be using Ubuntu 15.04 64bit server version, and I am going to host two testing websites namely “ami1.net” and “ami2.net”.
My test box IP address and hostname are 192.168.1.2/24 and localhost respectively. Be sure to modify the virtual domain names as per your requirement.
Install Apache Webserver
Prior to install apache server, let us update our Ubuntu server:
To do that, run:
sudo apt-get update
Now, install apache web server using the following command:
sudo apt-get install apache2
After installing apache server, let us test whether the webserver is working properly or not by navigating to the URL http://ip-address/.
As you see in the above picture, apache webserver is working.
Now, let us proceed to setup virtual hosts in Apache web server.
Setup Apache Virtual Hosts
1. Create Virtual Directories
Now, let us proceed to setup virtual hosts. As I mentioned earlier, I am going to host two virtual hosts called “ami1.net”, and “ami2.net”.
Create a public directory to place the two virtual hosts data.
First, let us create a directory for ami1.net site:
sudo mkdir -p /var/www/html/ami1.net/public_html
Then, create the directory for ami2.net site:
sudo mkdir -p /var/www/html/ami2.net/public_html
2. Setting Up Ownership and Permissions
The above directories are owned by root user now. We should change the ownership of these two directories to the regular user.
sudo chown -R $USER:$USER /var/www/html/ami1.net/public_html/
sudo chown -R $USER:$USER /var/www/html/ami2.net/public_html/
The “$USER” variable indicates the currently logged in user.
Set the read permissions to the Apache web root (/var/www/html/) directory, so that everyone can read files from that directory.
sudo chmod -R 755 /var/www/html/
We have created the directories for holding the websites data and assigned the necessary permissions and ownership to them.
4. Create Sample pages for Virtual Hosts
Now, we have to create the sample pages to be served through the websites.
First, let us create a sample page to the localhost virtual host.
Create a ‘index.html’ for unixmen1.local virtual host,
sudo vi /var/www/html/ami1.net/public_html/index.html
Add the following contents:
<html> <head> <title>ami1.net</title> </head> <body> <h1>Welcome to ami1.net</h1> </body> </html>
Save and close the file.
Similarly, add the sample page to the second virtual host.
sudo vi /var/www/html/ami2.net/public_html/index.html
Add the following contents:
<html> <head> <title>ami2.net</title> </head> <body> <h1>Welcome To ami2.net website</h1> </body> </html>
Save and close the file.
5. Create Virtual Host Files
By default, Apache comes with a default virtual host file called 000-default.conf. We will copy the 000-default.conf file contents to our new virtual host files.
cd /etc/apache2/sites-available/000-default.conf cp 000-default.conf ami1.net.conf
cd /etc/apache2/sites-available/000-default.conf cp 000-default.conf ami2.net.conf
Make sure the virtual host files contains .conf extension at the end.
Now, modify the unximen1.local.conf file to reflect with our new own values.
sudo vi /etc/apache2/sites-available/ami1.net.conf
Make the relevant changes that reflect to the unixmen1 site.
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@ami1.net ServerName ami1.net ServerAlias www.ami1.net DocumentRoot /var/www/html/ami1.net/public_html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost>
Like wise, modify the second virtual host file.
sudo vi /etc/apache2/sites-available/ami2.net.conf
Make the relevant changes that reflect to the unixmen2 site.
<VirtualHost *:80> # The ServerName directive sets the request scheme, hostname and port that # the server uses to identify itself. This is used when creating # redirection URLs. In the context of virtual hosts, the ServerName # specifies what hostname must appear in the request's Host: header to # match this virtual host. For the default virtual host (this file) this # value is not decisive as it is used as a last resort host regardless. # However, you must set it for any further virtual host explicitly. #ServerName www.example.com ServerAdmin webmaster@ami2.net ServerName ami2.net ServerAlias www.ami2.net DocumentRoot /var/www/html/ami2.net/public_html # Available loglevels: trace8, ..., trace1, debug, info, notice, warn, # error, crit, alert, emerg. # It is also possible to configure the loglevel for particular # modules, e.g. #LogLevel info ssl:warn ErrorLog ${APACHE_LOG_DIR}/error.log CustomLog ${APACHE_LOG_DIR}/access.log combined # For most configuration files from conf-available/, which are # enabled or disabled at a global level, it is possible to # include a line for only one particular virtual host. For example the # following line enables the CGI configuration for this host only # after it has been globally disabled with "a2disconf". #Include conf-available/serve-cgi-bin.conf </VirtualHost>
After modifying the virtual hosts files, disable the default virtual host (000.default.conf), and enable new virtual hosts as shown below.
sudo a2dissite 000-default.conf
sudo a2ensite ami1.net.conf
sudo a2ensite ami2.net.conf
Finally, restart the apache service.
In Ubuntu 15.04:
sudo systemctl restart apache2
In Ubuntu 14.10 and previous versions:
sudo service apache2 restart
That’s it. Now, we successfully configured the apache virtual hosts on our Ubuntu server.
Testing Virtual Hosts
Edit file /etc/hosts,
sudo vi /etc/hosts
Add the virtual domain names one by one as shown below.
[...] 192.168.1.2 ami1.net 192.168.1.2 ami2.net
Save and close the file.
Open up your browser and point to the URL
This way we can create multiple virtual hosts….