Creating an Apache virtual host manually

From Metro Studios Knowledgebase

Jump to: navigation, search


[edit] Abstract

This is based loosely off of the Slicehost virtual host setup that can be found here: Slicehost Apache Virtual Hosts on Ubuntu - part 1, and here: Slicehost Apache Virtual Hosts on Ubuntu - part 2. Also it probably wouldn't hurt to do some basic reading up on the core principles of virtual hosting: Wikipedia - Virtual Hosting.

We use this specific setup on our Slicehost servers as it gives us the utmost control over what is happening with the domain and lets us better configure Apache and PHP in a segregated environment than we would be able to on our traditional Rackspace servers.

You will need sudo or root access on the machine in order to make these changes.

[edit] Steps

[edit] Step 1

We will be working within the /var/www/vhosts/ directory on the server. This is where all of the virtual host directories reside for all physical web hosting on the server.

You need to copy over a template domain (basically a template of files) that will act as our base for the site. This directory can be found on dev1 and is named "template-domain". It contains httpdocs (the location of the public website), logs, and private (where you store information tied to this domain that you need to retain on server such as database backups).

First we're going to need to copy the template over to the end domain name we will be hosting our site at (host):

sudo cp template-domain -R -p

As you can see we need to recurse through the subdirectories by feeding cp the -R option, and we need to preserve permissions using -p to keep the group writable bit.

NOTE: Not all of our servers are set up using a technology called ACL. This is something we will need to update eventually for all servers so we all can update/access the site using our personal user accounts, eg: jmparks. It is setup this way so that anyone from the "developers" group should have access to edit the site. dev1 however is solid on it's ACL... I think. To ensure the ACL copied over correctly, run a getfacl. Using ACLs with Fedora Core 2 (Linux Kernel 2.6.5) is my favorite documentation on ACL and shows a good amount of base concept.

[edit] Step 2

Next we will be working within the /etc/apache2/sites-available/ directory. This directory holds individual Apache2 configuration files for every one of our virtual hosts. You can find a sample Metro Studios virtual host here: Sample virtual host configuration.

You will need to do is copy the virtual host configuration file to a new file that is the host name of the site (ex:

sudo cp

Open up the new configuration file and then do a find and replace replacing the old host name with the new one. Save that file's configuration (be absolutely sure it is correct and didn't have permission issues) and double check it. (I always use cat)

[edit] Step 3

The final step is to enable the site in Apache and then to reload the web server to finish setting up the hosting.

From within the /etc/apache2/sites-available/ issue the following command in order to enable the site (using as an example):

sudo a2ensite

This will load the virtual host configuration into Apache. At this point, all you should need to do is reload the web server by issuing:

/etc/init.d/apache2 reload

Personal tools
Wiki Navigation