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
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
Thanks Krig
I’ve updated the article
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
Hello Jay
AllowOverride All
This is the one behind making mod_rewrite work.
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 😉
Oops, my bad, I meant step 2
@Ahsan
Thanks Ahsan 🙂
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
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?
Nice tutorial hasin vaia. Today I created virtual host in my local server. But I used gedit instead of nano 😉
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. 🙂
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
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)
veru good and very informatic tutorial for the newbies like me .
thanx guys a tutorial is very good indeed i will try
thanx
generic fioricet online-no prescription
generic fioricet online-no prescription
Fioricet order overnight
Fioricet order overnight
I love this tutorial. It used to work great but I just installed ubuntu 9.1 and it ceased to work as well.
Apache has changed a few things like the httpd.conf file is now empty. Apache is making things more modular. Anyway enabling mod_rewrite is a little different now. Its a sym link. But the MAIN THING is that I could not get the name-based virtual host to work until I set this line:
to
So after hours of work. That is what it took.
it removed the code.
I just learned what some of the other guys learned above that u have to change
VirtualHost 127.0.1.2:80
to
VirtualHost *:80
I have another site in sites-available “mysite”. (Like you just tutored)
Then I a2dissite default reloaded apache, a2ensite mysite, & then reloaded apache2.
I get success messages at each step.
But when i open localhost, index.html of default opens.
Any help is greatly appreciated…
Thanks…
Thank you very much. It was really helpful and useful as well 🙂
perfect, thanks!