How to Run Virtual hosts in Apache webserver

 
    In this articles we will see about setting up virtual hosts on apache. Virtual host helps us to run multiple web sites on a single server.
There are 2 types of virtual hosting in Apache,

Types of Virtual hosts:
  1. Name-based virtual hosting.
  2. IP-based virtual hosting.
Name-based virtual hosting:

    Name-based vitual hosting allows you to run more domains under a single IP. The conf directory for virtual host is /usr/local/apache-2.4.37/conf/extra/httpd-vhost.conf




        We are going to run 2 domains www.1test.com and www.2test.com. After making the changes to the httpd-vhost.conf the file will be look like,



Now we have setup two virtual host using name-based virtual hosting,

The * match all addresses, so the main server serves no requests,
80 is the default port for httpd, so all the request are handled by http.

ServerAdmin      - The email address for the admin of the host.
DocumentRoot   - The document path where the web files of the host are maintained
ServerName       - The host name.
ServerAlias         - An alias for your host name.
ErrorLog            - Path for the error logs written.
CustomLog         - Path for the access logs written.


To enable virtual host the following things should be verified on httpd.conf file,

Vhost module should be enabled,



The above line should be uncommented on httpd.conf to enable the vhost module.

Including the vhost conf file,

        We can maintain the virtual host on the httpd.conf itself since we are going to maintain virtual hosts on default conf we need to include them on the httpd.conf.



Uncomment the above line on httpd.conf to the include the file.
IP-based virtual hosting:

          IP based virtual hosting comes when you want to run your host on different IP on the same server, This also allows you to set multiple domains if you are having multiple interfaces on your server.


On the above vhost file you are running two virtual hosts on different IP 192.54.67.10 and 192.54.67.11.

Do not forget to test your configuration through -t command before starting the apache process.

Thats it, now you are running virtual hosts on your system.

Feel free to ask if you have any questions.

Comments

Popular Posts on this blog

How to Install LAMP Stack on Linux - Ubuntu 17.04

Creating and Running Virtual Machines in LINUX - KVM - Cli Mode

Apache Webserver - Installation from source (tar.gz and tar.bz2)

Replacing Commands for Linux Files - Using Perl and Sed

How to install Docker on Ubuntu-17.04

Setting up a Mail server using Roundcube - Part-3 - Centos 7

Blog Top Sites

Blog Log