使用 systemd、Raspbian 8 (jessie) 启动 nginx?

使用 systemd、Raspbian 8 (jessie) 启动 nginx?

尝试在启动时启动 nginx 时收到以下错误Raspbian GNU/Linux 8 (jessie)

● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled)
   Active: failed (Result: exit-code) since Sun 2016-08-07 10:38:50 EDT; 1min 10s ago
  Process: 478 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)

我的配置工作正常,登录后可以启动 nginx;但我无法让 systemd 启动它。

这是我的单位文件:

# 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=syslog.target network.target remote-fs.target nss-lookup.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

/lib/systemd/system/nginx.service

之后我需要达到什么目标才能开始?我已经尝试过network-online.target(这将是最有意义的,并且收到了相同的结果。

更新

我改变了一些事情,感谢这个帖子我让 nginx 启动了...但它仍然失败并出现错误。

  1. 修改/etc/systemd/system/mult-user.target.wants/nginx.service为包括:
    1. After=network-online.target
    2. Wants=network-online.target
      1. 之前是After=network.target
  2. Ran sudo systemctl enable systemd-networkd-wait-online.service,启动它(因为network-online.target无法使用 启用sudo systemctl enable network-online.target
  3. sudo systemctl enable nginx
  4. 重新启动...
  5. 重新启动后,我在文件中运行systemd-analyze plot > something.svg并搜索nginx.service,它存在,但是它没有成功启动,而是给我一个关于我的反向代理服务器的错误..我不知道如何解决,但这是一个另一个问题的主题。

下面是我从 systemd-analyze 图中得到的图像:

systemd-分析输出...

但是......在我启动机器并运行之后,sudo systemctl start nginx它就可以正常启动。

以下是错误日志中的错误消息:

2017/05/16 13:12:53 [emerg] 555#0: host not found in upstream "somehost.somedomain.lan" in /etc/nginx/sites-enabled/siteconf:41

这是有问题的配置行:

server {
     listen     80; ## listen for ipv4; this line is default and implied
    #listen   [::]:80 default_server ipv6only=on; ## listen for ipv6

    # Make site accessible from http://localhost/
    server_name somehost somehost.somedomain.lan;

    # Note: There should never be more than one root in a 
    #       virtual host
    #   Also there should never be a root in the location.
    #root /var/www/nginx/;

         location ^~ / {
            resolver 127.0.0.1 valid=300s; # NOTE: Added this to resolve it.
            access_log ./logs/RootWiki_access.log;
            error_log ./logs/RootWiki_error.log;
            proxy_buffers 16 4k;
            proxy_buffer_size 2k;
            proxy_set_header Host $host;
            proxy_set_header X-Real_IP $remote_addr;
            rewrite /(.*) /$1 break;
            proxy_pass http://wiki.leerdomain.lan:8080; # NOTE: This one causes the error according to the error log.
        }

答案1

得到这个工作:

# 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=multi-user.target
Requires=network-online.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

在该Unit部分下我添加了以下内容:

[Unit]
# ...
After=multi-user.target
Requires=network-online.target

/lib/systemd/system/nginx.service

我还在 bash 中运行了以下命令:

$ sudo systemctl enable nginx

然后确保符号链接显示:

$ ls -la /etc/systemd/system/multi-user.target/wants
...
lrwxrwxrwx  1 root root   33 May 14  2016 nginx.service -> /lib/systemd/system/nginx.service
...

然后重新加载守护进程:

$ sudo systemctl daemon-reload

最后重新启动并查看它是否存在:

$ sudo systemctl status --state active | grep nginx

该图显示 nginx.service 已在多用户目标之后移动

相关内容