Friday, December 19, 2008

Apache virtual hosts (Done on F10)

This is about the name based virtual hosts using apache. I haven't tried ip-based virtual hosts. This is because of two reasons. (a) I do not have multiple IPs. (b) Even though it is possible to try this with private ips, it will only have only academic value and for a fact, I know that doing that will be really simple. (A simple soln: Run one apache per ip and set document root to any given directory)

To create a virtual host, go to the last line of your httpd.conf. You will see an example there. Just copy-n-paste the example and make the necessary changes. To create a virtual host, just do this much:

NameVirtualHost *:80


ServerAdmin webmaster@sarin.net.in
DocumentRoot /var/www/mydomain
ServerName mydomain.co.in
ErrorLog logs/mydomain-error_log
CustomLog logs/mydomain-access_log common


Now create the directory.
mkdir /var/www/mydomain
echo My homepage > /var/www/mydomain/index.html
Change the ownership of the directory
chown -R apache.apache /var/www/mydomain

Now, just restart httpd
service httpd restart

Note 1: You can create multiple virtual host entries by repeating ... block. (Also create directories with correct permission and restart service). "mydomain" & "mydomain.co.in" need to be replaced with your domain name.
Note 2: I have noticed that log file names cannot be same. They have to be different for different domains.

No comments: