在 Ubuntu 16.04 上安装 Nginx 时出错

在 Ubuntu 16.04 上安装 Nginx 时出错

我在尝试安装 nginx 时不断收到此错误。我尝试重新安装几次,但此错误不断弹出,我无法启动 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 Mon 2017-03-27 21:38:51 PHT; 10ms ago
  Process: 28178 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=1/FAILURE)
  Process: 28174 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)

Mar 27 21:38:49 nico-Aspire-E5-575G nginx[28178]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Mar 27 21:38:49 nico-Aspire-E5-575G nginx[28178]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Mar 27 21:38:50 nico-Aspire-E5-575G nginx[28178]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Mar 27 21:38:50 nico-Aspire-E5-575G nginx[28178]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Mar 27 21:38:51 nico-Aspire-E5-575G nginx[28178]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Mar 27 21:38:51 nico-Aspire-E5-575G nginx[28178]: nginx: [emerg] still could not bind()
Mar 27 21:38:51 nico-Aspire-E5-575G systemd[1]: nginx.service: Control process exited, code=exited status=1
Mar 27 21:38:51 nico-Aspire-E5-575G systemd[1]: Failed to start A high performance web server and a reverse proxy server.
Mar 27 21:38:51 nico-Aspire-E5-575G systemd[1]: nginx.service: Unit entered failed state.
Mar 27 21:38:51 nico-Aspire-E5-575G systemd[1]: nginx.service: Failed with result 'exit-code'.
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.0-0ubuntu0.16.04.4) | nginx-full (>= 1.10.0-0ubuntu0.16.04.4) | nginx-light (>= 1.10.0-0ubuntu0.16.04.4) | nginx-extras (>= 1.10.0-0ubuntu0.16.04.4); 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.0-0ubuntu0.16.04.4.1~) | nginx-full (<< 1.10.0-0ubuntu0.16.04.4.1~) | nginx-light (<< 1.10.0-0ubuntu0.16.04.4.1~) | nginx-extras (<< 1.10.0-0ubuntu0.16.04.4.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.
                                                                                                          Processing triggers for systemd (229-4ubuntu16) ...
Processing triggers for ureadahead (0.100.0-19) ...
Processing triggers for ufw (0.35-0ubuntu2) ...
Errors were encountered while processing:
 nginx-core
 nginx
E: Sub-process /usr/bin/dpkg returned an error code (1)

答案1

端口 80 上已有一些程序在运行,因此当 nginx 尝试启动并占用该端口时会失败。检查端口 80 上正在运行的程序并将其停止。您可以使用以下命令找出正在使用端口 80 的程序:

sudo netstat -lnp | grep 0.0.0.0:80

找到正在使用该端口的服务后,请执行systemctl stop停止该服务并尝试重新启动 nginx。请记住,您必须永久禁用该其他服务才能使 nginx 正常工作。

答案2

看起来 NGINX 和其他一些服务器应用程序都绑定使用端口 80 作为其默认端口。

简单的解决方案是将 NGINX 端口更改为其他端口,这可以通过编辑/etc/nginx/sites-available/default(或任何配置文件)来完成,以绑定到不同的端口。

寻找类似这样的一行:

Listen 80;

将 80 更改为空闲端口(例如 851):

Listen 851;

重新启动 NGINX,它应该可以再次启动,不会出现问题。

请注意,如果您真的想使用 NGINX 作为您的网络主机,您将需要更改其他网络服务器的配置。

答案3

我遇到这种情况的原因是,在 VirtualBox 上安装虚拟机时,选择了 Debian 9 上的默认选项。我猜 OP 也遇到了类似的事情。

(...)
Choose software to install
[ ] Desktop environment
[*] Web Server
(...)

这将安装 Apache 并立即启用/启动其服务。如果您的发行版使用 systemd,您可以检查是否是这种情况:

sudo systemctl list-units | grep apache

这表明:

apache2.service
      loaded active running   The Apache HTTP Server

您可以停止 Apache 服务:

sudo systemctl stop apache2.service

并且禁用服务以便它不会在下次重启时自动重新启动:

sudo systemctl disable apache2.service

之后你可以重新启动 nginx:

sudo systemctl restart nginx

您可以使用命令行浏览器(例如 Lynx)检查它是否正常工作:

lynx 127.0.0.1

                                    Apache2 Debian Default Page: It works (p1 of 4)
   Debian Logo Apache2 Debian Default Page
   It works!

   This is the default welcome page used to test the correct operation of the
   Apache2 server after installation on Debian systems. (...)

您可能觉得奇怪/困惑的是,Apache 欢迎页面仍然由 nginx 加载。这是因为 Apache 创建了一个默认 html 文件,默认情况下 nginx 指向该文件:/var/www/html/index.html。在我的例子中,默认的 nginx html 欢迎文件是/var/www/html/index.nginx-debian.html。您可以通过修改 nginx 默认配置文件(并使用适当的权限保存它)来更改加载哪个默认页面:

sudo nano /etc/nginx/sites-available/default

(...)
# Default server configuration
#
server {
      (...)
      # original index directive:
      # index index.html index.htm index.nginx-debian.html;
      # modified index directive:
      index index.htm index.nginx-debian.html;
      (...)
}

index.html通过从指令的参数中排除index,可以查找下一个候选文件。在我的例子中,index.htm不存在,因此index.nginx-debian.html使用。此更改直到 nginx 重新加载后才会生效,因此您可以向其发送重新加载信号:

sudo nginx -s reload

现在您可以查看默认的 nginx 欢迎页面:

lynx 127.0.0.1

                                                                 Welcome to nginx!
                                 Welcome to nginx!

   If you see this page, the nginx web server is successfully installed and
   working. Further configuration is required.
   (...)

答案4

根据错误信息:

  Package nginx-full is not installed.
  Package nginx-light is not installed.
  Package nginx-extras is not installed.

我建议你跑sudo apt-get install nginx-full nginx-light nginx-extras

相关内容