Setting Up Apache Virtual Host Support

Last modified: 
Friday, March 27th, 2015

Overview

Notes on setting up VritualHost support in CentOS 5.8 without using the buggy GUI. CentOS 5.8 is a little outdated at the time of this writing, but a lot of managed hosting still runs on it, so here we are.

Add Name Based VirtualHost Support

Open /et/httpd/conf/http.conf for editing as a Root level user.

 
vim /etc/httpd/conf/http.conf

Look for the comment line "Use name-based virtual hosting and update it as follows:

#
# Use name-based virtual hosting.
#
NameVirtualHost *:80

Add support for multiple virtual host files

Add the following to the bottom of /etc/httpd/conf/http.conf to enable support for managing virtual hosts in multiple files stored in the directory /etc/httpd/conf/vhosts.

#
# Store virtual host configurations in individual files with the extension .conf.
#
Include /etc/httpd/conf/vhosts/*.conf

Create a directory for the vhost.conf files

mkdir /etc/httpd/conf/vhosts
chown root:root /etc/httpd/conf/vhosts

Create at least one virtual host

Create a at least one virtual host configuration. In this example I store all the sites in folders based on their domain name, like /var/www/vhosts/example.com. Web files are stored in the folder /var/www/vhosts/example.com/httpdocs and logs are stored locally in the directory /var/www/vhosts/example.com/logs because that's how Plesk does it and I often find myself mimicking Plesk servers. I prefix file names with digits to control which order they are loaded.

cd /etc/httpd/conf/vhosts
vim 000.example.com.conf
#
# A plausible virtual host definition by way of example
#
# Virtual host example.com

        ServerName example.com
        DocumentRoot /var/www/vhosts/example.com/httpdocs
        ErrorLog /var/www/vhosts/example.com/logs/error_log
        TransferLog /var/www/vhosts/example.com/logs/access_log
        DirectoryIndex index.php
        
                Options ExecCGI FollowSymLinks Includes IncludesNOEXEC SymLinksIfOwnerMatch
                AllowOverride all
        

Add vhost files as needed.

Run configtest and reload

/etc/init.d/httpd configtest

If configtest returns Syntax OK, it's probably safe to reload, but not always.

/etc/init.d/httpd reload

See Also

[[HowToAddAVirtualHostToApache|Working with VirtualHosts on Debian]]


The operator of this site makes no claims, promises, or guarantees of the accuracy, completeness, originality, uniqueness, or even general adequacy of the contents herein and expressly disclaims liability for errors and omissions in the contents of this website.