如何阻止 nginx 在安装时自动启动?

如何阻止 nginx 在安装时自动启动?

我正在尝试通过以下命令在 Ubuntu 16.04 上安装 Nginx: sudo apt-get install nginx

但我收到以下错误:

Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
invoke-rc.d: initscript nginx, action "start" failed.
● nginx.service - A high performance web server and a reverse proxy server
   Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Tue 2017-07-11 16:41:31 UTC; 6ms ago
  Process: 17851 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=1/FAILURE)
 Main PID: 31524 (code=exited, status=0/SUCCESS)

Jul 11 16:41:31 vm systemd[1]: Starting A high performance web server and a reverse proxy s...r...
Jul 11 16:41:31 vm nginx[17851]: nginx: [emerg] open() "/etc/nginx/nginx.conf" failed (2: N...ory)
Jul 11 16:41:31 vm nginx[17851]: nginx: configuration file /etc/nginx/nginx.conf test failed
Jul 11 16:41:31 vm systemd[1]: nginx.service: Control process exited, code=exited status=1
Jul 11 16:41:31 vm systemd[1]: Failed to start A high performance web server and a reverse ...ver.
Jul 11 16:41:31 vm systemd[1]: nginx.service: Unit entered failed state.
Jul 11 16:41:31 vm systemd[1]: nginx.service: Failed with result 'exit-code'.
Hint: Some lines were ellipsized, use -l to show in full.
dpkg: error processing package nginx-core (--configure):
 subprocess installed post-installation script returned error exit status 1
dpkg: dependency problems prevent configuration of nginx:
 nginx depends on nginx-core (>= 1.10.3-0ubuntu0.16.04.1) | nginx-full (>= 1.10.3-0ubuntu0.16.04.1) | nginx-light (>= 1.10.3-0ubuntu0.16.04.1) | nginx-extras (>= 1.10.3-0ubuntu0.16.04.1); however:
  Package nginx-core is not configured yet.
  Package nginx-full is not installed.
  Package nginx-light is not installed.
  Package nginx-extras is not installed.
 nginx depends on nginx-core (<< 1.10.3-0ubuntu0.16.04.1.1~) | nginx-full (<< 1.10.3-0ubuntu0.16.04.1.1~) | nginx-light (<< 1.10.3-0ubuntu0.16.04.1.1~) | nginx-extras (<< 1.10.3-0ubuntu0.16.04.1.1~); however:
  Package nginx-core is not configured yet.
  Package nginx-full is not installed.
  Package nginx-light is not installed.
  Package nginx-extras is not installed.

dpkg: error processing package nginx (--configure):
 dependency problems - leaving unconfigured
No apport report written because the error message indicates its a followup error from a previous failure.
  Errors were encountered while processing:
 nginx-core
 nginx

我认为它在安装后默认尝试自动启动 nginx,但找不到 nginx 配置文件。如何才能阻止它在安装后自动启动,这样我的apt-get install命令就不会失败。

答案1

在基于 Debian 的发行版中,安装时服务默认启动并启用,这很不幸。这通常会导致安装不包含端到端默认配置的服务时出现可怕的错误,以及在使用直接读取此类输出流的 CM 产品(如 Ansible)时出现一堆垃圾输出的问题。

您可以更改此软件包的默认值,但这将要求您在本地机器上或通过具有镜像服务(例如 Foreman 或 Satellite)的程序重新打包应用程序(在本例中为 nginx)。

我个人解决这个问题的方法是尽可能使用 CentOS,它有一个合理的默认设置“已停止并禁用”。不过,这不是一个非常通用的解决方案。

使用以下步骤重新打包 deb 包:

1:提取 deb 包

`# dpkg-deb -x <package.deb> <dir>`

2:从包中提取控制信息

`# dpkg-deb -e <package.deb> <dir/DEBIAN>`
  1. 完成所有软件包更改后,重新打包 deb

# dpkg-deb -b <dir> <new-package.deb>

  1. 您可以在本地安装此 deb,也可以将其添加到镜像服务(如果有的话)。

另外,这里有一个巧妙的方法来解决这个问题,我厚颜无耻地抄袭了主要.io。它就像一个开关,您可以打开或关闭它来阻止新软件包启动其服务。请记住,这不会“禁用”这些服务,因此重新启动将使它们启动,除非您事先明确将它们设置为禁用:

如果您想阻止守护进程在安装软件包后启动,只需在 /usr/sbin/policy-rc.d 中添加几行:

cat > /usr/sbin/policy-rc.d < < EOF
#!/bin/sh
echo "All runlevel operations denied by policy" >&2
exit 101
EOF

现在,安装您需要的任何软件包,守护进程将保持停止状态,直到您启动它们(或重新启动服务器)。安装完软件包后,请务必删除您添加的策略文件。

如果您将其与 Ansible 之类的工具一起使用,则可以轻松在执行前后打开和关闭此功能。它还可以解决安装 Debian 软件包时出现错误的问题。

相关内容