Setup Virtual Hosts with Apache Web Server on Linux

First thing you need to do is to install apache in your machine.

Apache Installation

Apache gives its functionality and the components to individual units which can be customized and configured independently. The basic unit that describes a site or a domain is called a virtual host.

This allows the administrator to use one server to host multiple domains or sites with a single interface or IP address by using a mechanism.

$ sudo apt-get update
$ sudo apt-get install apache2

Create the Directory Structure

A directory structure shall hold the site information and data which will be served to visitors. By default, the top-level directory for apache is /var/www and we will be setting them to the individual directories under the /var/www directory for each site/domain.

$ sudo mkdir -p /var/www/firstSite.com
$ sudo mkdir -p /var/www/secondSite.com

Grant Permissions

Our directory’s owned by our root user. If we want our normal user to be able to modify files in our web directories, then we have to change the ownership to others normal users.

$ sudo chown -R $USER:$USER /var/www/firstSite.com
$ sudo chown -R $USER:$USER /var/www/secondSite.com
$ sudo chmod -R 755 /var/www

Create Demo Pages for for both sites.

To create a file we use touch command, The touch command is a standard command used in UNIX/Linux operating system which is used to create, change and modify timestamps of a file.

$ touch /var/www/webserver1.com/index.html

Then put some lines of html.

<!DOCTYPE html>
<html>
<head>
<title>Site one</title>
</head>
<body>

<h1>Site one heading</h1>
<p>My first paragraph.</p>

</body>
</html>

We’ll copy and past the same page for second server and modify it accordingly

$ cp /var/www/firstSite.com/index.html /var/www/secondSite.com/index.html
$ nano /var/www/webserver2.com/index.html
<html>
   <head>
      <title>Welcome to secondSite.com!</title>
   </head>
   <body>
      <h1>Success!</h1>
   </body>
</html>

Create New Virtual Host Files

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/firstSite.conf
$ sudo nano /etc/apache2/sites-available/firstSite.com.conf
<VirtualHost *:80>
   ServerAdmin admin@firstSite.com
   ServerName firstSite.com
   ServerAlias www.firstSite.com
   DocumentRoot /var/www/firstSite.com/
   ErrorLog ${APACHE_LOG_DIR}/error.log
   CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Copy First Virtual Host file and customize for Second Domain webserver2.com

$ sudo cp /etc/apache2/sites-available/firstSite.com.conf /etc/apache2/sites-available/secondSite.com.conf
$ sudo nano /etc/apache2/sites-available/secondSite.com.conf

Do the same thing for secondSite VH

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

Enable the New Virtual Host Files

$ sudo a2ensite webserver1.com.conf
$ sudo a2ensite webserver2.com.conf

We needed to restart Apache server

$ sudo service apache2 restart

Testing the Websites

$ sudo /etc/hosts
192.168.1.84 website1.com
192.168.1.84 website1.com
Facebook Notice for EU! You need to login to view and post FB Comments!