Nginx init.d 脚本

Nginx init.d 脚本

昨天在我的 debian 机器上进行 dist 升级后,我的 nginx 初始化脚本遇到了一些问题。每当我尝试使用 init 脚本启动 nginx 时,都会出现一条错误消息:

# service nginx start
Job for nginx.service failed. See 'systemctl status nginx.service' and 'journalctl -xn' for details.

systemctl status nginx.service 的输出:

# systemctl status nginx.service
● 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 Tue 2015-05-19 12:36:12 CEST; 7s ago
  Process: 14087 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
  Process: 14126 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=203/EXEC)

Journalctl -xn 的输出:

# journalctl -xn
systemd[1]: nginx.service never wrote its PID file. Failing.
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
systemd[1]: Unit nginx.service entered failed state.
systemd[14126]: Failed at step EXEC spawning /usr/sbin/nginx: No such file or directory
-- Subject: Process /usr/sbin/nginx could not be executed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- The process /usr/sbin/nginx could not be executed and failed.
--
-- The error number returned while executing this process is 2.
systemd[1]: nginx.service: control process exited, code=exited status=203
systemd[1]: Failed to start A high performance web server and a reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
systemd[1]: Unit nginx.service entered failed state.

我的 nginx 文件在 init.d 中:

#! /bin/sh

### BEGIN INIT INFO
# Provides:          nginx
# Required-Start:    $all
# Required-Stop:     $all
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts the nginx web server
# Description:       starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/nginx
NAME=nginx
DESC=nginx

test -x $DAEMON || exit 0

# Include nginx defaults if available
if [ -f /etc/default/nginx ] ; then
    . /etc/default/nginx
fi

set -e

. /lib/lsb/init-functions

case "$1" in
  start)
    echo -n "Starting $DESC: "
    start-stop-daemon --start --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
        --exec $DAEMON || true
    echo "$NAME."
    ;;
  restart|force-reload)
    echo -n "Restarting $DESC: "
    start-stop-daemon --stop --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON || true
    sleep 1
    start-stop-daemon --start --quiet --pidfile \
        /usr/local/nginx/logs/$NAME.pid --exec $DAEMON -- $DAEMON_OPTS || true
    echo "$NAME."
    ;;
  reload)
      echo -n "Reloading $DESC configuration: "
      start-stop-daemon --stop --signal HUP --quiet --pidfile /usr/local/nginx/logs/$NAME.pid \
          --exec $DAEMON || true
      echo "$NAME."
      ;;
  status)
      status_of_proc -p /usr/local/nginx/logs/$NAME.pid "$DAEMON" nginx && exit 0 || exit $?
      ;;
  *)
    N=/etc/init.d/$NAME
    echo "Usage: $N {start|stop|restart|reload|force-reload|status}" >&2
    exit 1
    ;;
esac

exit 0

nginx 守护进程文件位于/usr/local/sbin/nginx(我在那里编译的)。

到目前为止我尝试过的事情:我将守护程序文件复制/usr/local/sbin/nginx/usr/sbin/nginx

当我使用它时,service nginx start它会挂在启动 nginx 上。当我尝试连接到端口 80 时,它确实有效。所以我继续编辑我的初始化脚本并更改PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/binPATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/sbin:/usr/bin.然而,这给出了相同的结果。

到目前为止我还没有尝试过任何其他事情。

答案1

我的 nginx 文件在 init.d 中:

… 是完全不相关。看看 的输出systemctl。它准确地告诉您要查看哪个文件:

已加载:已加载(/lib/systemd/system/nginx.service;已启用)

是你的服务单位。并systemctl告诉您该服务单元已配置哪些内容来准备启动服务:

进程:14126 ExecStartPre=/usr/sbin/nginx -t -q -g 守护进程开启; master_process 开启;(代码=已退出,状态=203/EXEC)

这就是您将在ExecStartPre服务单元的设置中找到的内容。您会从中注意到这PATH也是完全无关的(这是 systemd 中的设计)。该杂志对所发生的事情更加明确:

systemd[14126]:在步骤 EXEC 生成 /usr/sbin/nginx 时失败:没有这样的文件或目录

无论您怎么想,计算机都会认为您尝试启动该服务时/usr/sbin/nginx无法作为程序运行,因为不存在该名称的文件。

我有根据的猜测是,您RootDirectory的服务单元中有一个设置,或者您将文件复制/usr/sbin不是在更改后的根环境中,或者更改后的根环境中缺少加载程序映像所需的一些辅助共享库。

答案2

我在 Debian 8 中遇到了同样的问题。对我来说,我发现

[date] debian systemd[1]: nginx.service never wrote its PID file. Failing.

在 /var/log/syslog ,这与问题类似

# journalctl -xn
systemd[1]: nginx.service never wrote its PID file. Failing.

原因是在 /lib/systemd/system/nginx.service 我有

PIDFile=/var/run/nginx.pid

但由于 nginx 已构建,因此它会将 pid 写入 /usr/local/nginx/log/nginx.pid

修复方法是更改​​/添加 /usr/local/nginx/conf/nginx.conf 中的行

pid        /var/run/nginx.pid;

相关内容