This site is now 100% read-only, and retired.

Hosting multiple sites with Apache

Posted by Steve on Thu 30 Sep 2004 at 18:50

Apache is probably the most popular webserver for the Linux platform, and despite being very powerful and extensible it is very well documented. In spite of this documentation many people seem to struggle with hosting multiple sites with Apache.

There are two ways to host multiple sites with one Apache instance, and both are very simple to setup. You have the choice of using Name based virtual hosts, or IP based virtual hosts.

In most common situations you will use Name based virtual hosts, this only requires that all the sites you wish to host point to the IP address of your Apache server.

To start with you'll need to install the apache server itself. On a Debian system you will install software via the apt-get system, so as root you need to run the following commands apt-get update and apt-get install apache.

Once this has been done you will find you have Apache installed, and it's default configuration is included inside the directory /etc/apache, by default this will be setup to serve the files that are contained in the directory /var/www.

To enable multiple host support you must add, or uncomment, the following line:


NameVirtualHost *

This sets up Apache to accept the hosts.

Once this is done you need to create the directories to contain your sites, personally I use /home/www/name.of.site.org.

Assuming that you wish to host two sites you should create two directories:


root@skx:# mkdir -p /home/www/www.foo.com
root@skx:# mkdir -p /home/www/www.foo.com/htdocs
root@skx:# mkdir -p /home/www/www.foo.com/logs
root@skx:# mkdir -p /home/www/www.foo.com/cgi-bin
root@skx:# mkdir -p /home/www/www.bar.com/
root@skx:# mkdir -p /home/www/www.bar.com/htdocs
root@skx:# mkdir -p /home/www/www.bar.com/logs
root@skx:# mkdir -p /home/www/www.bar.com/cgi-bin

This gives you the directories to place your content inside htdocs, a directory for the CGI scripts, if you need them, and a directory to contain the logfiles for that host.

The next step is to add the configuration for each site, this can be done by adding the following settings to the default configuration file, /etc/apache/httpd.conf:


<VirtualHost *>
        # Basic setup
        ServerAdmin webmaster@foo.com
        ServerName www.foo.com
        DocumentRoot /home/www/www.foo.com/htdocs/


        # HTML documents, with indexing.
        <Directory />
        Options +Includes
        </Directory>

        # CGI Handling
        ScriptAlias /cgi-bin/ /home/www/www.foo.com/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>

        # Logfiles
        ErrorLog  /home/www/www.foo.com/logs/error.log
        CustomLog /home/www/www.foo.com/logs/access.log combined
</VirtualHost>

<VirtualHost *>
        # Basic setup
        ServerAdmin webmaster@bar.com
        ServerName www.bar.com

        DocumentRoot /home/www/www.bar.com/htdocs/

        # HTML documents, with indexing.
        <Directory />
        Options +Includes
        </Directory>

        # CGI Handling
        ScriptAlias /cgi-bin/ /home/www/www.bar.com/cgi-bin/
        <Location /cgi-bin>
                Options +ExecCGI
        </Location>

        # Logfiles
        ErrorLog  /home/www/www.bar.com/logs/error.log
        CustomLog /home/www/www.bar.com/logs/access.log combined
</VirtualHost>

This sets up two sites, which have their docements and logfiles contained beneath /home/www/www.nameofsite.com. You can adjust the paths if you wish to keep the files elsewhere.

Before attempting to restart the server you should run a quick test to make sure that the configuration file /etc/apache/httpd.conf doesn't contain any errors. To do that you can run:


root@skx:/etc/apache# apachectl  configtest
Syntax OK
root@skx:/etc/apache#

Any errors should be highlighted, but in this case we see that our syntax is OK, so we can restart apache with:


root@skx:/etc/apache# /etc/init.d/apache reload
Reloading apache configuration.
root@skx:/etc/apache#

 

 


Re: Hosting multiple sites with Apache
Posted by Anonymous (127.0.xx.xx) on Fri 22 Oct 2004 at 14:07

Very good !!

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (127.0.xx.xx) on Fri 5 Nov 2004 at 10:46
This is a very good article, since the huge amount of available information makes it very easy for a a starter to become disoriented fast.
I followed your indications, all went smoothly, but I can't get it to work. I am redirecting from my registar to my machine (my registar is gandi.net), since my ip fluctuates with the wind (cable they say can't provide static ip). After doing all the steps, I tried from gandi.net to redirect o my.temporary.ip but the system they have to check it said that it was not resolving.
So far what I am doing is keeping the whole website in /var/www/domainname/, and by redirecting to my.temp.ip/var/www/domainname/ it works.
My whole intention is to be able to eliminate the ip from the location bar in the browser for visitors...
I also need the secure server, so I am reading your advice on it too.
Thank you for your help.

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (75.28.xx.xx) on Mon 18 Jun 2007 at 03:02
Regarding your IP problem. Checkout dyndns.com. I've used them for years. A small client monitors your router for the address change and sends an update to them. DNS upates in seconds.

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (24.129.xx.xx) on Mon 21 Jan 2008 at 05:02
I could easily be wrong about this, but you might want to double-check your DocumentRoot and/or server-root. It *sounds* (err, *reads*) like you're serving out your ROOT directory instead of your /var/www. This is why you need "http://ip.add.re.ss/var/www/domainname"; to get to your site. I wonder, if you type in "http://ip.add.re.ss/etc/fstab"; what would be displayed.

Also, as there are many flavours of Apache, be sure to *THOROUGHLY* check through the directories... I have, for instance, /etc/apache2/sites-available and /etc/apache2/sites-enabled that each have subfolders and more configgy stuffs in them. I believe these get checked last and anything in them takes precedence over other files. Again, I could easily be wrong.


I'm currently (re)setting up my web server. I was wanting an easy way to allow my wife to have a "testbed" development area on the server so she could play with code via SSH or FTP or whatever and see it live without screwing with the production server. Unfortunately, I don't have enough capital to provide identical production and development environments on separate hosts, so I have them both on one. I started out looking for a way to inject some kind of "document-root" variable into the web code so she could, for example, #include a "vars" html file, set her links to e.g. "virtual=$docroot/pages/header.html". Then, edit $docroot in the special file when it goes live so everything still works. After a few hours with Google, I still was nowhere, and CGI, SSI, PHP were no help. I believe this article may save this day...

As it is right now, I started playing with virtual servers in Webmin and now nothing works. I'm hoping the sample httpd.conf files here will help me get it back up.

At the moment, I get the following. I can't set up exactly like the examples here because I don't know enough about Apache to trust straying from the way they currently have it set up (see above) so I put the info in the appropriate conf file that Apache2 is using -- and yet, there seems to be a bug.


{YOU ARE ROOT}
[Serenity][pts/0]
[root][/home/cj]# apache2ctl configtest
[Sun Jan 20 23:43:20 2008] [warn] NameVirtualHost *:0 has no VirtualHosts
Syntax OK

{YOU ARE ROOT}
[Serenity][pts/0]
[root][/home/cj]#


Still gonna try it out...

Cheers


CJ

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by yaarg (129.215.xx.xx) on Tue 28 Jun 2005 at 14:56
[ View Weblogs ]
Hrm nice setup. I've tried to setup virtual hosting myself but I'm still having trouble. Main problem is I can't seem to use "wildcards" to accept something like *.domain.com. So if soembody types in somethingrandom.domain.com it will simply go to the first virtual host if that makes sense?! :/

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Steve (82.41.xx.xx) on Tue 28 Jun 2005 at 14:58
[ View Weblogs ]

I think it should do that anyway - But you could use Apache's notion of a default vhost if not.

The apache docs cover it..

Steve
-- Steve.org.uk

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by yaarg (129.215.xx.xx) on Tue 28 Jun 2005 at 15:01
[ View Weblogs ]
Hmm. I'm on it!

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Steve (82.41.xx.xx) on Wed 29 Jun 2005 at 00:58
[ View Weblogs ]

If you didn't already find it here's an example.

Steve
-- Steve.org.uk

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (83.226.xx.xx) on Thu 7 Jul 2005 at 19:49
thanks bro...
very helpfull...

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (212.61.xx.xx) on Fri 22 Jul 2005 at 12:40
and what about apache ssl hosting with virtual hosts.

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Steve (82.41.xx.xx) on Fri 22 Jul 2005 at 12:43
[ View Weblogs ]

No different than non-SSL.

Although there's the limitation of using only one unique certificate perl IP address ...

Steve
-- Steve.org.uk

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (24.22.xx.xx) on Tue 30 Aug 2005 at 03:04
:( Sadley when I tried to do this I got this error:

Syntax error on line 18 of /etc/apache/httpd.conf:
Invalid command 'ScriptAlias', perhaps mis-spelled or defined by a module not included in the server configuration

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Steve (82.41.xx.xx) on Tue 30 Aug 2005 at 03:11
[ View Weblogs ]

ScriptAlias is defined in the mod_alias module.

For Apache 1.3.x add the following to your /etc/apache/httpd.conf file:

LoadModule cgi_module /usr/lib/apache/1.3/mod_cgi.so 
LoadModule alias_module /usr/lib/apache/1.3/mod_alias.so 

Or just comment out the line included in the error message if you don't wish to use CGI scripts!

Steve
-- Steve.org.uk

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by trakic (85.81.xx.xx) on Thu 8 Sep 2005 at 09:45
[ View Weblogs ]
All this httpd.conf changes can be reduced with mass vhost hosting package WITHOUT restarting apache:

$ sudo apt-get install libapache-mod-dynvhost

Enable this module with (in this case I run apache-perl) ymmv:

$ sudo /usr/sbin/apache-modconf apache-perl enable mod_dynvhost

See some additional explanations for this package:
$ dpkg -L libapache-mod-dynvhost

and apache manual:
$ lynx http://localhost/manual/vhosts

/greeZ admir

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Steve (82.41.xx.xx) on Thu 8 Sep 2005 at 09:52
[ View Weblogs ]

True there are some modules which allow you to do this without restarting/reloading your Apache setup.

However the package you mention libapache-mod-dynvhost doesn't actually appear to be contained in any of the Debian releases; stable, unstable, or testing.

The main advantage of doing things the way they are presented here, instead of using a mass-virtual-hosting module, is that you can customize each site. For example setting up site-specific Aliases, or enabling/disabling PHP on a site-by-site basis. None of those things are possible in most of the virtual hosting modules.

If you're adding hosts sufficiently often that reloading Apache causes problems then you're probably better off using another approach, such as mod_vhost, or similar.

Steve
--

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Rustyfox (82.38.xx.xx) on Thu 15 Sep 2005 at 19:15
There's a potential Really Bad Problem in this example - storing the logs under a home directory.

If someone were to use this method to provide system users access to sites, and the logs directory was owned by that user, then it's possible for that user to remove the log file (due to inherited permissions from the logs dir ownership) and create a symlink to any other file. Apache will merrily then write over that file as root. Oops.

Make sure the logs dir isn't owned by any user with the ability to control it (and the directory is never empty so it can't be removed and recreated, same problem), or better store logs somewhere else entirely - for example /var/log/apache/sites/

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (81.130.xx.xx) on Sun 18 Sep 2005 at 09:38
Hi,
I'm running apache2 on debian sarge and have following written to my sites-available files:


# Basic setup
ServerAdmin compass@posh-promdresses.co.uk
ServerName www.posh-promdresses.co.uk
DocumentRoot /home/compass/posh-promdresses.co.uk/htdocs/

# HTML documents, with indexing.

Options +Includes


# Logfiles
ErrorLog /home/compass/posh-promdresses.co.uk/logs/error.log
CustomLog /home/compass/posh-promdresses.co.uk/logs/access.log combined


I'd like my sites to display when the www is omitted from the domain address eg posh-promdresses.co.uk rather than www.posh-promdresses.co.uk

How do I enable the above???

Pete

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Rustyfox (82.38.xx.xx) on Sun 18 Sep 2005 at 11:05
Add

ServerAlias posh-promdresses.co.uk

And make sure posh-promdresses.co.uk is set up in DNS the same as www.

And as I mentiond above, be carefull about having the logs under the home directory if it's for some user other than yourself - or just avoid it altogether for good practice.

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (81.130.xx.xx) on Sun 18 Sep 2005 at 18:02
Thanks for your input RustyFox. I'd failed to setup the A Record within the dns properly. All's well now though:

http://rustyfox.posh-promdresses.co.uk/

http://posh-promdresses.co.uk/

Thanks for your input!!!

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (82.119.xx.xx) on Fri 21 Oct 2005 at 11:12
Good job Steve !

Quick, easy and clear.

tnx a lot Max

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by liorda (132.69.xx.xx) on Wed 11 Oct 2006 at 11:43
There's a problem now: the new log files are no longer beeing rotated.

You can easily solve it by creating a new file in /etc/logrotate.d/ and entering the new logs' directories.

See http://www.debian-administration.org/articles/117 or `man logrotate` for more details.

--Lior

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (83.160.xx.xx) on Mon 17 Sep 2007 at 21:17
Thanks! I actually used your article to setup Apache for my XP Windows machine, so I can use multiple sites. And it works! Great.

Marcel

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (24.161.xx.xx) on Sat 17 Nov 2007 at 22:43
I successfully set up two websites "mysite1" and "othersite1" on single apache. but if I want to test them locally it only opens mysite1 but there is no way to access the "othersite1" locally.
if i do http://192.168.1.5 its opens mysite1, what should I change in apache config so that i can do http://192.168.1.5/mysite1 and http://192.168.1.5/othersite1 to open both site independently

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (82.32.xx.xx) on Sat 17 Nov 2007 at 23:37
Simplest thing to do is update your /etc/hosts file:
192.168.1.5 mysite1 othersite1
Then it'll work ..

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (220.227.xx.xx) on Tue 12 Feb 2008 at 09:35
Hi i dont have a DNS server but want to host two websites on a single apache server. Seems to be the same as mentioned in the topic. But i want it to be accessible over the LAN where no DNS server is not mentioned or provided.
i have made entries in the host file also..... Could anyone please tell me what the problem is???

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (213.186.xx.xx) on Sat 31 Jan 2009 at 15:31

[ Parent ]

Re: Hosting multiple sites with Apache
Posted by Anonymous (217.116.xx.xx) on Mon 4 May 2009 at 04:38
Check of enable vhost_alias.load in /etc/apache2/mods-enabled/

[ Parent ]