Configure Supervisor to run Apache2 webserver on Ubuntu/Debian Linux

The idea about starting your services under Supervisor’s supervision is to be able to run multiple services under a single daemon. Depending on the Supervisor’s configuration it will be able to start,stop or restart any given service as a child process. In this config we show how to runapache2 as supervised service ( useful for docker images etc. ) on Ubuntu/Debian Linux.
First, install supervisor:

# apt-get install supervisor

Include apache2‘s supervisor configuration into /etc/supervisor/conf.d/. Supervisor will pick any configuration files from this directory where the only requirement is *.conf file extension. For example insert a following lines into a new file /etc/supervisor/conf.d/apache2.conf:

[program:apache2]
command=/usr/sbin/apache2ctl -DFOREGROUND

Once you have included the above config file stop apache2 webserver if its currently running:

# /etc/init.d/apache2 stop  
[ ok ] Stopping web server: apache2.
root@4e004b451a98:/# /etc/init.d/apache2 status
[FAIL] apache2 is not running ... failed!

and restart supervisor:

# /etc/init.d/supervisor restart
Restarting supervisor: supervisord.

Once you have restarted supervisor the apache2 webserver should be also started. Confirm that apache2 webserver is running:

# /etc/init.d/apache2 status
[ ok ] apache2 is running.

Troubleshooting

if from some reason you cannot get your apache running under supervisor run supervisor in nodaemon mode:

# /etc/init.d/supervisor stop
Stopping supervisor: supervisord.
# supervisord -n

Error message:

/usr/lib/python2.7/dist-packages/supervisor/options.py:296: UserWarning: Supervisord is running as root and it is searching for its configuration file in default locations (including its current working directory); you probably want to specify a "-c" argument specifying an absolute path to a configuration file for improved security.
  'Supervisord is running as root and it is searching '
2015-05-07 01:21:00,849 CRIT Supervisor running as root (no user in config file)
2015-05-07 01:21:00,849 WARN Included extra file "/etc/supervisor/conf.d/apache2.conf" during parsing
2015-05-07 01:21:00,858 INFO RPC interface 'supervisor' initialized
2015-05-07 01:21:00,858 CRIT Server 'unix_http_server' running without any HTTP authentication checking
2015-05-07 01:21:00,858 INFO supervisord started with pid 542
2015-05-07 01:21:01,862 INFO spawned: 'apache2' with pid 545
2015-05-07 01:21:01,914 INFO exited: apache2 (exit status 0; not expected)
2015-05-07 01:21:02,918 INFO spawned: 'apache2' with pid 548
2015-05-07 01:21:02,970 INFO exited: apache2 (exit status 0; not expected)
2015-05-07 01:21:04,975 INFO spawned: 'apache2' with pid 551
2015-05-07 01:21:05,025 INFO exited: apache2 (exit status 0; not expected)
2015-05-07 01:21:08,031 INFO spawned: 'apache2' with pid 554
2015-05-07 01:21:08,078 INFO exited: apache2 (exit status 0; not expected)
2015-05-07 01:21:09,079 INFO gave up: apache2 entered FATAL state, too many start retries too quickly

If you see the above error message make sure that you Apache webserver is not running already.

Another error message you may see is:

AH00526: Syntax error on line 74 of /etc/apache2/apache2.conf:
Invalid Mutex directory in argument file:${APACHE_LOCK_DIR}

This is because you are trying to run apache2 command directly. You may try run apache2ctl instead.