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がマルチユーザーターゲットの背後に移動したことを示しています。

おすすめ記事