我在 Ubuntu 18.04 上安装了 nginx。之后,我在默认文件中做了一些更改。现在我尝试启动它,但出现以下错误:
$ sudo service nginx restart
Job for nginx.service failed because the control process exited with
error code. See "systemctl status nginx.service" and "journalctl -xe"
for details.
$ systemctl status nginx.service
nginx.service - A high performance web server and a reverse proxy
server Loaded: loaded (/lib/systemd/system/nginx.service; enabled;
vendor preset: en Active: failed (Result: exit-code) since Sat
2019-05-25 13:35:03 CDT; 19min a
Docs: man:nginx(8) Process: 3220 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code Process: 3219
ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process
May 25 13:35:01 lc2530hz nginx[3220]: nginx: [emerg] bind() to
0.0.0.0:80 failed May 25 13:35:01 lc2530hz nginx[3220]: nginx: [emerg] bind() to [::]:80 failed (9 May 25 13:35:02 lc2530hz nginx[3220]:
nginx: [emerg] bind() to 0.0.0.0:80 failed May 25 13:35:02 lc2530hz
nginx[3220]: nginx: [emerg] bind() to [::]:80 failed (9 May 25
13:35:02 lc2530hz nginx[3220]: nginx: [emerg] bind() to 0.0.0.0:80
failed May 25 13:35:02 lc2530hz nginx[3220]: nginx: [emerg] bind() to
[::]:80 failed (9 May 25 13:35:03 lc2530hz nginx[3220]: nginx: [emerg]
still could not bind() May 25 13:35:03 lc2530hz systemd[1]:
nginx.service: Control process exited, code May 25 13:35:03 lc2530hz
systemd[1]: nginx.service: Failed with result 'exit-cod May 25
13:35:03 lc2530hz systemd[1]: Failed to start A high performance web
serv lines 1-17/17 (END)
$ journalctl -xe
Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit phpsessionclean.service has finished starting up.
--
-- The start-up result is RESULT.
May 25 13:42:32 lc2530hz systemd[1]: Starting Message of the Day...
-- Subject: Unit motd-news.service has begun start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit motd-news.service has begun starting up.
May 25 13:42:32 lc2530hz systemd[1]: Started Message of the Day.
-- Subject: Unit motd-news.service has finished start-up
-- Defined-By: systemd
-- Support: http://www.ubuntu.com/support
--
-- Unit motd-news.service has finished starting up.
--
-- The start-up result is RESULT.
May 25 13:42:53 lc2530hz org.gnome.Shell.desktop[1989]: (/usr/lib/firefox/firefo
May 25 13:42:58 lc2530hz systemd-resolved[782]: Server returned error NXDOMAIN,
May 25 13:47:19 lc2530hz /usr/lib/gdm3/gdm-x-session[1855]: (II) event5 - PixAr
lines 1576-1598/1598 (END)
$ nginx -t
nginx: [alert] could not open error log file: open()
"/var/log/nginx/error.log" failed (13: Permission denied) 2019/05/25
14:01:03 [warn] 3564#3564: the "user" directive makes sense only if
the master process runs with super-user privileges, ignored in
/etc/nginx/nginx.conf:1 nginx: the configuration file
/etc/nginx/nginx.conf syntax is ok 2019/05/25 14:01:03 [emerg]
3564#3564: open() "/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /etc/nginx/nginx.conf test failed
zulfi@lc2530hz:/etc/nginx/sites-enabled$
/etc/nginx/sites-enabled$ cat default
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##
Default server configuration
server {
listen 80 default_server;
listen [::]:80 default_server;
# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
#
include snippets/snakeoil.conf;
root /var/www/html;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
#server_name_;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/=404;
}
# pass PHP scripts to FastCGI server
# location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# With php-fpm (or other unix sockets):
# fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
# With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one #
#location ~ /\.ht {
# deny all;
#}
}
# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#} user@lc2530hz:/etc/nginx/sites-enabled$
请有人指导我如何重新启动 nginx。
答案1
看起来好像你的 80 端口被阻止了。(nginx: [emerg] bind() to 0.0.0.0:80 failed
)尝试:
sudo netstat -lntp | grep ":80"
找出哪个进程阻塞了端口,终止该程序/服务并重新启动 nginx。
我还建议您以 nginx 运行的用户身份运行nginx -t
,因此
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed
明确指示写入日志文件是否确实存在问题。如果您没有与用户相同的访问权限,则以标准用户身份运行此操作可能会导致不正确的结果http
。
答案2
首先验证配置文件是否有效:
sudo nginx -t
无需重新启动 Nginx 服务即可应用更改:
sudo nginx -s reload
如果更改未立即应用,请使用以下命令重新启动服务(与上一步中使用的命令不同,它将突然关闭所有 Web 客户端连接):
sudo systemctl restart nginx
在极端情况下,您可以终止该进程并重新启动:
sudo pkill -9 nginx && sudo systemctl start nginx
在某些情况下,Nginx 可能无法重新启动,因为某些子进程或其他进程(Apache 或 httpd)正在使用端口 80 或 443。在这些情况下,我建议停止 Nginx(sudo systemctl stop nginx
)并验证是否有任何进程继续在端口 80 上监听:
sudo netstat -ant | grep ": 80"
您可以通过以下方式查看更多详细信息:
sudo lsof -i -P -n | grep ": 80"
为了更轻松地查看正在运行的进程,您可以使用htop
。
当没有人使用 TCP/80 端口时,你可以重新启动 Nginx:
sudo systemctl start nginx
答案3
尝试这个,以 root 用户身份检查服务,禁用,再次启用并启动。
sudo -i
systemctl check nginx
systemctl disable nginx
systemctl enable nginx
systemctl start nginx