Jan
9
Setting Up Name Based Virtual Hosting
Filed Under Web Development
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 site has been useful, please consider participating in the Fundraiser.
Other Points of Interest
Comments
15 Responses to “Setting Up Name Based Virtual Hosting”
Leave a Comment
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 http://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)