Home > Apache, Linux > Virtual Web Site Hosting

Virtual Web Site Hosting

November 4th, 2007 Leave a comment Go to comments

A common practice is to use one apache server to host websites for multiple domain names. Apache supports virtual servers out of the box making this easy to setup. This tutorial is based on Fedora Core 5, but should apply to most other linux distros.

To get started you need to have a folder to store the data for each virtual website. On my system I have a created a new user for each website following the pattern.


For example to add jsmith. Note that the /home/jsmith folder must have execute permissions and the public_html folder must have read permissions for apache.

useradd jsmith
cd /home/jsmith
mkdir public_html
chown jsmith:jsmith public_html
chmod 711 .
chmod 755 public_html

Lets open up the apache config and add the required sections.

vim /etc/httpd/conf/httpd.conf

Scroll down to the end of the file and you will see the virtual hosting section. The first thing we must do is let apahce know what IPs and ports it will be serving for virtual hosts. The * wild card is supported. The line below states that apache will server requests for port 80 on all incoming IPs.

NameVirtualHost *:80

Now we set the individual settings for each virtual host. If you are a hosting provider you probably will want a catchall host to display if the user goes to the server’s IP or hostname. To do this we use the _default_ attribute.

<VirtualHost _default_:80>
   ServerAdmin email@domain.com
   DocumentRoot /var/www/html
   ServerName www.domain.com

On the rest of your virtual hosts I recommend to fill in a ServerAlias. Lets say your site is http://www.jsmith.com/. Without a ServerAlias if a visitor points his/her browser to http://jsmith.com/ they will get the default host instead of http://www.jsmith.com/.

<VirtualHost *:80>
   ServerAdmin email@jsmith.com
   DocumentRoot /home/jsmith/public_html
   ServerName www.jsmith.com
   ServerAlias jsmith.com *.jsmith.com

Make sure to verify the apache config first

apachectl configtest

If it comes back syntax OK you are ready to reload the config

service httpd reload

or start up the apache server if it is not already running.

service httpd start
Categories: Apache, Linux Tags:
  1. December 7th, 2007 at 13:30 | #1

    I couldn’t understand some parts of this article al Web Site Hosting, but I guess I just need to check some more resources regarding this, because it sounds interesting.

  1. No trackbacks yet.