Sunday, December 24, 2006

Setting up name-based virtual host on apache

I just finished setting up name-based virtual host for one of my clients. The server is Red Hat Linux Enterprise 4 update 4 (RHEL) with public IP. After I have successfully setting up DNS server correctly in particular to access to this server, this is how I setup name-based virtual host for it.

What is name-based virtual host?
A feature for webserver (ie apache,IIS etc) to serve many websites with one IP. The server will serve webpages accordingly based on the name of the site (ie www.example.com , mail.example.com , blog.example.com etc2).

Let's do it.
This tip is for Red Hat and maybe the same for Fedora Core (FC). On Mandriva and other distros, the directive to change may be in different files and different folders.

Step 1
Open up /etc/httpd/conf/httpd.conf with your favorite text editor (I personally use joe in console).

Step 2
Coment out this line:

NameVirtualHost *:80


Step 3
Comment out these lines (in the VirtualHost containers and change accordingly to your server name:

ServerAdmin testuser@example.com
DocumentRoot /var/www/html/
ServerName www.example.com
ErrorLog /var/log/www.example.com.log
CustomLog /var/log/www.example.com.common.log common


ServerAdmin testuser2@example.com
DocumentRoot /var/www/html/email
ServerName mail.example.com
ErrorLog /var/log/mail.example.com.log
CustomLog /var/log/mail.example.com.common.log common


Note:
See the DocumentRoot directive in each VirtualHost container. It is different. And also see the difference of ServerName directive. It is used by Apache to differentiate the website served based on the name.

Step 4
Restart apache by issuing "service httpd restart" in console (without the double quote). Notice any error if any.

Step 5
Now you can try accessing your server with www.example.com and mail.example.com. See if it serves the right page. If yes, congratulations. You have successfully done it.

That's all there is to it. Have fun.

0 comments:

Post a Comment