Setting up Apache Virtual Hosts on CentOS

So you’ve got a new VPS host from HostGator. You want to host yours and your 5,000 closest friends’ sites too: but in order to do that you have to setup your apache server’s virtual hosts.

Step 1. Make a Directory to contain your Virtual Host files

[root@two]# cd /etc/httpd/
[root@two httpd]# mkdir sites-available
[root@two httpd]# mkdir sites-enabled

Now that you’ve created ‘sites-enabled’ and ‘sites-available’, we’re going to make Apache check any of the conf files you have in ‘sites-enabled’.

Step 2. Tell Apache to Look for your Conf Files

[root@two httpd]# vim conf/httpd.conf

Add these lines to the end of your ‘httpd.conf’ file:

NameVirtualHost *:80
Include /etc/httpd/sites-enabled/

Step 3. Create Some Configuration Files for your Sites

[root@two httpd]# cd sites-available/
[root@two sites-available]# cat >
# (/etc/httpd/sites-available/
<VirtualHost *:80>
        ServerAlias *

        #Indexes + Directory Root
        DocumentRoot /home/csmith/www/
        <Directory "/home/csmith/www/">
                Options Indexes FollowSymLinks MultiViews
                AllowOverride All
                Order allow,deny
                Allow from all

        #LOG FILES
        ErrorLog  /home/csmith/www/
        CustomLog /home/csmith/www/ combined

Change the DocumentRoot to wherever you want to host your website for example ‘/opt/development/username/’ and make sure that directory exists (user mkdir).
Make sure your log’s directory exists also.

Step 4. Symlink the Conf Files To ‘sites-enabled’

[root@two sites-available]# ln -s /etc/httpd/sites-available/ /etc/httpd/sites-enabled/

I symlink my sites into ‘sites-enabled’ because that way its easy to turn on and off sites (for development) without having to make/remake VHost files. Just ‘rm’ the file from sites-enabled if you want apache to stop serving it.

Step 5. Restart Apache

[root@two sites-available]# /etc/init.d/httpd restart


[root@two sites-available]# /etc/init.d/httpd graceful

That should work. Just place all your servable documents (or symlink things) into the DocumentRoot you specified. Voila…i hope.
I’ll throw a script to make this easy up on Github.