Automatic vHost with Apache
In my development machine i wanted to have a specific vHost for each project. Something that would allow me to type URL like:
- http://wordpress-test1.lan/
- http://phpmyadmin.lan/
- http://project1.lan/
I started to set for each one a vHost in Apache but after a while I've found myself with too many vHost files.
So I started to search for a more easy and fast way, and here it is:
Note: you need to install Apache mod_rewrite to make things work.
Preamble:
- You have Apache installed, and your DocumentRoot is /var/www/
- You have mod_rewrite installed and enabled
- You can modify the /etc/hosts file
Let's say we want to install a test/demo version of Wordpress and access it from the URL http://wp-test.lan/
- We download and untar Wordpress in /var/www/wp-test/
- We tell our machine to resolve the domain wp-test.lan with the IP 127.0.0.1. To make this we add a line to our hosts file.
hpatoio@namazu:~$ sudo su -
[sudo] password for hpatoio:
root@namazu:~#echo "127.0.0.1 wp-test.lan" >> /etc/hosts
root@namazu:~#exit
hpatoio@namazu:~$
- Now we have to change Apache config. Open /etc/apache2/sites-anabled/000-default with your favorite editor and add these lines inside the <VirtualHost *> directive:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^.]+)\.lan$
RewriteRule ^(.+)$ /%1$1 Basically :
- The first line turns the mod_rewrite on
- The second one tells Apache to keep in the host part of the URL (HTTP_HOST) everything that stands before ".lan"
- Finally the third line insert the string matched before (%1) between the host and the resource part (directory + file + query string) of the requested URL
- Restart Apache
hpatoio@namazu:~$sudo /etc/init.d/apache2 restart
Now if open your browser and you point it to the URL http://wp-test.lan/ you should see the WP installation page. Right ?
May 19, 2008 by simone
Trackback
Filed in: Ubuntu, apache
Comment feed