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
127.0.1.2:80 ilove.php
now whenever you point to http://ilove.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 “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 admin@ilove.php
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
or
sudo /etc/init.d/apache2 reload
or
sudo a2ensite ilove.php
and you are done! now you can point your browser to http://ilove.php














14 responses so far ↓
krig // May 29, 2008 at 9:50 pm
Instead of duplication
sudo cp /etc/apache2/sites-available/ilove.php /etc/apache2/sites-enabled/ilove.php
it’s much better to create symbolic link
cd /etc/apache2/sites-enabled
sudo ln -s /etc/apache2/sites-available/ilove.php ilove.php
hasin // May 29, 2008 at 9:56 pm
Thanks Krig
I’ve updated the article
Jay Pipes // May 29, 2008 at 10:24 pm
Hi! I don’t see how this has to do with mod_rewrite. As far as I understand, this is the normal way to do multiple localhosts on a debian system, regardless of mod_rewrite.
Also, krig, here is a shortcut to the ln -s stuff:
sudo a2ensite ilove.php
Cheers,
Jay
hasin // May 29, 2008 at 10:36 pm
Hello Jay
AllowOverride All
This is the one behind making mod_rewrite work.
Ahsan // May 29, 2008 at 10:38 pm
I was just about to mention it when I saw Jay already mentioned it. In step 3, instead of manually creating the symbolic link, we can use
sudo a2ensite ilove.php
And we can easily disable the site using
sudo a2dissite ilove.php
Though its a debian/ubuntu specific command, but reduces the hassle of creating and removing symbolic links. And, this article is about ubuntu anyway
Ahsan // May 29, 2008 at 10:38 pm
Oops, my bad, I meant step 2
hasin // May 30, 2008 at 11:55 am
@Ahsan
Thanks Ahsan
Setting up virtual host with mod rewrite enabled in Ubuntu Hardy Heron | Programmers Blog // May 31, 2008 at 5:00 pm
[...] Read More: http://hasin.wordpress.com/2008/05/29/setting-up-virtual-host-with-mod-rewrite-in-ubuntu-hardy-heron... [...]
Madhusudan.C.S // June 2, 2008 at 9:53 am
Hi,
Thanks a lot for the post. I had a similar article myself on virtual hosts and have been following that procedure on Feisty and Gutsy. But somethings seems not to be working in Hardy. I have created a file called drupal6 and it looks like this
ServerAdmin madhusudancs@gmail.com
ServerName drupal6
ServerAlias http://www.locald6.com
DocumentRoot /home/madhu/mywebdevelopment/drupal6.0/
Options FollowSymLinks
AllowOverride All
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
Alias /doc/ “/usr/share/doc/”
Options Indexes MultiViews FollowSymLinks
AllowOverride None
Order deny,allow
Deny from all
Allow from 127.0.0.0/255.0.0.0 ::1/128
and as you said have enabled the site and have added 127.0.0.3 drupal6
to the host. But when I give drupal6 in browser it gives the following error
403 Forbidden
You don’t have permission to access / on this server.
My apache error.log has the following entries
Mon Jun 02 09:18:50 2008] [crit] [client 127.0.0.3] (13)Permission denied: /home/madhu/mywebdevelopment/.htaccess pcfg_openfile: unable to check htaccess file, ensure it is readable
Note that even though my DocRoot points to one directory below i. /home/…/mywebdevelopment/drupal6.0 it is looking for .htaccess in mywebdevelopment. So just to test I put my .htaccess copy there also. Still it shows the same error. Permission on all the files and directories is set to 777.
Please help me. Thanks a lot
Alfredo Herrejon // June 21, 2008 at 4:50 am
Hi, thanks for this howto, i am getting the next error when i try to reload apache2
NameVirtualHost *:80 has no VirtualHosts
can somedody help me please?
Md. Mahmud Ahsan // July 23, 2008 at 12:50 pm
Nice tutorial hasin vaia. Today I created virtual host in my local server. But I used gedit instead of nano
Batmunkh // August 24, 2008 at 2:52 am
nice tut.
Alfred! i got same error. but i solved it as:
if you use NameVirtualhost *:80 then use
else
NAMEVIRTUALHOST * then use
i don’t know why it happens. but when i did it the error has gone.
Ken // August 29, 2008 at 2:06 am
Hi,
I’ve never had to do this, and am still a bit confused.
Here is my setup.
Ubuntu 8.04 running on a remote server
Apache2
Tomcat 5.5
My webserver is running at http://www.mysite.com successfully. I have a separate application that I installed can only get to by typing in the url:
http://www.mysite.com:8080/accounting
I want users to type in
http://www.mysite.com/accounting
I know this should be ‘easy’ but I’ve google for hours and not found ‘the step-by-step’ files to edit and text to enter to do this. Can anyone help? Thanks in Advance!
Ken
redivide // September 7, 2008 at 11:14 am
I tried this method but it didn’t work for me… did some more googling and got this, which seems to work:
Setting Up A Name Based Virtual Host (vHost)
Leave a Comment