Systemd

使用 systemd、Raspbian 8 (jessie) 在啟動時啟動 nginx?

  • September 1, 2019

嘗試在啟動時啟動 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為包括:

  2. After=network-online.target

  3. Wants=network-online.target

    1. 之前After=network.target
  4. Ran sudo systemctl enable systemd-networkd-wait-online.service,啟動它(因為network-online.target無法使用 啟用sudo systemctl enable network-online.target

  5. sudo systemctl enable nginx

  6. 重新啟動…

  7. 重新啟動後,我在文件中執行systemd-analyze plot > something.svg並蒐索nginx.service,它存在,但是它沒有成功啟動,而是給我一個關於我的反向代理伺服器的錯誤..我不知道如何解決,但這是一個另一個問題的主題。

下面是我從 systemd-analyze plot 得到的圖像:

系統分析輸出…

但是……在我啟動機器並執行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.
       }

得到這個工作:

# 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 已在多使用者目標之後移動的圖表

引用自:https://unix.stackexchange.com/questions/301903