How to sequence a proper shutdown and startup of web services with systemd?

I am trying to structure the shutdown and startup of Apache, PHP-FPM and MariaDB services using systemd:

These are the additional configuration files in /etc/systemd/system folder:

# httpd.service
.include /usr/lib/systemd/system/httpd.service
[Unit]
After=mariadb.service php-fpm.service
Before=php-fpm.service

# php-fpm.service
.include /usr/lib/systemd/system/php-fpm.service
[Unit]
Before=mariadb.service

My intention is to start Apache only after PHP-FPM and MariaDB has started and to stop Apache before stopping PHP-FPM, stop PHP-FPM before MariaDB.

However, I am getting errors on both startup and shutdown:

12:42:09 systemd[1]: Found ordering cycle on php-fpm.service/stop
12:42:09 systemd[1]: Found dependency on mariadb.service/stop
12:42:09 systemd[1]: Found dependency on php-fpm.service/stop
12:42:09 systemd[1]: Job httpd.service/stop deleted to break ordering cycle starting with php-fpm.service/stop
12:42:09 systemd[1]: Stopping MariaDB database server...
12:42:12 systemd[1]: Stopped MariaDB database server.
12:42:12 systemd[1]: Stopping The PHP FastCGI Process Manager...
12:42:12 systemd[1]: Failed to remove content of temporary directory /tmp/systemd-mariadb.service-Xp7JJZ5: No such file or directory
12:42:12 systemd[1]: Stopped The PHP FastCGI Process Manager.
12:42:12 systemd[1]: Failed to remove content of temporary directory /tmp/systemd-php-fpm.service-XPLabUE: No such file or directory
-- Reboot --
12:46:20 systemd[1]: Found ordering cycle on php-fpm.service/start
12:46:20 systemd[1]: Found dependency on mariadb.service/start
12:46:20 systemd[1]: Found dependency on php-fpm.service/start
12:46:20 systemd[1]: Job httpd.service/start deleted to break ordering cycle starting with php-fpm.service/start

It seems that the ordering cycle that I specified is causing problems. How should this be resolved?


Solution 1:

As far as i can see, you are having a cyclic dependecy. You are telling systemd to start PHP-fpm before Apache and at the same time after Apache. This cannot work the way you want it to.

In your httpd.service file specify the following:

Requires=mariadb.service php-fpm.service
After=mariadb.service php-fpm.service

The explanation of the options for systemd unit files. It also says that the shutdown-order will be the reversed startup order, so you do not have to configure that separately. The "Requires" part will make sure Apache only starts if MariaDB and PHP-fpm start successfully.