Configure Virtual Host on Apache web server

0

Virtual host is a feature of apache web server. This feature helps to setup several domains on one single server. Also we can put several other rules according to domain names. First of all let’s try to know what are the important issues make us use this features.

  1. We can define different domains as virtual hosts
  2. We can put different modules for different domains.
  3. We can use ssl and tsl security according to client’s requirement on domains.
  4. We can get report according to the domain name using virtual host facility.

We can configure a virtual host using two components. IP address and domain name. if we configure the virtual host using IP address then we need to get a single IP for every single virtual host separately. But if we create virtual host using domain name then we just need to put the record on our DNS server and we can get more than one domain name on a single IP address.

So it is recommended that to create virtual host using domain names and put rules and allow modules according to the domain names.

Now let’s learn how to create a virtual host on a apache server.

First of all we need to make sure that our apache web server is installed properly and working. Now there are two ways to setup virtual host. We can put the configuration directly in the httpd configuration file or we can create and use configuration file separately.

But the basic configuration is same for both the cases. So let’s know about what are the configurations we need to put into configuration file. The basic lines to put into the configuration lines are given below.

<VirtualHost (IP ADDRESS or Domain Name)>
ServerName domain_name
ServerPath /domain_name
DocumentRoot /rootdirectorypath
</VirtualHost>

Well these above lines are the basics; we can add more lines to configure more issues to a virtual host.

Now if we want we can easily put this on the httpd.conf file. It will work. But the only issue is that this is not the best practice.

Let’s talk about the best practice. To create a virtual host we should follow the below steps.

  1. Create two folders in /var/www/html directory named sites-registered and sites-enabled. Run the below commands.

#mkdir /etc/httpd/sites-registered & mkdir /etc/httpd/sites-enabled

  1.  Now open /etc/httpd/conf/httpd.conf and insert the below line.

#vim /etc/httpd/conf/httpd.conf

IncludeOptional sites-enabled/*.conf

  1. Now save and exit this file. Then go to the following directory.

#cd /etc/httpd/sites-registered

  1. Create and edit a file using vim. We will take a virtual host for the domain name test.local and let’s name the file as text.conf. so run the following command and edit the file.

#vim /etc/httpd/sites-registered/test.conf

Now put the below text into this file.

<VirtualHost *:80>

ServerName www.test.local

ServerAlias test.local

DocumentRoot /var/www/test.local/public_html

ErrorLog /var/www/test.local/error.log

</VirtualHost>

  1. Once the above step is done. There are only 2 more steps we need to complete and then we can restart the service to active these changes. On this step we need to link this file with sites-enabled directory and create a link file in there. For that fun the below command.

#ln –s /etc/httpd/sites-registered/test.conf  /etc/httpd/sites-enabled/test.conf

  1. Now restart the httpd service and active our virtual host. So run the below command.

#systemctl restart httpd.service

After this we can create an index and put some text to confirm if the file is working or not.

Note that, we have to put a DNS entry on a corresponding name server so that we can resolve the domain name.

Leave A Reply

Your email address will not be published.