Friday, December 26, 2008

Adding a Virtual Host to Apache2 based on Name in CentOS 5

Well today I was setting up our new web development server and I wanted to use Host / Name based virtual hosts in apache, instead of the previous use of ports.

Much to my amazment this one is really simple.

Make sure to uncomment or add in:
NameVirtualHost *:80
Since I don't want to add multple IP's to the server and I always want to use port 80 (The default for http). 

Then I added 

<VirtualHost *:80>
        ServerName scorpio.mydomain.local
        ServerAlias scorpio
        DocumentRoot "/var/www/html"
        ServerPath /html/
        DirectoryIndex index.php index.htm
</VirtualHost>
<VirtualHost *:80>
        ServerName dev.mydomain.local
ServerAlias www.mydowmain.local mydomain.local
        DocumentRoot "/var/www/html"
        ServerPath /html/
        DirectoryIndex index.php index.htm
</VirtualHost>
<VirtualHost *:80>
        ServerName dev.myotherdomain.local
ServerAlias www.myotherdowmain.local myotherdomain.local
        DocumentRoot "/var/www/html"
        ServerPath /html/
        DirectoryIndex index.php index.htm
</VirtualHost>
<VirtualHost *:80>
        ServerName dev.mythirddomain.local
ServerAlias www.mythirddowmain.local mythirddomain.local
        DocumentRoot "/var/www/html"
        ServerPath /html/
        DirectoryIndex index.php index.htm
</VirtualHost>

When it was all said and done this was pretty easy.  Not as easy as doing the same thing in IIS in my opinion but still very easy.  I could have tried this with Webmin but I did this all using vi from the CLI.

1 comment:

Matt said...

Nice post. Very clear and simple. I can definitely see this being used by someone who needs to add vhosts to apache. Thanks!