It’s easy (in debian 6.0 at least) to have more than one instance of apache2 running because the management scripts check whether they were invoked as “somethingapache2” or as “somethingapache2-something”. In the notes below I’m using ‘b’ as the suffix, so my paths will end with ‘apache2-b’.

[Before launching into this, check that your apache start/stop script /etc/init.d/apache2 includes the line

DIR_SUFFIX="-${0##*/apache2-}"

somewhere near the top. (In v2.2.15 it’s at line 15.) If not, these notes will be pretty much no help to you, and you should consider upgrading to a version that has it.]

First identify what needs to be copied or linked from your original instance of apache2, so you need a list of what paths are included in your current instance. Generally I use such package info a lot, so on debian I do (as root):

cd / && ln -s -nf var/lib/dpkg/info dinfo

This means that the path info for package <pkg> is available at /dinfo/<pkg>.list

Paths to be copied

Once I’ve checked the output of the following is sensible, I change the ‘echo‘ to ‘cp -a‘.

#!/bin/bash
DIR_SUFFIX="b"
for p in $(grep apache2$ /dinfo/apache2.2-common.list \
| grep -Fv share \
| grep -Fv init.d \
| grep -Fv lib); do
    echo $p "${p}-$DIR_SUFFIX"
done

On my current setup, that results in the following paths being duplicated with the suffix.

  1. /etc/apache2
  2. /etc/cron.daily/apache2
  3. /etc/default/apache2
  4. /etc/logrotate.d/apache2
  5. /var/cache/apache2
  6. /var/log/apache2


(The dir /var/run/apache2-b will be created automatically.) If you know you’ll never use apache cache, you can skip 3 & 5. Scripts in 1—4 then need to be edited to change ‘apache2’ to ‘apache2-b’. I prefer to do this in vim using ‘:%s/apache2/apache2-b/gc‘ to step through each edit.
Then check that no ports/sites clash with the original.

Paths to be linked

Once I’ve checked the output of the following is sensible, I change the ‘echo‘ to ‘ln -s -nf‘.

#!/bin/bash
DIR_SUFFIX="b"
for p in $(grep -E "sbin/|init.d/" /dinfo/apache2.2-common.list \
| grep -F 2); do
    d=${p%/*}
    f=${p##*/}
    if [ $p != "$d/$f" ]; then
        echo "Paths got mangled ($d) ($f)" >&2
        exit 1
    fi
    (cd $d && echo $f "${f}-$DIR_SUFFIX")
done

On my current setup, that results in the following paths being linked with the suffix.

  1. /etc/init.d/apache2
  2. /usr/sbin/a2dismod
  3. /usr/sbin/a2dissite
  4. /usr/sbin/a2enmod
  5. /usr/sbin/a2ensite
  6. /usr/sbin/apache2ctl

If you want the new instance to run as a different user, just edit /etc/apache2-b/envvars.
Then invoke /etc/init.d/apache2-b start
and ps should then show that /usr/sbin/apache2 -d /etc/apache2-b -k start is running.

More paths to be linked

The above all works a treat… until you upgrade the ‘main’ instance of apache and your sibling instances are left out in the cold, possibly broken. Until some handy scripts turn up, the answer is to copy less and link more.

#!/bin/bash
DIR_SUFFIX="b"
for d in conf.d mods-available; do
    (cd /etc/apache2/$d \
        && find * -maxdepth 0 -type f) \
    | \
    (cd /etc/apache2-$DIR_SUFFIX/$d \
        && while read f; do
            ln -s -nf ../../apache2/$d/$f
        done)
done

cd /etc/apache2-$DIR_SUFFIX
# Can add envvars to following list if running as same user
for f in apache2.conf httpd.conf; do
    ln -s -nf ../apache2/$f
done

Custom changes to new instance

So now you have the flexibility to run the new instance as a separate user, to use a separate perl binary, to use the same perl binary but with a completely different modperl environment, and so on. It’s advantageous to keep apache2.conf a symbolic link and never edit it. If you want to have custom settings, eg the number of child processes, just add/edit a file under conf.d.
/etc/apache2-b/conf.d/threads:

<IfModule mpm_prefork_module>
    StartServers 2
    MinSpareServers 2
    MaxSpareServers 4
    MaxClients 150
    MaxRequestsPerChild 0
<IfModule>

So a huge thank-you to the apache folk for removing what was a big headache.