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#
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 | Reply to this comment ]
[ Parent | Reply to this comment ]
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 | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | 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 | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | View Weblogs ]
If you didn't already find it here's an example.
Steve
-- Steve.org.uk
[ Parent | Reply to this comment ]
very helpfull...
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | 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 | Reply to this comment ]
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 | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | 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 | Reply to this comment ]
$ 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 | Reply to this comment ]
[ Send Message | View Steve's Scratchpad | 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 | Reply to this comment ]
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 | Reply to this comment ]
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 | Reply to this comment ]
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 | Reply to this comment ]
http://rustyfox.posh-promdresses.co.uk/
http://posh-promdresses.co.uk/
Thanks for your input!!!
[ Parent | Reply to this comment ]
Quick, easy and clear.
tnx a lot Max
[ Parent | Reply to this comment ]
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 | Reply to this comment ]
Marcel
[ Parent | Reply to this comment ]
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 | Reply to this comment ]
192.168.1.5 mysite1 othersite1Then it'll work ..
[ Parent | Reply to this comment ]
i have made entries in the host file also..... Could anyone please tell me what the problem is???
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]
[ Parent | Reply to this comment ]