Setting Up Name Based Virtual Hosting
I do apologize again for some of the recent downtime on the blog. I’ve been researching tuning options all evening to try to bring the memory consumption lower. I did also add an additionl 256M RAM to the machine hoping that will help. Thanks for the patience during this bumpy ride.
I wanted to put down some of my notes concerning configuring Apache2 with virtual hosts for hosting multiple sites. This is something I have done quite a bit before but had a little trouble on this last setup. So in an attempt to better document my steps here is what I did.
Installing Apache 2
I’ll be discussing name based virtual hosting with Apache (Apache 2 to be specific). We’ll need to install that before we dive in:
sudo aptitude install apache2
Once these are setup you should be able to connect to localhost in your browser and see a test page.
NameVirtualHost
With the default configuration you are only serving up one site, and that site is based on your IP address. What I’m setting up is name-based virtual hosting, meaning the Apache server will serve specific content based on the domain name requested. In this way a single server can host multiple sites, and serve up unique content based on the domain requested.
My preferred method of using name based virtual hosting is creating a seperate file for each domain. These can all be done within one file, but I’ll be creating a new file for each site.
First we need to define to Apache that we’re using name based virtual hosting instead of IP based. You can append the following line to your /etc/apache2/apache2.conf to define this:
NameVirtualHost ip.address:port
The above should be your public facing IP address (assuming you’re creating a public site), and port is generally port 80 by default. After this we’ll create the base configuration for your virtual hosts. Debian and Ubuntu use /etc/apache2/sites-available/ and /etc/apache2/sites-enabled/ directories for defining virtual hosting. One nice thing about this is that you can have more sites “available” than you have “enabled”, meaning not everything configured is actually live and listening. This is nice to quickly disable a site for whatever reason.
I like to create unique files for each of my domains within the /etc/apache2/sites-available/ folder. For example I have a file called “ubuntu-tutorials.com” in that directory, with the following contents:
<VirtualHost 67.207.131.28:80>
ServerName ubuntu-tutorials.com
ServerAlias www.ubuntu-tutorials.com
ServerAdmin christer.edwards@ubuntu.com
DocumentRoot /var/www/ubuntu-tutorials.com/html
</VirtualHost>
What these settings do is as follows:
- ServerName listens for requests asking for a certain domain
- ServerAlias defines any additional domains that should match
- ServerAdmin is the contact for the site
- DocumentRoot is the path to the content for that site
Now that this file is created in the /etc/apache2/sites-available/ folder we’re just about ready to start, but we need to enable it. We can do that by creating a symbolic link from one folder to the next.
cd /etc/apache2/sites-enabled/
ln -s ../sites-available/ubuntu-tutorials.com .
This site is now available (as in configured) and enabled (as in listening) once we restart the apache service:
sudo /etc/init.d/apache2 restart
Testing
To test your configuration you can, temporarily, configure your /etc/hosts file to point the domain to your IP address and see if your server loads up the correct site. This is only needed if the hostname or domain name does not already resolve to your IP address. Editing the /etc/hosts by adding the following line:
ip.address domain.tld
Open your browser, try to access domain.tld and see if it loads the contents from your local DocumentRoot (from the configuration above). You might want to drop a file in the DocumentRoot to verify its pulling your local content.
cd /var/www/ubuntu-tutorials.com/html
echo "Hello World" > index.html
Conclusion
I hope I didn’t miss anything here. One of the main purposes of this writeup is to document what I did to setup my server. I do it so rarely I don’t always remember all the steps when I need to. If this helps you setup name based virtual hosting, great. Leave a comment and let me know. If I forgot anything critical please also let me know so I can update the contents.
If this article has been helpful, please consider linking to it.
Looks good, just you can use a2ensite to avoid the symlink :
sudo a2ensite ubuntu-tutorials.com && sudo /etc/init.d/apache2 restart
will do the trick, more in the debi^w ubuntu way
To make sure every one (including search engines) only index the ‘short URL’ I use a rewrite rule simular to the one below for most of my sites. To keep things clean. You mave have to enable the mod_rewrite module first.
RewriteEngine On
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^ubuntu-tutorials\.com [NC]
RewriteRule (.*) http://ubuntu-tutorials.com/$1 [R=301,L]
What a good idea, I’ve got name-based virtual hosting set up but it took a couple of days of following instructions and I’m not sure I could do it again. The steps you have posted look correct to me, I’ll be using this tutorial next time I need to do this. Thanks
The first site that I create works just fine. Let’s say e.x foo.com. But when I create another one, bar.com. It directs me to the default-root (/var/www/) and not /var/www/bar.com/html
Why? I have added it into /etc/hosts and I also checked that the symlink and the /etc/apache2/sites-available/bar.com is correct.
@TomTom – you’ll want to make sure that your documentroot setting is different for foo and bar. also restart apache after you create any new hosts.
@Eelco:
I use this more generic RewriteRule to redirect requests for www.* which doesn’t require you to modify it for every site.
# Redirect all requests for a www host
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,QSA,L]
@post author (no attribution?)
If you merely want to add new sites you can also use force-reload which is faster.
sudo /etc/init.d/apache2 force-reload
Ack, WordPress isn’t displaying that rule correctly.
The RewriteRule should redirect to http://percent1/dollar1
You might consider using VirtualDocumentRoot; that way, you won’t have to set up every single site (but it has some downsides)
I have the same problem as tomtom. My second site still redirects to the first site. I have different document roots and I have restarted apache.
/etc/apache2/sites-available/mysway.org:
ServerName mysway.org
ServerAlias http://www.mysway.org
ServerAdmin lusths@cs.ua.edu
DocumentRoot /var/www/mysway.org/html
/etc/apache2/sites-available/rra.cs.ua.edu:
ServerName rra.cs.ua.edu
ServerAlias rra.cs.ua.edu
ServerAdmin lusths@cs.ua.edu
DocumentRoot /var/www/rra.cs.ua.edu/html
Found my problem. I had
ServerAlias rra.cs.ua.edu
It needs to be
ServerAlias http://rra.cs.ua.edu
This was very helpful I had looked every where to get this setup.
Kevin
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
Totally does not work. 403 after everything.
Going back to Gutsy will be the solution, that worked at least.
I was running into this and found that the 403 error is caused by having AllowOverride set to All. I changed it to None and it worked fine…except that I need it set to All…
Oh well, back to hunting.
Thanks for the informative article. I am trying to setup a web application where some of the pages require SSL. The NameVirtualHost directive is really confusing me. Do I put two of these; one for plain http and another for https?
Thanks.
-Raj
(BTW, your openid based posting seems broken)
I think my problem is even more fundamental than configuring apache.
I have a domain name of epc-inc.com.
We have a working website at http://www.epc-inc.com.
The files for this website are in our var/www/epc directory.
When I browse to http://www.epc-inc.com/ I go to the correct website.
Now i copy the epc website directory and files to a new one named tmr:
sudo cp -r /var/www/epc /var/www/tmr
Now i edit /var/www/tmr/index.htm and change it.
etc/apache2/epc looks as follows:
ServerName epc-inc.com
ServerAdmin admin@epc-inc.com
Redirect permanent / http://www.epc-inc.com/
ServerName http://www.epc-inc.com
ServerAdmin admin@epc-inc.com
#ErrorDocument 404 /error404.html
DocumentRoot /var/www/epc
Options -ALL
AllowOverride None
Order deny,allow
deny from all
AllowOverride None
Order allow,deny
allow from all
My wiki site looks as follows:
Now:
sudo cp /etc/apache2/epc /etc/apache2/tmr
Now modify /etc/apache2/tmr as follows:
ServerName tmr.epc-inc.com
ServerAdmin admin@epc-inc.com
ServerName tmr.epc-inc.com
ServerAdmin admin@epc-inc.com
#ErrorDocument 404 /error404.html
DocumentRoot /var/www/tmr
Options -ALL
AllowOverride None
Order deny,allow
deny from all
AllowOverride None
Order allow,deny
allow from all
Then i set go to my server and set a forward lookup zone new host(a) to associate tmr with my apache server 192.168.2.10
Then enable the site and try browsing to tmr.epc-inc.com with no luck.
I realize that there is something fundamental that I don’t understand.
Thanks for any advice
Tom
Thanks, dude, for the clear and working start. I wasn't sure if I should delete the default out-of-the-box enabled site once I'd set up my own virtual sites. I did, and everything is dandy.
I'm guessing there's some way to send mail.userdomain.ca to webmail virtual hosting for that user – I'll be figuring that out next.
It looks like you need to ensure that the virtualhost directive is configured the same as the NamedVirtualHost directive in ports.conf in 9.10.
ie. (in /etc/apache2/ports.conf) NamedVirtualHosts *
therefore, your directive must be in whatever site-enabled virtualhost file you make.
Well it appears that you must use “NamedVirtualHosts *” in ports.conf. I’m not sure why and haven’t found anything that says “*:80″ should not work, but changing this corrected the issue for me. However, that means your virtual hosts will be configured for all listened to ports. Hope this helps someone.
Thanks for the guide, i was able to leave my original site as is (the default) and add a new site which works fine without www. If I enter the http://www.newsite.tld it takes me to my original site. will i need a second sites-available entery for the www?
Add the www entry on the ‘ServerAlias’ line and it should catch that as well.
Nice post… My problem was solved about my ubunto linux programming.
Thanks, very good tutorial. My prolembs solved.
how do you test the .tld? it does not resolve.
Ok I need help: Here is what I am wanting to do:
I want to host three sites as follows:
1. http://www.wilmingtoncoc.com – Host as a WordPress site. Use Word press to put content on it.
2. http://www.mikealrhughes.com – Host by putting files up using some kind of ftp server by way of Dreamweaver.
3. http://www.biblematters.net – a Mailman mailing list of my closed mailing list.
Right now my http://www.wilmingtoncoc.com is forwarded to my public IP address. I don’t know if that is the way it needs to remain or not. Someone can tell me that. Also I want to install Webmin to control all of this. Especially if I want to add another site to the mix in the future. I am trying to do all of this with Ubuntu 10.04 any help by anyone appreciated.
Thanks,
Mike Hughes
Is there a limit to thee number of virtual host sites/files? I am trying to load 2 files in the sites-available dir, and if one works, the other doesn’t and vice versa.. any suggestions? I am on Karmic Kola Ubuntu 9.10 server ed.
it’s not work, did i make any mistake..
@ scull7
I wanted to set-up two (2) virtual hosts for development purposes on Ubuntu 10.04. Everything seemed to be configured correctly, but I was getting:
“[warn]
VirtualHost x.x.co.uk:80 overlaps with VirtualHost x.y.co.uk:80, the
first has precedence, perhaps you need a NameVirtualHost directive”
Only one of the sites would resolve to the correct document root.
I searched and searched, tried various fixes and nothing………
Then I amended my ports.conf file (in /etc/apache2/ports.conf) from:
NamedVirtualHosts *:80
to
NameVirtualHost localhost:80
I then reloaded the apache server: /etc/init.d/apache2 reload
hey presto! everything worked fine.
My GOD thank you for such a clear tutorial….the trouble with linux is that gurus rarely take the time to speak english.
Thank you again!
Where does this rule go? Sorry, newbie here.
@ beerfan