Ubuntu unattended-upgrades stops apache

I ran into the exact same problem today. It's late answer but might be helpful to someone.
My OS: ubuntu 16.04.7

Short answer:
DO NOT start apache2 processes by apachectl start.
Instead, always use systemctl start apache2.


When you start apache2 by apachectl systemd cannot realize the processes are running. (systemctl status apache2 shows you inactive(dead))

One day, unattended-upgrades applies some library updates, then apache2 is going to be restarted via systemd. This behavior is defined in /var/lib/dpkg/info/xxx.postinst (xxx is the library updated). In my case apache2_invoke enmod php7.0 is called and finally it invokes invoke-rc.d apache2 restart which is equivalent to systemctl stop apache2 && systemctl start apache2.

Let's take a look at the systemd service unit of apache2.

/lib/systemd/system/apache2.service.d/apache2-systemd.conf

[Service]
Type=forking
RemainAfterExit=no

The definition has no ExecStart of ExecStop line because it is generated by systemd-sysv-generator, so systemctl cmd is a facade of /etc/init.d/apache2 in this case.
The important line is RemainAfterExit=no. This means "After the start script (init.d/apache2 start) exits, there must not be the daemon either, so that ExecStop will be called to clean up processes."

Here is a whole apache-disappearing scenario.

  1. unattended-upgrades invokes systemctl restart apache2. (or maybe just systemctl start apahce2, then skip to 3)
  2. ExecStop is called and just ignored because the service status is inactive.
  3. ExecStart is called and exits immediately because there are already apache2 processes which started by apachectl.
  4. ExecStop is called again because ExecStart is over, this time the script is executed because systemd now controls apache2.
  5. ExecStop (/etc/init.d/apache2 stop) kills all apache2 processes even though they are started by apachectl.