2 instances of Apache2 mass virtual hosting

I wanted to have a mass virtual hosting for http and a mass virtual hosting for https, the only solution I found was to have 2 instances. I used the macro capabilities of Apache2, this can help to use different IP addresses, different network ports or configuration. All the following commands are for Linux Debian Etch.

I already had a mass virtual hosting running after this post :

http://offroad.gonzofamily.com/2008/01/12/apache2-mass-virtual-hosting/

Then now I want it to work on both 80 and 443 tcp ports.

First you must enable the ssl module of apache :

a2enmod ssl

Let’s activate the macro mode of Apache2. This will permite to run 2 instances of Apache2.

apt-get install libapache2-mod-macro
a2enmod macro

Create a file named /etc/apache2/apache2-instance.macro :

<Macro SetupInstance $instance $ip $port>
LockFile /var/lock/apache2/$instance.accept.lock
PidFile /var/run/apache2/$instance.pid
ErrorLog /var/log/apache2/$instance/error.log
Listen $ip:$port
</Macro>

Create /etc/apache2/apache2-http.conf :

Include /etc/apache2/apache2.conf
Include /etc/apache2/apache2-instance.macro
Use SetupInstance "apache2-http" "*" "80"
UseCanonicalName Off
# this log format can be split per-virtual-host based on the first field
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog /var/log/apache2/access_log vcommon
# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /home/websites/http/%0/online
VirtualScriptAlias /home/websites/http/%0/cgi-bin

And /etc/apache2/apache2-https.conf :

Include /etc/apache2/apache2.conf
Include /etc/apache2/apache2-instance.macro
Use SetupInstance "apache2-https" "*" "443"
# SSL STUFF
SSLEngine on
SSLCertificateFile /etc/ssl/private/company.com.crt
SSLCertificateKeyFile /etc/ssl/private/company.com.key
# END SSL STUFF
UseCanonicalName Off
# this log format can be split per-virtual-host based on the first field
LogFormat "%V %h %l %u %t \"%r\" %s %b" vcommon
CustomLog /var/log/apache2/access_log vcommon
# include the server name in the filenames used to satisfy requests
VirtualDocumentRoot /home/websites/https/%0/online
VirtualScriptAlias /home/websites/https/%0/cgi-bin

So now I don’t need any file like /etc/apache2/sites-enabled/010-vmasshost

rm /etc/apache2/sites-enabled/010-vmasshost

And now the http(s) websites will be in /home/websites/http(s).

Don’t forget to create the log folders :

mkdir /var/log/apache2/apache2-http
mkdir /var/log/apache2/apache2-https

Copy the /usr/sbin/apache2ctl to /usr/sbin/apache2-http-ctl et /usr/sbin/apache2-https-ctl

cp /usr/sbin/apache2ctl /usr/sbin/apache2-http-ctl
cp /usr/sbin/apache2ctl /usr/sbin/apache2-https-ctl

Edit apache2-http-ctl and change this line into :

HTTPD='/usr/sbin/apache2 -f /etc/apache2/apache2-http.conf'

Edit apache2-https-ctl and change this line into :

HTTPD='/usr/sbin/apache2 -f /etc/apache2/apache2-https.conf'

Copy /etc/init.d/apache2 to /etc/init.d/apache2-http and /etc/init.d/apache2-https and move the file to /etc/init.d/apache2.save

cp /etc/init.d/apache2 /etc/init.d/apache2-http
cp /etc/init.d/apache2 /etc/init.d/apache2-https
mv /etc/init.d/apache2 /etc/init.d/apache2.save

Edit /etc/init.d/apache2-http and set

APACHE2CTL="$ENV /usr/sbin/apache2-http-ctl"

Edit /etc/init.d/apache2-https and set

APACHE2CTL="$ENV /usr/sbin/apache2-https-ctl"

Add at the beginning of /etc/init.d/apache2-http :

PIDFILE='/var/run/apache2/apache2-http.pid'
if [ -e "$PIDFILE" ] ; then
PID=`cat "$PIDFILE"`
fi

Add at the beginning of /etc/init.d/apache2-https :

PIDFILE='/var/run/apache2/apache2-https.pid'
if [ -e "$PIDFILE" ] ; then
PID=`cat "$PIDFILE"`
fi

Comment out everything from ‘for i in $PID’ to ‘done’. Make the same changes for /etc/init.d/apache2-https

    #for i in $PIDS; do
#    if [ "$i" = "$PID" ]; then
# in this case the pid stored in the
# pidfile matches one of the pidof apache
# so a simple kill will make it
#        echo $PID
#        return 0
#    fi
#done

Last create a new /etc/init.d/apache2 script (mind the execute privs) with the following content:

#!/bin/sh
/etc/init.d/apache2-http $@
/etc/init.d/apache2-https $@

Comment the content of /etc/apache2/ports.conf

#Listen 80
#Listen 443

NB: for the moment I have the following error when I try to restart :

root@server:/etc/init.d# /etc/init.d/apache2 restart
Forcing reload of web server (apache2)...httpd (no pid file) not running
waiting .............................httpd (pid 25601) already running
Forcing reload of web server (apache2)...httpd (no pid file) not running
waiting .............................httpd (pid 25617) already running

References :
(FR) http://g-rossolini.developpez.com/tutoriels/logiciels/plusieurs-apache-simultanes/
(EN) http://blog.adaniels.nl/articles/running-multiple-instances-of-apache/

Leave a Reply

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