Ubuntu – Apache2 fails to start on boot with Ubuntu 16.04

16.04Apache2serversystemd

I have just created a new Ubuntu 16.04 server (on digital ocean, if it matters). I have set up Apache2, and it runs correctly, but on every restart it fails to boot.

I get the following from systemctl status:

root@twl-ubuntu-2gb-sgp1-01:~# systemctl status apache2.service 
● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; static; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: inactive (dead)
     Docs: man:systemd-sysv-generator(8)

If I run systemctl is-enabled I get "static":

root@twl-ubuntu-2gb-sgp1-01:~# systemctl is-enabled apache2.service 
static

Even after running systemctl enable it doesn't change:

root@twl-ubuntu-2gb-sgp1-01:~# systemctl enable apache2.service 
Synchronizing state of apache2.service with SysV init with /lib/systemd/systemd-sysv-install...
Executing /lib/systemd/systemd-sysv-install enable apache2
root@twl-ubuntu-2gb-sgp1-01:~# systemctl is-enabled apache2.service 
static

But apache has no problems starting. When I run systemctl start manually it works just fine:

root@twl-ubuntu-2gb-sgp1-01:~# systemctl start apache2.service 
root@twl-ubuntu-2gb-sgp1-01:~# systemctl status apache2.service 
● apache2.service - LSB: Apache2 web server
   Loaded: loaded (/etc/init.d/apache2; static; vendor preset: enabled)
  Drop-In: /lib/systemd/system/apache2.service.d
           └─apache2-systemd.conf
   Active: active (running) since Sat 2016-07-09 07:33:30 EDT; 8s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 1847 ExecStart=/etc/init.d/apache2 start (code=exited, status=0/SUCCESS)
    Tasks: 7
   Memory: 22.5M
      CPU: 455ms
   CGroup: /system.slice/apache2.service
           ├─1871 /usr/sbin/apache2 -k start
           ├─1875 /usr/sbin/apache2 -k start
           ├─1876 /usr/sbin/apache2 -k start
           ├─1877 /usr/sbin/apache2 -k start
           ├─1878 /usr/sbin/apache2 -k start
           └─1879 /usr/sbin/apache2 -k start

Jul 09 07:33:28 twl-ubuntu-2gb-sgp1-01 systemd[1]: Starting LSB: Apache2 web server...
Jul 09 07:33:28 twl-ubuntu-2gb-sgp1-01 apache2[1847]:  * Starting Apache httpd web server apache2
Jul 09 07:33:29 twl-ubuntu-2gb-sgp1-01 apache2[1847]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set t
Jul 09 07:33:30 twl-ubuntu-2gb-sgp1-01 apache2[1847]:  *
Jul 09 07:33:30 twl-ubuntu-2gb-sgp1-01 systemd[1]: Started LSB: Apache2 web server.

I'm new to systemd, only recently upgraded to 16.04 from 14.04. Where am I going wrong, or perhaps, what can I do to debug why apache2 is refusing to start?

Update 1 (2016-07-14)

It turns out Apache is being loaded, but is receiving a SIGTERM as soon as it launches. The only line in the log file is:

[Wed Jul 13 21:37:15.730331 2016] [mpm_prefork:notice] [pid 1871] AH00169: caught SIGTERM, shutting down

Any ideas how to get a better error message? As I mentioned running systemctl start apache2 immediately after boot starts everything just fine.

Update 2 (2016-07-15)

It turns out that Sigterm was from it being killed when I ran sudo reboot. There were no log messages in /var/log/apache2/error.log. If I run journalctl I get these lines during the bootup process, so it seems to work correctly, or at least, no errors are logged:

Jul 13 21:38:04 twl-ubuntu-2gb-sgp1-01 systemd[1]: Starting LSB: Apache2 web server...
Jul 13 21:38:04 twl-ubuntu-2gb-sgp1-01 apache2[1800]:  * Starting Apache httpd web server apache2
Jul 13 21:38:04 twl-ubuntu-2gb-sgp1-01 apache2[1800]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set t
Jul 13 21:38:05 twl-ubuntu-2gb-sgp1-01 apache2[1800]:  *
Jul 13 21:38:05 twl-ubuntu-2gb-sgp1-01 systemd[1]: Started LSB: Apache2 web server.

But then the service is never accessible, and systemctl status apache2.service still reports it as "inactive (dead)"

Best Answer

I looked up what the the static state of is-enabled means, and found that:

In this context, static means that the unit file does not contain an "install" section, which is used to enable a unit. As such, these units cannot be enabled.

Also, I found that the Apache2 package contains only a systemd include file, not a complete file:

 # /lib/systemd/system/apache2.service.d/apache2-systemd.conf
 [Service]
 Type=forking
 RemainAfterExit=no

It appears that the remainder of the logic for managing the Apache2 process comes from using a shim that allows systemd to run SysVinit scripts. So try enabling Apache2 to run at boot using a symlink in a SysVinit directory:

ln -s /etc/init.d/apache2 /etc/rc5.d/S02apache2

It seems like a bug or at least a missing feature that Apache2 isn't shipping with a full systemd 'service' file on Ubuntu 16.04.