![如何使用 systemd 正确关闭和启动 Web 服务?](https://linux22.com/image/639097/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%20systemd%20%E6%AD%A3%E7%A1%AE%E5%85%B3%E9%97%AD%E5%92%8C%E5%90%AF%E5%8A%A8%20Web%20%E6%9C%8D%E5%8A%A1%EF%BC%9F.png)
我正在尝试使用 systemd 构建 Apache、PHP-FPM 和 MariaDB 服务的关闭和启动:
这些是文件夹中的附加配置文件/etc/systemd/system
:
# 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
我的目的是仅在 PHP-FPM 和 MariaDB 启动后启动 Apache,并在停止 PHP-FPM 之前停止 Apache,在停止 MariaDB 之前停止 PHP-FPM。
但是,我在启动和关闭时都遇到错误:
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
看来我指定的订购周期出了问题。该如何解决?
答案1
据我所知,您有一个循环依赖。您告诉 systemd 在 Apache 之前启动 PHP-fpm,同时在 Apache 之后启动。这无法按您希望的方式工作。
在你的httpd 服务文件指定以下内容:
Requires=mariadb.service php-fpm.service
After=mariadb.service php-fpm.service
systemd 单元文件选项的解释。它还表示关机顺序将是反向启动顺序,因此您不必单独配置。“Requires”部分将确保仅当 MariaDB 和 PHP-fpm 成功启动时 Apache 才会启动。