Running 2 apache instances on the same machine
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
Comments
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.
My script create new apache instance on same machine. Test on SLES11SP-x64
i tried your script envy0 and it seems to have made all the right adjustments to create a new instance of Apache, however i’m still getting a bunch of errors. Below is my output when i try starting apache2a
Church:~ # rcapache2
rcapache2 rcapache2a
Church:~ # rcapache2
rcapache2 rcapache2a
Church:~ # rcapache2a status
Checking for apache2a: unused
Church:~ # rcapache2a start
Module “actions” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “alias” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “auth_basic” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “authn_file” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “authz_host” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “authz_groupfile” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “authz_default” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “authz_user” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “authn_dbm” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “autoindex” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “cgid” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “dir” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “env” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “expires” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “include” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “log_config” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “mime” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “negotiation” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “setenvif” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “ssl” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “suexec” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “userdir” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “proxy” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “proxy_ajp” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Module “rewrite” is not installed, ignoring.
Check the APACHE_MODULES setting in /etc/sysconfig/apache2a.
Starting HTTP instance apache2a (worker) Syntax error on line 102 of /etc/apache2a/server-tuning.conf:
Invalid command ‘BrowserMatch’, perhaps misspelled or defined by a module not included in the server configuration
The command line was:
/usr/sbin/httpd2-worker -f /etc/apache2a/httpd.conf -DSSL
failed