Setting up name-based virtual hosting on Ubuntu
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 [email protected] 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.
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://[percent]1/%5Bdollar%5D1
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 [email protected]
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 [email protected]
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 [email protected]
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 [email protected]
Redirect permanent / http://www.epc-inc.com/
ServerName http://www.epc-inc.com
ServerAdmin [email protected]
#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 [email protected]
ServerName tmr.epc-inc.com
ServerAdmin [email protected]
#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
i am a Greek man.
All the above are very usefull
BUT what happens with phpmyadmin;it works;
thank you
if i want to change the /var/www/………..
to /home/aslanidhsgevrgios/asla/…………how do this;
i want to have my files index.html,.php and others in this directory
and execute from here
how apache2 must know it;
Great article — however — I can’t get away from the start up test page – It Works! I am new to Apache and Ubuntu. Your article makes sense. Any suggestions? Thanks.
Hi & TNX for your article,
my problem is: I had configured ubuntu, apache, php5,… NEXT created DB’s and copied old /etc/apache2 and hosts; in terminal i wrote /etc/init.d/apache2 start and got this:
* Starting web server apache2
[Sat Dec 25 15:34:58 2010] [warn] NameVirtualHost *:0 has no VirtualHosts
[Sat Dec 25 15:34:58 2010] [warn] NameVirtualHost *:0 has no VirtualHosts
[Sat Dec 25 15:34:58 2010] [warn] NameVirtualHost *:80 has no VirtualHosts
Action ‘start’ failed.
The Apache error log may have more information.
[fail]
MY CONFIG:
file /etc/apache2/site-enabled/default is:
#NameVirtualHost *
ServerAdmin webmaster@localhost
ServerName profs
DocumentRoot /var/www/proffessor/
Options FollowSymLinks
AllowOverride None
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 off
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
NameVirtualHost *
ServerAdmin webmaster@localhost
ServerName education
DocumentRoot /var/www/education/
Options FollowSymLinks
AllowOverride None
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
NameVirtualHost *
ServerAdmin webmaster@localhost
ServerName student
DocumentRoot /var/www/student/
Options FollowSymLinks
AllowOverride None
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
/***FINISH***/
file httpd.conf is EMPTY.
file hosts is:
172.16.30.50 MYNMAE # Added by NetworkManager
127.0.0.1 localhost.localdomain localhost
::1 MYNAME localhost6.localdomain6 localhost6
TNX.
@ best
Hi all,
I discover my error.log and I found my soloution at:http://ubuntuforums.org/showthread.php?t=1639623.
I Hope this good for somebody.TNX.
Thanks, I had done this before but forgot to record the steps. You’re a life saver!
Hi,
We have a server which is running only on one domain.
How i can check this is ip based ?
and I want to run my zend framework site , so i think only making virtual host can help me so for making the virtual host on this server do i need a new domain ? because i dont have new domain 🙂
if there is any other way than how ?
Hi,
I am trying to configure apache namebased virtualhosting on my amazon ec2 instance but its public ip address is different from private ip address. Then how can we setup namebased virtual hosts in this situation?
Regds
Sushant Chawla
Hello,
I have followed this tutorial, and I am getting a mysterious error. Requests for http://www.mydomain.com work fine, but requests for http://mydomain.com do not work properly. In Firefox, they redirect to http://www.mydomain.com, and in Chrome they result in an “Unable to contact server” message from the browser.
What’s going on? I have created master DNS entries for all the websites I am hosting, but I have basically left the default settings Linode gives us alone.
Thanks in advance!
Joshua
thanks man! worked perfectly…
great tutorial. im a noob with all of this and i have followed several different tutorials. all have failed me.
im still having a problem though. i have 2 different domain names, example1.com and example2.com.
when i goto either, it takes me to the root of the WWW directory /var/www/
and from there i can click through the 2 different domains.
another thing im not sure about is in the /etc/apache2/apache2.conf
the part of “NameVirtualHost ip.address:port”
is that supposed to be the IP address of my web server, my router, my external WAN IP?
please let me know.
thanks in advance 🙂
ok… i made it past the WWW directory. i had a type-o in one of the domain files example1.com.
but now that i fixed it, when i goto example1.com or example2.com, they both load example1.com’s index.html file.
any idea as to why ?
Great tutorial! I’m a newbie to apache but manage to setup virtual host by following your tutorial. Can you teach me how to setup virtual host for SSL as well? Thanks!
@airborne
Have you seen the comment by John Lusth, sounds like you have the same issue which he fixed:
plz plz plz help me out……………… there was some problem like this—->
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, webmaster@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
Apache/2.2.17 (Ubuntu) Server at netorbis.com Port 80
———->>
———>> but when i delete the .htaccess file frome public folder then it takes the change url but not include index.php……..plz help as soon as
@Eelco
What file do i write this….sorry i’m a bit of a newb but i’ve got pretty far by myself.
heres another question too..is it possible to set all this up on a dynamic ip?
Great stuff!! Thanks a lot!
So I get to the part where I need to restart apache and it tells me:
apache2: syntax error on line 230 of /etc/apahce2/apahce2.conf: Could not open configuration file /etc/apache2/sites-enabled/test2: No such file or directory Action ‘configtest’ failed.
When I look in the sites-enabled folder it shows test2 as a link though. However, when I use terminal to look the 000-default file is in blue text but my test2 file is in red text with a dark gray background.
Any suggestions? Thanks.
This post is old but still very helpful. I’ve followed it to setup vhosts on our server and it worked like a charm thanks!
A couple of tips… On Ubuntu / Debian the NameVirtualHost *:80 is already defined in ports.conf. When you define the virtual hosts you should make sure that the *:80 matches exactly in the definitions and that you don’t define NameVirtualHost in a second place.
Trying to set up 2 virtual hosts. Through my registrar I have both FQDNs pointing to my static IP address. On my Ubuntu box I’ve set up http://www.auldsbel.org and http://www.rebuild-it.com in /etc/apache2/sites-available to look like:
DocumentRoot /var/www/www.auldsbel.org
ServerName http://www.auldsbel.org
ServerAlias auldsbel.org
and
DocumentRoot /var/www/www.rebuild-it.com
ServerName http://www.rebuild-it.com
ServerAlias rebuild-it.com
In /etc/hosts I have:
127.0.0.1 localhost
127.0.0.1 auldsbel.org
127.0.0.1 rebuild-it.com
The last line I also tried: 127.0.1.1 rebuild-it.com
What’s happening is that no matter which domain I go to they both seem to pick up the index.html in the /var/www/www.auldsbel.org/ directory. I’m not seeing the index.html I created in the /var/www/www.rebuild-it.com/ directory. I checked permissions and both paths are set to http://www.data.
Cheers.
Thanks for the great write up! Helped me get started easily on my switch from another distro to Ubuntu.
I think it may be worth noting the following two commands which are a bit easier to run than the symlink.
a2ensite example.com – enable example.com site
a2dissite example.com – disable example.com site
thanks didn’t even have to finish ready the tut you got me on track. Simple to the point. To easy.
Thanks for this! I know its a few years old but after trying to follow a few other tutorials, this was the clearest and most concise.
Hi,
I have created a video and guide for how to do this if you this helps anyone:
http://www.youtube.com/watch?v=QaZBly7_0zo
http://wilson18.com/how-to/linux-networking/how-to-host-multiple-sites-on-1-web-server-using-ubuntu-server-12-04/
How do I set a subdomain up ?
I followed the same steps for the subdomain but it does not work
Thank you 🙂
I folled the turorial but when hitting my website url it shows the files for the DocumentRoot rather than the running application. Any ideas for such a behaviour? thanks