Apache server doesn't start automatically

Try to run

update-rc.d apache2 enable [list of run levels]

as root.

You may interested to read

man update-rc.d

I'm adding this answer based on recent issues I encountered with the same symptoms.

First some background data:

  • Ubuntu uses scripts in the /etc/init.d/ folder to start/stop services.
  • Ubuntu uses symlinks to those /etc/init.d/ scripts, stored in the /etc/rc#.d/ folders, to start/stop services based on the "runlevel".
  • Symlinks that start with an "S" indicate that the service should be started.
  • Symlinks that start with a "K" indicate that the service should be stopped (killed).
  • Runlevel 1 executes the scripts symlinked in /etc/rc1.d/, runlevel 2 uses /etc/rc2.d/, and so on.
  • The default runlevel for Ubuntu is 2.
  • Installation of Apache essentially runs sudo update-rc.d apache2 defaults which creates the appropriate symlinks in the /etc/rc#.d/ folders.

So it seems that on my server something, or someone, at some point ran sudo update-rc.d apache2 disable which removed all the "S" symlinks and replaced them with "K" symlinks. Thus killing, or just not starting, Apache when initializing any of the runlevels.

My solution was just to re-enable Apache:

sudo update-rc.d apache2 enable

Now Apache starts/stops as expected when starting up or switching runlevels.

NB:

It's worth noting that just running sudo update-rc.d apache2 defaults again is insufficient because it sees that symlinks exist and considers that they are what is wanted. It just responds with:

System start/stop links for /etc/init.d/apache2 already exist.

In this case, I found out why Apache wouldn't start. I could not find a trace of this in the startup logs, only in the output printed to the screen on bootup.

But here you go: The last line in the script /etc/apache2/apache2.conf failed.

Why?

It says:

Include sites-enabled/

However, two of the sites I've set up are located in my own home directory - which is encrypted!

So, on bootup (during startup of Apache) these sites don't exist, and Apache fails and refuses to run.

Solution?

I've created a small script called "disable_sites" and symlinked it into /etc/rc0.d and /etc/rc6.d (shutdown and reboot):

#!/bin/bash

/usr/sbin/a2dissite vvsshop
/usr/sbin/a2dissite neoflex
/etc/init.d/apache2 reload

exit 0

I also made a script to re-enable the sites whenever I log in and added it as a startup program in my System Settings. So now it works!

So, I suppose the lesson here is that when Apache inexplicably fails to load during bootup and you can't find any errors in the logs or when starting the server manually, check if all the directories it needs are available. This could probably also be solved in some other way.