OSX - 启动和停止Nginx出现错误

3

我通过Homebrew安装了nginx,但当我尝试启动或停止时出现以下错误:

➜  ~  sudo nginx -s stop
nginx: [error] open() "/usr/local/var/run/nginx.pid" failed (2: No such file or directory)
➜  ~  sudo nginx
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:8080 failed (48: Address already in use)
nginx: [emerg] still could not bind()

文件结构

➜  ~  /usr/local/var/run
➜  run git:(master) ls
nginx
➜  run git:(master) cd nginx
➜  nginx git:(master) ls
client_body_temp fastcgi_temp     proxy_temp       scgi_temp        uwsgi_temp
➜  nginx git:(master)

检查nginx是否正在运行

➜  ~  ps waux | grep nginx
harshamv          963   0.0  0.0  2432772    656 s000  S+    1:32PM   0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=.cvs --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn nginx

当我尝试运行Nginx时,出现以下错误。
➜  ~  nginx
nginx: [alert] could not open error log file: open() "/usr/local/var/log/nginx/error.log" failed (13: Permission denied)
2015/07/02 13:39:44 [emerg] 1074#0: open() "/usr/local/var/log/nginx/access.log" failed (13: Permission denied)

无法启动程序,因为8080端口已被占用(可能是由某个进程打开的,但看起来不像是nginx)。pgrep nginx命令可以告诉您nginx是否仍在运行,而lsof -i 4tcp |grep 8080命令则可以告诉您如果不是nginx,则哪个进程持有8080端口。 - Leonid Shevtsov
@LeonidShevtsov 是的,nginx正在8080端口运行,我该如何停止它? - Harsha M V
当我运行那些命令时,没有显示任何内容。@LeonidShevtsov - Harsha M V
请运行 sudo netstat -lnt | grep :8080。 如果输出为空,则没有任何东西占用端口8080。您是否运行了上面建议的 lsof 命令?如果 nginx 正在运行,则可以将其杀死,因为 pid 文件丢失,因此启动/停止脚本将无法停止它。 - alvits
当我访问localhost:8080时,会出现欢迎使用Nginx的界面。当我运行sudo netstat -lnt | grep :8080lsof -i 4tcp |grep 8080时,它只是回到了命令提示符行。 - Harsha M V
显示剩余10条评论
2个回答

0

我曾经遇到过这样的情况。如果你使用类似于 nginx -c my.conf 的配置文件启动 nginx,那么你必须使用 nginx -s stop -c my.conf 命令来停止它。

在你的 my.conf 文件中,你可以使用 pid /some_location/nginx.pid;,这将在该位置生成一个 pid 文件,而不再是默认位置 /usr/local/var/run/nginx.pid,这就是为什么你无法停止它的原因。

我认为当你遇到这种错误时,你可以使用 ps -ef | grep nginx 命令查找其 pid,并手动杀死它。


-4

Mac OS X EI在/usr目录下没有权限

解决方法:

sudo chown -R username run

chmod -R 777 run/

如果出现其他权限问题,例如“xxx”权限,请使用“chown”和“chmod”命令。

通过这种方式,我的问题得到了解决!


2
这是非常糟糕的建议。chmod 777永远不是正确的解决方案。 - Suever
是的,你说得对。但在我的个人电脑上,chmod 777只是为了测试。在服务器端,你可以设置严格的权限。 - 石卫军

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接