使用 systemd 在启动时启动 nginx

使用 systemd 在启动时启动 nginx

我刚刚在 Debian 8 服务器上安装了 nginx 1.9。当我告诉它运行时,nginx 工作正常,但它似乎不会在启动时自动加载 nginx。

我已经尝试了互联网上推荐的许多初始化脚本,但还没有任何效果。所以现在我试图用 systemctl 来解决这个问题。

~$ systemctl status nginx
● nginx.service
   Loaded: masked (/dev/null)
   Active: inactive (dead)
~$ sudo systemctl try-restart nginx
Failed to try-restart nginx.service: Unit nginx.service is masked.
~$ sudo systemctl reload nginx
Failed to reload nginx.service: Unit nginx.service is masked.
~$ sudo systemctl reload nginx
Failed to reload nginx.service: Unit nginx.service is masked.

不幸的是,我不知道“服务被屏蔽”是什么意思,也不知道为什么它被屏蔽。

当我跑步时

sudo nginx

服务器运行得很好。然后,我研究了揭开 nginx 服务的掩码。

~$ sudo systemctl unmask nginx.service
Removed symlink /etc/systemd/system/nginx.service.

好吧酷,现在我可以使用 systemctl 启动 nginx。所以我检查了一下重启是否会自动加载nginx。但它未能做到这一点,我不知道接下来该去哪里。

有人可以帮我让 nginx 在启动时自动运行吗?

答案1

您似乎混淆了启用、启动和屏蔽操作。

  • systemctl start, systemctl stop: 启动(停止)相关单元立即地;
  • systemctl enable, systemctl disable: 标记(取消标记)单位开机时自动启动(以特定于单位的方式,在其[Install]部分中描述);
  • systemctl mask, systemctl unmask:禁止(允许)所有和任何启动相关单元的尝试(手动或作为任何其他单元的依赖项,包括默认启动目标的依赖项)。请注意,systemd 中自动启动的标记是通过添加从默认启动目标到相关单元的人为依赖来实现的,因此“mask”也不允许自动启动。

因此,这些都是不同的操作。其中,你想要systemctl enable

参考号:系统控制(1)

更多:Lennart Poettering (2011-03-02)。 《关闭的三个层次》管理员的 systemd。 0pointer.de。

答案2

修复了已接受答案中的链接,使其重定向到正确的页面。但这里有一个相关的内容:

sudo systemctl enable nginx.service
sudo systemctl start nginx.service
sudo systemctl status nginx.service

看起来/lib/systemd/system/nginx.service像这样:

# Stop dance for nginx
# =======================
#
# ExecStop sends SIGSTOP (graceful stop) to the nginx process.
# If, after 5s (--retry QUIT/5) nginx is still running, systemd takes control
# and sends SIGTERM (fast shutdown) to the main process.
# After another 5s (TimeoutStopSec=5), and if nginx is alive, systemd sends
# SIGKILL to all the remaining processes in the process group (KillMode=mixed).
#
# nginx signals reference doc:
# http://nginx.org/en/docs/control.html
#
[Unit]
Description=A high performance web server and a reverse proxy server
After=network.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q -g 'daemon on; master_process on;'
ExecStart=/usr/sbin/nginx -g 'daemon on; master_process on;'
ExecReload=/usr/sbin/nginx -g 'daemon on; master_process on;' -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /run/nginx.pid
TimeoutStopSec=5
KillMode=mixed

[Install]
WantedBy=multi-user.target

`

答案3

这对我有用: https://web.archive.org/web/20150328063215/https://longhandpixels.net/blog/2014/02/install-nginx-debian-ubuntu

我忽略了该文档的大部分内容,该文档专门用于编译 nginx 的其他版本,并转到“使其自动启动”。

我按照那里的指示进行操作,现在当我重新启动时,nginx 1.9 正在运行。

我非常感谢大家的帮助和见解。谢谢你们!

答案4

来自 nginx 资源https://www.nginx.com/resources/wiki/start/topics/examples/systemd/

echo "
[Unit]
Description=The NGINX HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true

[Install]
WantedBy=multi-user.target
" > /lib/systemd/system/nginx.service

相关内容