In this article 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 virtual 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

<virtualhost *:80>
ServerAdmin webmaster@dummy-host.example.com
DocumentRoot "/usr/local/apache-2.4.37/docs/dummy-host.example.com"
ServerName dummy-host.example.com
ServerAlias www.dummy-host.example.com
ErrorLog "logs/dummy-host.example.com-error_log"
CustomLog "logs/dummy-host.example.com-access_log" common
</VirtualHost>

<virtualhost *:80>
ServerAdmin webmaster@dummy-host2.example.com
DocumentRoot "/usr/local/apache-2.4.37/docs/dummy-host2.example.com"
ServerName dummy-host2.example.com
ErrorLog "logs/dummy-host2.example.com-error_log"
CustomLog "logs/dummy-host2.example.com-access_log" common
</VirtualHost>

    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,

<virtualhost *:80>
ServerAdmin webmaster@1test.com
DocumentRoot "/usr/local/apache-2.4.37/htdocs/1test.com"
ServerName 1test.com
ServerAlias www.1test.com
ErrorLog "logs/1test.com -error_log"
CustomLog "logs/1test.com -access_log" common
</VirtualHost>

<virtualhost *:80>
ServerAdmin admin@2test.com
DocumentRoot "/usr/local/apache-2.4.37/htdocs/2test.com"
ServerName 2test.com
ErrorLog "logs/2test.com-error_log"
CustomLog "logs/2test.com-access_log" common
</VirtualHost>

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,

LoadModule vhost_alias_module modules/mod_vhost_alias.so

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.

Include conf/extra/httpd-vhosts.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.

<virtualhost 192.54.67.10:80>
ServerAdmin webmaster@1test.com
DocumentRoot "/usr/local/apache-2.4.37/htdocs/1test.com"
ServerName 1test.com
ServerAlias www.1test.com
ErrorLog "logs/1test.com -error_log"
CustomLog "logs/1test.com -access_log" common
</VirtualHost>

<virtualhost 192.54.67.11:80>
ServerAdmin admin@2test.com
DocumentRoot "/usr/local/apache-2.4.37/htdocs/2test.com"
ServerName 2test.com
ErrorLog "logs/2test.com-error_log"
CustomLog "logs/2test.com-access_log" common
</VirtualHost>

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

  1. Be the first to add a comment.