Friday, November 23, 2007

 

How to configure subdomains in localhost

To configure subdomains with localhost, you have to modify two different elements:
- your dns so that localhost subdomains do point to localhost
- web server (apache) settings

1) if you try http://test.localhost, the web browser tries to find such name on the web and does not understand it's still localhost
=> you must set it explicitly:
- modify "/etc/hosts" and map "127.0.0.1" to "localhost test.localhost" with our example (=> do it manually for each subdomain, unless you find a way with regular expressions to add a generic rules)
2) now you need to configure apache web server if you want each subdomain to point to a different location on your hdd
2.a) open /etc/apache2/httpd.conf
2.b) you need to declare an entry for the subdomains you want, for example you can use the following code (with no space after '<' - used here otherwise interpreted as HTML node and not displayed + > not interpreted):
<VirtualHost *>
     #Primary VHOST
     DocumentRoot /var/www
     ServerName localhost
< /VirtualHost>
<VirtualHost *>
     DocumentRoot /home/remy/www
     ServerName test.localhost
< /VirtualHost>
In this example, each test.localhost has its own root folder (/home/remy/www), different from the generic one (/var/www).
Now restart apache '/etc/init.d/apache2 restart'

Using subdomains with localhost is very usefull when you work on different website projects locally and some of them are based on the zend framework. By doing so, you don't need a dedicated web server (lamp) for each project, which is a nonsense in itself.

Comments: Post a Comment

<< Home

This page is powered by Blogger. Isn't yours?