Virtual Hostterm refers to the method of running more than one website such as host1.domain.com, host2.domain.com, or www.domain1.com, www.domain2.com etc., 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 different IP address. With name-based virtual hosting, you can host multiple websites/domains on the same IP address. Virtual hosting can be useful if you want to host multiple websites and domains from a single physical server or VPS.
Download Free eBook: “Apache HTTP Server Cookbook”
Hope you got the basic idea of Apache virtual hosts. Today, we are going to see how to configure Apache virtual hosts in Ubuntu 16.04 LTS server. My test box IP address is 192.168.1.105 and hostname is ubuntuserver .
Configure Apache Virtual Hosts in Ubuntu 16.04 LTSFirst, we will see how to configure name-based virtual hosts in Apache webserver.
Configurename-based virtual hosts 1. Install Apache webserverMake sure you have installed Apache webserver. To install it on Ubuntu, run:
sudo apt-get install apache2Once apache installed, test whether it is working or not by browsing the apache test page in the browser.
Open your web browser and point it to http://IP_Address or http://localhost . You should see a page like below.
Good! Apache webserver is up and working!!
2. Create web directory for each hostI am going to create two virtual hosts, namely ostechnix1.lan and ostechnix2.lan .
Let us create a directory for first virtual host ostechnix1.lan. This directory is required for storing the data of our virtual hosts.
To do so, enter:
sudo mkdir -p /var/www/html/ostechnix1.lan/public_htmlLikewise, create a directory for second virtual host ostechnix2.lan as shown below.
sudo mkdir -p /var/www/html/ostechnix2.lan/public_htmlThe above two directories are owned by root user. We need to change the ownership to the regular user.
To do so, run:
sudo chown -R $USER:$USER /var/www/html/ostechnix1.lan/public_html sudo chown -R $USER:$USER /var/www/html/ostechnix2.lan/public_htmlHere, $USER refers the currently logged-in user.
Next, set read permissions to the Apache root directory i.e /var/www/html/ using command:
sudo chmod -R 755 /var/www/html/We do this because we already created a separate directory for each virtual host to storing their data. So we made the apache root directory as read only for all users except the root user.
We have created required directories for storing data of each virtual host, setup proper permissions. Now, it is time to create some sample pages which will be served from each virtual host.
3. Create demo web pages for each hostLet us create a sample page for ostechnix1.lan site. To do so, run:
sudo vi /var/www/html/ostechnix1.lan/public_html/index.htmlAdd the following lines in it:
<html><head>
<title>www.ostechnix.lan</title>
</head>
<body>
<h1>Hello, This is a test page for ostechnix1.lan website</h1>
</body>
</html>
Save and close the file.
Likewise, create a sample page for ostechnix2.lan site:
sudo vi /var/www/html/ostechnix2.lan/public_html/index.htmlAdd the following lines in it:
<html><head>
<title>www.ostechnix.lan</title>
</head>
<body>
<h1>Hello, This is a test page for ostechnix2.lan website</h1>
</body>
</html>
Save and close the file.
4.Create configuration file for each hostNext, we need to create configuration files for each virtual host. First, let us do this for ostechnix1.lan site.
Copy the default virtual host file called 000-default.conf contents to the new virtual host files like below.
sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ostechnix1.lan.conf sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/ostechnix2.lan.confPlease be mindful that you must save all configuration files with .conf extension at the end, otherwise it will not work.
Now, modify the configuration files to match with our virtual hosts.
Edit ostechnix.lan1.conf file:
sudo vi /etc/apache2/sites-available/ostechnix1.lan.confEdit/modify ServerAdmin, ServerName, ServerAlias and DocumentRoot values matches to virtual host.
<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@ostechnix1.lan
ServerName ostechnix1.lan
ServerAlias www.ostechnix1.lan
DocumentRoot /var/www/html/ostechnix1.lan/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>
Save and close the file.
Next, edit ostechnix2.lan.conf file:
sudo vi /etc/apache2/sites-available/ostechnix2.lan.confMake the necessary changes.
<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@ostechnix2.lan
ServerName ostechnix2.lan
ServerAlias www.ostechnix2.lan
DocumentRoot /var/www/html/ostechnix2.lan/public_html
# Available loglevels: trace8, ..., trace1, debug, info, notice, warn,
# error, crit, alert, emerg.
# It is also possible