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 “ilove.php” – and It will listen to the ip “127.0.1.2”
so we will add the following line to our /etc/hosts file
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 “ilove.php” – I recommend to keep it the same name as your virtual domain.
sudo nano /etc/apache2/sites-available/ilove.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 ilove.php ServerAlias www.ilove.php ServerAdmin firstname.lastname@example.org 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 “ilove.php”
sudo ln -s /etc/apache2/sites-available/ilove.php /etc/apache2/sites-enabled/ilove.php
Step 3: restart apache (or reload)
simple, either one of the followings
sudo /etc/init.d/apache2 restart
sudo /etc/init.d/apache2 reload
sudo a2ensite ilove.php
and you are done! now you can point your browser to http://ilove.php