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.

4 Responses to Setting up Apache Virtual Hosts on CentOS

    • Thank u David, I’ve been solved the pblorem. I have to use the Windows Installer for PHP 5.2.0, I just leave the Apache 2.2.3 and reinstall PHP. But I don’t understand, ’cause I had (previously) Apache 1.5.x or 2.2.0 (i can’t remember well!) and PHP 5.2.0 and I don’t use the Windows Installer for PHP, just extract the files of PHP and configure my Apache and it’s done, but now not, anyway, thank u so much for the help, and I sorry (dumb, I don’t check it the Windows Installer, anyway!!)

  1. Pingback: How can I set up Virtual Hosts in Centos7? | Question and Answer

Leave a Reply to Gilson Cancel reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>