如何阻止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的configuration文件。 我怎样才能阻止安装后自动启动,以便我的apt-get install命令不会失败。

在基于Debian的发行版中,启动并启用安装服务的不幸默认值。 这通常会导致在安装不包含自己的端到端默认configuration的服务时出现可怕的错误,以及在使用直接读取这样的输出stream的CM产品时的一堆垃圾输出,例如Ansible。

你可以改变这个软件包的默认值,但是它会要求你在本地机器上重新打包应用程序(在这种情况下是nginx),或者通过一些镜像服务(如Foreman或者Satellite)重新打包。

我个人通过使用CentOS来解决这个问题,它有一个合理的默认值“停止和禁用”。 虽然这不是一个通用的解决scheme。

使用以下步骤重新打包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,或将其添加到镜像服务(如果有的话)。

或者,这里是一个聪明的方法来解决这个问题,我无耻地从major.io撕裂 。 它就像一个开关,您可以打开和closures,以防止新包开始他们的服务。 请记住,这不会“禁用”这些服务,所以重新启动将使其启动,除非您事先明确地将其设置为禁用:

如果你想阻止守护进程在安装包之后启动,只需要在/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这样的东西,这将很容易打开和closures前后执行。 它也将停止安装Debian软件包时出现的丑陋的错误问题。