How to Setup Virtual Hosts with Apache Web Server on Linux


In this article, you will learn about how to set up Apache virtual hosts on an Ubuntu Linux. During this process, you will learn how to serve different sites to different users depending on the domain/site they are requesting.

Apache Web Server

The Apache web server is the most popular and powerful way of serving web sites on the internet. It is used for more than half of all the active websites on the internet and is extremely powerful and flexible.

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.

Apache Installation

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

After these steps are complete, we can get started.

For Demo purpose, we shall configure apache to make a virtual host for webserver1.com and another for webserver2.com. This will be like a reference guide, but you should substitute your own site/domains.

Create the Directory Structure

The first step here would be that, 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.

For each of our site, we are going to make site directories –

$ sudo mkdir -p /var/www/webserver1.com
$ sudo mkdir -p /var/www/webserver2.com

Grant Permissions

Now, we have the directory structure of our site/domain, but they are created and 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/webserver1.com
$ sudo chown -R $USER:$USER /var/www/webserver2.com

We should also modify our permissions a little bit to ensure that read access is permitted to the general web directory and all of the files and folders it contains so that pages can be served correctly:

$ sudo chmod -R 755 /var/www

Your web server should now have the permissions it needs to serve the site, and your user should be able to create content within the necessary folders.

Create Demo Pages for Each Virtual Host

First, we shall start with webserver1.com. Then, we can create an index.html file in our editor by typing:

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

Edit the index.html file with a simple HTML script which indicates the site it is connected to. <html>

$ nano /var/www/webserver1.com/ index.html
<html>
   <head>
      <title>Welcome to webserver1.com!</title>
   </head>
   <body>
      <h1>Success! The webserver1.com virtual host is working!</h1>
   </body>
</html>
class="result notranslate"$ cp /var/www/webserver1.com/index.html /var/www/webserver2.com/index.html

We can then edit the file and modify the relevant pieces of information:

$ nano /var/www/webserver2.com/public_html/index.html
<html>
   <head>
      <title>Welcome to webserver2.com!</title>
   </head>
   <body>
      <h1>Success! The webserver2.com virtual host is working!</h1>
   </body>
</html>

Create New Virtual Host Files

Virtual host files are the configuration files that specify the virtual host and instructs the Apache to respond to various domain requests.

$ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/webserver1.com.conf
$ sudo nano /etc/apache2/sites-available/webserver1.com.conf
<VirtualHost *:80>
   ServerAdmin admin@webserver1.com
   ServerName webserver1.com
   ServerAlias www.webserver1.com
   DocumentRoot /var/www/webserver1.com/public_html
   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/webserver1.com.conf /etc/apache2/sites-available/webserver2.com.conf
$ sudo nano /etc/apache2/sites-available/webserver2.com.conf

You now need to modify all of the pieces of information to reference your second domain. When you are finished, it may look something like this:

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

Enable the New Virtual Host Files

Now that we have already created our virtual host files, we must enable these sites. Apache has its own tools that allow us to do this.

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

We needed to restart Apache to make these changes take effect:

$ sudo service apache2 restart
* Restarting web server apache2
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1.
Set the 'ServerName' directive globally to suppress this message.
This is a harmless message that does not affect our site.

Testing the Websites

As these site/domains are not the public so no entry will be there to easy the steps we needed to edit the /etc/hosts file so that if we browse with domain names webserver1.com and webserver2.com we can see the web pages

$ sudo /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.84 website1.com
192.168.1.84 website1.com

Now that you have two hosts configured, you can test your setup easily by opening the favorite browser and open the domains.

http://webserver1.com


http://webserver2.com

After the configuration you should now have a single server handling two separate domain/site names. You can expand this process by following the steps we outlined above to make additional virtual hosts. There is no software limit for configuring the domain names Apache can handle any no of sites, we can make as many as sites/domains till your servers are capable of handling them.

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 20-Jan-2020

17K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements