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

(Visited 1 times, 1 visits today)
Tags:
Category: SUSE Linux Enterprise Server, Technical Solutions
This entry was posted Tuesday, 20 May, 2008 at 6:33 am
You can follow any responses to this entry via RSS.

Comments

  • livbest says:

    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.

  • envy0 says:

    My script create new apache instance on same machine. Test on SLES11SP-x64

    #!/bin/bash
    pname=apache2a
    
    ####################
    # clone base files #
    ####################
    cp -p /etc/init.d/apache2 /etc/init.d/$pname
    chmod 744 /etc/init.d/$pname
    ln -s /etc/init.d/$pname /usr/sbin/rc$pname
    cp -p -R /usr/share/apache2 /usr/share/$pname
    ln -s apache2 /var/lib/$pname
    ln -s apache2 /usr/lib64/$pname
    ln -s apache2-event /usr/lib64/$pname-event
    ln -s apache2-prefork /usr/lib64/$pname-prefork
    ln -s apache2-worker /usr/lib64/$pname-worker
    cp -p -R /etc/apache2 /etc/$pname
    cp -p /etc/sysconfig/apache2 /etc/sysconfig/$pname
    mkdir /var/log/$pname
    chmod 750 /var/log/$pname
    
    
    #################
    # change $pname #
    #################
    filenames=(
    /etc/init.d/$pname
    /usr/share/$pname/get_includes
    /usr/share/$pname/get_module_list
    )
    for filename in ${filenames[@]}
    do
      sed -i "s/pname=apache2/pname=$pname/" $filename
    done
    
    #####################################
    # replace "/apache2" with "/$pname" #
    #####################################
    filenames=(
    #/usr/share/$pname/find_httpd2_includes
    #/usr/share/$pname/find_mpm
    #/usr/share/$pname/load_configuration
    #/usr/share/$pname/sysconf_addword
    /etc/sysconfig/$pname
    /etc/$pname/httpd.conf
    /etc/$pname/default-server.conf
    /etc/$pname/errors.conf
    /etc/$pname/mod_log_config.conf
    /etc/$pname/mod_mime-defaults.conf
    /etc/$pname/ssl-global.conf
    /etc/$pname/sysconfig.d/global.conf
    )
    for filename in ${filenames[@]}
    do
      sed -i "s//apache2//$pname/" $filename
    done
    
    
    ###############################
    # IMPORTANT : Change pid file #
    ###############################
    filename=/etc/init.d/$pname
    sed -i 's//var/run/httpd2.pid//var/run/$pname.pid/' $filename
    echo -e "nPidFile /var/run/$pname.pidn" >> /etc/$pname/uid.conf
    
    
    #########################
    # Adjust missing config #
    #########################
    filename=/etc/sysconfig/$pname
    sed -i "s/ServiceRestart: apache2/ServiceRestart: $pname/" $filename
    sed -i "s/Network/WWW/Apache2/Network/WWW/$pname/" $filename
    sed -i "s/Configuration for Apache 2/Configuration for $pname/" $filename
    filename=/etc/init.d/$pname
    sed -i "s/# /etc/init.d/apache2/# /etc/init.d/$pname/" $filename
    sed -i "s/apache apache2 httpd/$pname/" $filename
    sed -i "s/Apache 2.2 HTTP Server/HTTP instance $pname/" $filename
    sed -i "s/Start the Apache HTTP daemon/Start HTTP instance $pname/" $filename
    sed -i "s/Starting httpd2/Starting HTTP instance $pname/" $filename
    sed -i "s/Shutting down httpd2/Shutting down HTTP instance $pname/" $filename
    sed -i "s/Restarting httpd2/Restarting $pname/" $filename
    sed -i "s/Reload httpd2/Reload $pname/" $filename
    sed -i "s/Checking for httpd2/Checking for $pname/" $filename
    
    
    #############
    # Logrotate #
    #############
    cp -p /etc/logrotate.d/apache2 /etc/logrotate.d/$pname
    filename=/etc/logrotate.d/$pname
    sed -i "s//apache2//$pname/" $filename
    
    
    ###################################################
    # Make php.ini per instance at /etc/$pname/conf.d #
    ###################################################
    if [ -e /etc/php5/apache2/php.ini ]; then
      cp -p /etc/php5/apache2/php.ini /etc/$pname/conf.d
      echo -e "ntPHPIniDir /etc/$pname/conf.dnn" > /etc/$pname/conf.d/php.conf
    fi
    
  • mvorwald says:

    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

  • Leave a Reply

    Your email address will not be published. Required fields are marked *