Linux

CentOS(Community Enterprise Operating System) is an exceptional Linux1 distribution based on the Red Hat Enterprise Linux distribution.

What is a Virtual Host

A virtual host is a way for Apache to host different sites depending on the configuration. If you want your server, mostly likely a VPS or just your local machine, you can set virtual hosts for more than one website to work.

The guide assumes that you have Apache installed. If not, you should run the following command:

yum install httpd

Prepare your website

Create a directory in /var/www/html/example.com

mkdir /var/www/html/example.com

example.com can be anything. In our example, we will use example.com. In the example.com folder, create a file called index.html and put the following code.

<html>
<head><title>Website 1</title></head>
<body>The Body of the First Website</body>
</html>

Configurations

The Apache configuration for CentOS(Community Enterprise Operating System) is found at /etc/httpd/conf/ folder. The configuration file is http.conf

We will need to modify the file.

vim /etc/httpd/conf/httpd.conf

Below the file, you will find something similar to the following:

#
# VirtualHost example:
# Almost any Apache directive may go into a VirtualHost container.
# The first VirtualHost section is used for requests without a known
# server name.
#
#<VirtualHost *:80>
#    ServerAdmin webmaster@dummy-host.example.com
#    DocumentRoot /www/docs/dummy-host.example.com
#    ServerName dummy-host.example.com
#    ErrorLog logs/dummy-host.example.com-error_log
#    CustomLog logs/dummy-host.example.com-access_log common
#</VirtualHost>

Copy and paste the Virtualhost directive and uncomment. Configure it depending on the location of the website your created above. You should have something like the following:

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/html/example.com
    ServerName example.com
    ErrorLog logs/example.com-error_log
    CustomLog logs/example.com-access_log common
</VirtualHost>

Restart Apache

You will need to restart Apache for it to load the new configurations.

service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd:                                            [  OK  ]

You should now see your website when you visit example.com.

Rinse and Repeat

To create another Virtualhost, you would just duplicate the steps above. Create a your website and point the virtualhost to the right location.

If you have another website called example2.com, you would have this in the httpd.conf file.

<VirtualHost *:80>
    ServerAdmin webmaster@example.com
    DocumentRoot /var/www/html/example.com
    ServerName example.com
    ErrorLog logs/example.com-error_log
    CustomLog logs/example.com-access_log common
</VirtualHost>
<VirtualHost *:80>
    ServerAdmin webmaster@example2.com
    DocumentRoot /var/www/html/example2.com
    ServerName example2.com
    ErrorLog logs/example2.com-error_log
    CustomLog logs/example2.com-access_log common
</VirtualHost>

Virtualhosts are quite easy to create with Apache. However, here are some notes to consider:

  • Be careful not to duplicate virtualhosts. This will cause errors.
  • There will be warnings when you create virtualhosts that point to an invalid location.

1 Linux is a registered trademark of Linus Torvalds