Blog Entry

Running 2 apache instances on the same machine

Author Info

20 May 2008 - 5:33am
Submitted by: leraly

blog
Reads:

5602

Score:
2
2
1
 
Comments:

1

Suppose you need to run 2 or more apache instances on 1 machine (for whatever reason)

Here is how you can do this in a way without the need to modify init scripts, compile a second instance, do some dirty hacks, ...

The SuSE apache package and scripts are already ready to achieve this.

In this example I'm going to run 2 instances (each on their own ip address)

If you install the apache2 package it will (by default) put all its configuration files under /etc/apache2. The apache is started via the startup script (/etc/init.d/apache2 - rcapache2).

The default configuration makes apache listen on all ip addresses, since we want to run a second instance of apache on a separate address we need to limit the first instance to its own address. This can be done by modifying the /etc/apache2/listen.conf file and specify on which address apache should listen. (e.g listen on ip 1.1.1.1)

Listen 1.1.1.1:80

After you made a change to this configuration file you need to restart apache. Verify if apache is now only listening on the specified ip.

# lsof -i tcp:80
COMMAND PID USER FD TYPE DEVICE SIZE NODE NAME
httpd2-pr 6010 root 3u IPv4 18189 TCP 1.1.1.1:http (LISTEN)

Now we can continue with the configuration of our second instance.
Create a separate configuration directory for this instance

# cp -pr /etc/apache2 /etc/myapache2
# cp -p /etc/sysconfig/apache2 /etc/sysconfig/myapache2

Modify the necessary values like paths,etc... in the configuration files in /etc/myapache2/{httpd,default-server}.conf and the /etc/sysconfig/myapache2 file.

Don't forget to modify /etc/myapache2/listen.conf, where you need to specify the ip address (on which this instance should listen)

Listen 2.2.2.2:80

In the /etc/myapache2/default-server.conf add the following line (at the top)

PidFile /var/run/myapache2.pid

Otherwise the both apache instances will use the same pid file.

As a final step you now need to create a small wrapper script (around rcapache2) where you point the following variables to the directories of the 2nd instance

#!/bin/bash

export sysconfdir=/etc/myapache2
export sysconfig_apache=/etc/sysconfig/myapache2
export pidfile=/var/run/myapache2.pid
export logdir=/var/log/myapache2
export homedir=/var/lib/myapache2

rcapache2 $1

(offcourse this script needs some error handling)
Don't forget to make the script executable

That's it.

Now you can start both instances

rcapache2 start
/path/to/wrapper-script start


Author Info

20 May 2008 - 5:33am
Submitted by: leraly




User Comments

a little somethind to add

Submitted by livbest on 2 July 2009 - 9:23am.

Thank you for a very interesting article, it was the path opener for a solution I was searching. However, it is not complete. What I have to add to the solution:
- when you copy the whole /etc/apache2 to the new directory, some references in the *.conf files still remain to the hard-linked /etc/apache2. Specifically : in httpd.conf and default-server.conf replace all entries /etc/apache2 to /etc/"apache2_new" . Also, be sure to check any references inside virtual hosts, if you have any (it was my case).
- next, I personally didn't make any changes to /etc/sysconfig/"apache2_new", as I didn't need to, but you might.
- final step, you don't have to create a new "rcapache2", it is enough to copy /etc/init.d/apache2 to /etc/init.d/"apache2_new" and make effective the following changes inside:
* LEAVE $pname with its default value "apache2"
* edit sysconfdir:=/etc/"apache2_new"
* edit sysconfig_apache:=/etc/sysconfig/"apache2_new"
* edit pidfile:=/var/run/"httpd_new.pid"
That's about it. You can start the new service with /etc/init.d/"apache_new" start, and enjoy all the advantages of the old script. I hope I haven't leave anything out. If you get any errors, be sure to dig inside the .conf files inside the new /etc/"apache2_new" directory for the correct references to the new directory.
Thank you again for the great article, it saved me a lot of time. With this resource and some digging, I was able to set an active/active loadbalancing solution with apache2 and heartbeat on SLES10.

© 2009 Novell, Inc. All Rights Reserved.