Multiple Apache Instances in Ubuntu
Saturday, 22 March 2008 00:00

I have to start this post by saying that I am by no means a Linux expert. However, I recently decided to move my web application hosting off of a Windows Server and onto a Linux (Ubuntu) Server. One portion of my old Windows setup shared the Apache Web Server binary files to run two separate Apache servers. I have it setup this way so that I can start/stop each server without taking the other down, and to ensure that if one crashes it doesn't affect the other.

It took me some time to figure out how to do this in Ubuntu and I couldn't find any good tutorials, but it is possible and seems to be running fairly well. There's always more than one way to skin a cat, but below I will outline the steps I took to get this running. If any Linux experts have better suggestions I would more than welcome any comments!

Directory Setup

First, make sure you have Apache2 installed:

sudo apt-get install apache2

You will notice that this will install Apache for you and the configuration files will be placed under /etc/apache2. You will need to copy this directory to your own location, such as /customer/server1. Be sure to use the -r option to recursively copy it. You'll need to do this once for each instance you want to run.

Before making this copy, I had noticed is that one of the symbolic links used an absolute path. If you're not too familiar with symbolic links, this means that after making a copy, one file would be pointing back to the original configuration files. This isn't what we want, so we first need to tweak the symbolic link before copying the directory structure:

sudo rm /etc/apache2/sites-enabled/000-default
sudo ln -s ../sites-available/default /etc/apache2/sites-enabled/000-default
sudo cp -r /etc/apache2 /custom/server1
sudo cp -r /etc/apache2 /custom/server2

Configure Apache

Now that this is done, you can go into each of your custom directories and configure Apache however you'd like. Configuring Apache is beyond the scope of this tutorial, but there are a few important notes:

  • The two different instances must be listening on different ports or IP addresses (try editing the ports.conf file)
  • Browse through all the different configuration files and make sure that you change any references from /etc/apache2 to /custom/server1 (or your own location)
  • In the apache2.conf file, make sure that each instances uses its own lock and pid files. I changed two lines for each instance:

 

LockFile /var/lock/apache2/accept-server1.lock
PidFile /var/run/apache2-server1.pid

Starting/Stopping Apache

You can start/stop the different Apache instances by passing in an argument that specifies the configuration file to use. To start a particular Apache instance you can do this:

sudo apache2 -f /custom/server1/apache2.conf -k start

After starting each instance of Apache, you can attempt to navigate to the page in your favorite web browser, and it should be working at this point! To stop them, the command is similar:

sudo apache2 -f /custom/server1/apache2.conf -k stop

Configuring Apache to Start on Bootup

In Linux, it seems there are many different ways to get things running on startup. The best I can do is outline how I have it setup and hope that it may work the same for you.

First, I needed to create my own start/stop script. I named it apache2-server1 and placed it inside the /etc/init.d/ directory. You'll need to create a similar script for other instances as well:

#!/bin/bash
case $1 in
start)
sudo apache2 -f /custom/server1/apache2.conf -k start
;;
stop)
sudo apache2 -f /custom/server1/apache2.conf -k stop
;;
esac
exit 0

Then, you need to install this script for startup using this command:

sudo update-rc.d apache2-server1 defaults

Conlusion

That should be all there is to it! Hopefully these directions work for your particular distribution, or hopefully you're knowledgable enough with Linux to adjust it for your needs.