Server just upgraded from Squeeze to Wheezy. After reboots, I noted that apache2 wasn't starting, so installed bootlogd to inspect the output of init scripts.
The error output from apache start was,
Tue Feb 3 08:49:55 2015: [....] Starting web server: apache2(99)Cannot assign requested address: make_sock: could not bind to address [0123:4567:890:abc::d]:80
Tue Feb 3 08:49:55 2015: no listening sockets available, shutting down
Tue Feb 3 08:49:55 2015: Unable to open logs
Tue Feb 3 08:49:55 2015: Action 'start' failed.
Tue Feb 3 08:49:55 2015: The Apache error log may have more information.
Timestamps in the boot output show 4s between "Configuring network interfaces" (08:49:51) and starting apache.
I've worked around this by inserting a sleep 5
into /etc/init.d/apache2
after which apache2 starts fine, so I'm reading it as slow network initialization (possibly specific to IPv6 networking). The server is running on VMWare, and I have access only to the VM.
- What better methods could I use to make Apache wait a few seconds and check its addresses are up and running before start?
- Or to make the network init script not exit until the addresses are ready?
Best Answer
Well, the method I am outlining obviously does not solve the underlying issue. But I believe using a stand-alone monitoring tool that can handle the “kicking Apache into service” task without having to hack the
init.d
script is a more stable solution. The key is to usemonit
which is self-described as:To install it in Ubuntu, just do this; I am using
aptitude
and this is on Ubuntu 12.04 FWIW:Once installed I like to setup the default mail server for sending out alerts. This assumes you have
postfix
orsendmail
active on your server. Open up themonit
control file with your favorite text editor; I like to usenano
:Look for the
set mailserver
lines and set this line and save themonit
control file:Now check and make sure there is a
monit
conf.d
directory setup; this is where individualmonit
tasks are setup:If somehow that
/etc/monit/conf.d
was not set up, create it like this:Now with that done, let’s create a
monit
Apache ruleset. First, figure out where the Apache.pid
file is set and check that it is set. This is usually where it is set on Ubuntu 12.04 using the default Apache package install:If that
.pid
filepath is correct, let’s create the actualmonit
Apache ruleset withnano
like this:And place this code in that file and save it:
The logic to the script is fairly simple: The
monit
Apache ruleset will check the/var/run/apache2.pid
file and it knows to use specificinit.d
directives forstart
andstop
logic. The magic comes from theif
/then
block which basically monitors Apache port80
on localhost127.0.0.1
and will take action to recover Apache if there is a timeout of 15 seconds or more. And thealert
line will send e-mail alerts to a specified e-mail address if the conditions oftimeout
ornonexist
are met; that is optional so feel free to comment that out if you don’t have the need to be flooded by e-mail alerts.Now restart
monit
:And you can follow the
monit
log here to see it do it’s thing and debug it if something doesn’t work as expected:So when the dust settles on that server, you will have
monit
setup to make sure Apache is running. Very useful as a general “keep the Apache web server alive” tool, but in your casemonit
can take over the task of making sure Apache comes up on reboot/startup so you don’t have to muck around with core Apacheinit.d
script which is really a bit messy and is easily forgotten in the future during an upgrade.Also note, I can see that you are binding Apache explicitly to IPv6, but this monitoring script assumes that if it can’t reach
127.0.0.1
then it should take action. Maybe you need to change the IPv4 localhost address of127.0.0.1
to the IPv6 equivalent of address of::1
. Experiment and see what works.