【问题标题】:Nginx.pid Permission DeniedNginx.pid 权限被拒绝
【发布时间】:2018-11-27 19:50:04
【问题描述】:

我正在通过 brew 使用 nginx,它似乎已配置并正常工作 - 在一定程度上。我有多个主机文件(大约 20 个站点),几乎所有这些文件都可以正常工作,但是,添加的任何新站点都将无法正常工作。

所有旧主机文件似乎都可以正常加载,例如在端口 80 上使用服务器名称 site.test,但添加的任何新主机文件在我的网络浏览器中返回“找不到服务器”。

经过多次故障排除和 Google 搜索,我终于在我的设置中发现了一些奇怪的东西,正是这篇文章触发了它:nginx.conf and nginx.pid users and permissions。虽然它似乎并没有解决我的确切问题。

当我运行nginx -t 时,我得到:

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

但是,当我运行 sudo nginx -t 时,我得到:

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

我已经杀死了 nginx 并使用 brew 优雅地停止它并使用 sudo 重新启动它,但我似乎遇到了与 nginx.pid 相同的问题。

当我启动 nginx 时,我使用:sudo brew services start nginx

运行ps aux | grep nginx,返回:

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;

这应该是正确的,因为我的用户在 nginx.conf 中设置为user Media32 staff

我可以提供任何其他必要的代码,但我已经为此不知所措好几天了,我们将不胜感激。

更新

我似乎已经通过编辑/etc/hosts 并为新主机文件添加记录来解决这个问题,即。 127.0.0.1 site.test 然后在重新加载 nginx 之后它就可以工作了,但是我以前从来没有这样做过,任何人都可以解释为什么需要这样做并且之前不需要这样做吗?

【问题讨论】:

  • 我认为应该与您的更新无关。这是您的计算机解析主机的方式。只需将 nginx 服务器指向 127.0.0.1。

标签: nginx


【解决方案1】:

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

sudo brew services stop nginx

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

sudo brew services start nginx

nginx -t

【讨论】:

  • 警告:nginx 必须以非 root 身份运行才能在用户登录时启动!但我忽略了这一点并且工作正常。
  • nginx: [warn] 冲突的服务器名称 "kartify.loc" on 0.0.0.0:80, 忽略 nginx: 配置文件 /usr/local/etc/nginx/nginx.conf 语法没问题 nginx : 配置文件 /usr/local/etc/nginx/nginx.conf 测试成功 bug nginx is working
【解决方案2】:

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

    127.0.0.1 mysite.test 

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

在我的情况下,我得到了一个

 the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /usr/local/etc/nginx/nginx.conf:2

"/usr/local/var/run/nginx.pid" failed (13: Permission denied)

bind() to 0.0.0.0:80 failed (48: Address already in use)

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

  1. 停止根进程

    sudo nginx -s stop
    
  2. 检查进程是否停止

    ps aux | grep nginx
    
  3. 重启进程

    sudo nginx -s reload
    

给了我错误

nginx: [error] open() “/usr/local/var/run/nginx.pid” failed (2: No such file or directory)

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

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

    brew services start nginx
    
  2. 运行命令的结果

    ps aux | grep nginx
    
    youruser 89212 0.0 0.0 4268280 644 s002  S+ 2:46PM 0:00.00 grep nginx
    youruser 89179 0.0 0.0 4302204 1776 ?? S 2:45PM 0:00.00 nginx: worker process  
    youruser 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;
    

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

【讨论】:

    【解决方案3】:

    确保您使用“sudo nginx -t”以 root 用户身份运行 nginx -t,因为配置检查器需要 root 权限。

    (这解决了我的问题)

    【讨论】:

      猜你喜欢
      • 2015-08-30
      • 1970-01-01
      • 2018-07-19
      • 2018-02-05
      • 2015-04-28
      • 2018-02-03
      • 2012-04-14
      相关资源
      最近更新 更多