Ubuntu Hardy Heron (8.04): Virtual host with mod_rewrite
Tuesday, 13 October 2009 09:33
Virtual host is really what the name says, a virtual environment over the top of your current web server to simulate a separate hosting environment. Using virtual host you can enable site specific features and keep your development environment totally separate from another one. For example, if you want to experiment a simple application with both mod_rewrite enabled or disabled, you can setup two virtual host with these different settings to take place. In this blog post I will show you how to set up virtual host in ubuntu hardy heron with mod_rewrite enabled.

Step 1: setup a virtual domain
open /etc/hosts and add a virtual domain with a specific local IP. In this file it contains ip and domain name separated by a space. You can also add the port using a colon with the IP. Lets assume that our virtual domain name is “twitter.php” – and It will listen to the ip “127.0.1.2″
so we will add the following line to our /etc/hosts file
127.0.1.2:80 twitter.php
now whenever you point to http://twitter.php – your browser will actually open http://127.0.1.2:80
Step 2: configure virtual host with apache
here we will configure our newly added virtual domain against apache as a virtual host, and did I forget to say, with mod_rewrite enabled :)
goto /etc/apache2/sites-available and create a file named “twitter.php” – I recommend to keep it the same name as your virtual domain.
sudo nano /etc/apache2/sites-available/twitter.php
write the following contents inside. but please note to create the appropriate directory before linking your virtual host with that, for example we’ve create a folder named “/home/<user name>/www/ilovephp” and linked that directory as my document root in the following configuration file.
<VirtualHost 127.0.1.2:80>
ServerName twitter.php
ServerAlias www.twitter.php
ServerAdmin
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
DocumentRoot /home/<user>/www/ilovephp
<Directory /home/<user>/www/ilovephp>
Options FollowSymLinks
AllowOverride All
</Directory>
</VirtualHost>
now create a symbolic link of this file to /etc/apache2/sites-enabled directory as “twitter.php”
sudo ln -s /etc/apache2/sites-available/twitter.php /etc/apache2/sites-enabled/twitter.php
Step 3: restart apache (or reload)
simple, either one of the followings
sudo /etc/init.d/apache2 restart
or
sudo /etc/init.d/apache2 reload
or
sudo a2ensite twitter.php
and you are done! now you can point your browser to http://twitter.php

written by Abigail31Gallagher, March 04, 2010