Nginx.pid Permission Denied

J. *_*son 8 nginx

I am using nginx through brew and it appears to be configured and working correctly - to a point. I have multiple host files (around 20 sites) and almost all of them work fine, however, any new sites added will not work.

All old host files seem to load just fine, for example with server name site.test on port 80, but any new host files added return with a "Server not found" in my web browsers.

After much troubleshooting and Google searching, I've finally found something that seems odd in my setup and it was this post that triggered it: nginx.conf and nginx.pid users and permissions. Although it doesn't seem to solve my exact problem.

When I run nginx -t I get:

nginx: [warn] the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/etc/nginx/nginx.conf:1
nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)
nginx: configuration file /usr/local/etc/nginx/nginx.conf test failed
Run Code Online (Sandbox Code Playgroud)

However, when I run sudo nginx -t I get:

nginx: the configuration file /usr/local/etc/nginx/nginx.conf syntax is ok
nginx: configuration file /usr/local/etc/nginx/nginx.conf test is successful
Run Code Online (Sandbox Code Playgroud)

I have both killed nginx and stopped it gracefully with brew and restarted it with sudo, but I appear to get the same issue with nginx.pid.

When I start nginx, I use: sudo brew services start nginx

Running ps aux | grep nginx, returns:

Media32           7444   0.0  0.0  4339424   2016   ??  S    12:16pm   0:00.01 nginx: worker process  
Media32           7443   0.0  0.0  4339424   1836   ??  S    12:16pm   0:00.00 nginx: worker process  
root              7440   0.0  0.0  4297440   4876   ??  Ss   12:16pm   0:00.01 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;
Run Code Online (Sandbox Code Playgroud)

Which should be correct as my user is setup in nginx.conf to be user Media32 staff.

I can provide any other code necessary, but I have been at a bit of a loss for days on this now and any help would be appreciated.

Update

I appear to have solved this by editing /etc/hosts and adding a record for the new host files, ie. 127.0.0.1 site.test and then after reloading nginx it worked, but I have never had to do this before, can anyone shed any light on why this is needed and wasn't needed prior?

Nan*_*mar 7

我已经在 macOS Catalina 中解决了这个问题。

sudo brew services stop nginx

sudo chmod 777 /usr/local/var/run/nginx.pid

sudo brew services start nginx

nginx -t
Run Code Online (Sandbox Code Playgroud)


小智 0

除了将 /private/etc/hosts 中的任何新主机设置为

\n\n
    127.0.0.1 mysite.test \n
Run Code Online (Sandbox Code Playgroud)\n\n

是本地添加新主机域时的规则,

\n\n

就我而言,我得到了一个

\n\n
 the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/etc/nginx/nginx.conf:2\n\n"/usr/local/var/run/nginx.pid" failed (13: Permission denied)\n\nbind() to 0.0.0.0:80 failed (48: Address already in use)\n
Run Code Online (Sandbox Code Playgroud)\n\n

工作解决方案由以下步骤组成:

\n\n
    \n
  1. 停止根进程

    \n\n
    sudo nginx -s stop\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
  3. 检查进程是否停止

    \n\n
    ps aux | grep nginx\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
  5. 重新启动进程

    \n\n
    sudo nginx -s reload\n
    Run Code Online (Sandbox Code Playgroud)
  6. \n
\n\n

给了我错误

\n\n
nginx: [error] open() \xe2\x80\x9c/usr/local/var/run/nginx.pid\xe2\x80\x9d failed (2: No such file or directory)\n
Run Code Online (Sandbox Code Playgroud)\n\n

probabil .pid 是用错误的 root 用户启动的,因为我取消了 /usr/local/etc/nginx/nginx.conf 中 .pid 路径的行的注释,然后我再次将其注释掉

\n\n
    \n
  1. 以用户而非 root 身份启动 nginx

    \n\n
    brew services start nginx\n
    Run Code Online (Sandbox Code Playgroud)
  2. \n
  3. 运行命令的结果

    \n\n
    ps aux | grep nginx\n\nyouruser 89212 0.0 0.0 4268280 644 s002  S+ 2:46PM 0:00.00 grep nginx\nyouruser 89179 0.0 0.0 4302204 1776 ?? S 2:45PM 0:00.00 nginx: worker process  \nyouruser 89178 0.0 0.0  4275372 4368 ?? S 2:45PM 0:00.01 nginx: master process /usr/local/opt/nginx/bin/nginx -g daemon off;\n
    Run Code Online (Sandbox Code Playgroud)
  4. \n
\n\n

可以看出,nginx 进程以预期用户身份启动,而不是以 root 身份启动,进程之间的冲突消失了,我可以访问 PHP 应用程序本地域。

\n