Thursday, January 24, 2008

Hosting multiple sites on a single server using the same IP

Apache has a great feature which allows hosting multiple sites using the same apache instance and even the same IP address. To know more click here

Following example shows how this can be done.

Open the httpd.conf configuration file which is being used by apache.

Add following at the end:

NameVirtualHost 127.0.0.1:80

<VirtualHost 127.0.0.1:80>
ServerAdmin your@email.com
DocumentRoot /doc/root/for/first/domain/
ServerName www.firstdomain.com
ServerAlias www.firstdomain.com
ErrorLog logs/firstdomain_error_log
CustomLog logs/firstdomain_access_log common
</VirtualHost>

<VirtualHost 127.0.0.1:80>
ServerAdmin your@email.com
DocumentRoot /doc/root/for/second/domain/
ServerName www.seconddomain.com
ServerAlias www.seconddomain.com
ErrorLog logs/seconddomain_error_log
CustomLog logs/seconddomain_access_log common
</VirtualHost>

Replace 127.0.0.1 with the appropriate IP address, firstdomain and seconddomain as per the requirement.

Restart apache.

I tried this with apache 2.0.59 and it works fine.

No comments: