Nginx权限拒绝错误求助:本地多应用部署故障排查
本地多应用Web服务器部署故障排查
环境与问题
- 系统:Mac Monterey 12.7.6(Intel)
- 组件:Nginx、PHP 7.4(shivammathur/php/php@7.4)
- 部署目标:
- Yii 1(路径
/Users/ademugnirusmana/Project/www/wms)→www.wms.local - Laravel 7(路径
/Users/ademugnirusmana/Project/www/middleware)→www.middleware.local - CodeIgniter 3(路径
/Users/ademugnirusmana/Project/www/reporting)→www.reporting.local
- Yii 1(路径
- 故障现象:在Yii项目目录执行
php -S localhost:8080正常,但浏览器访问www.wms.local时显示“File not found”。
相关配置信息
Nginx配置(Yii示例)
server { listen 80; server_name www.wms.local; root /Users/ademugnirusmana/Projects/www/wms; index index.php; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include fastcgi_params; fastcgi_pass 127.0.0.1:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_index index.php; } error_page 404 /404.html; location = /404.html { root /usr/share/nginx/html; } }
Hosts配置
## # Host Database # # localhost is used to configure the loopback interface # when the system is booting. Do not change this entry. ## #127.0.0.1 localhost 255.255.255.255 broadcasthost ::1 localhost 127.0.0.1 www.wms.local #127.0.0.1 www.reporting.local #127.0.0.1 www.middleware.local
PHP-FPM配置(usr/local/etc/php/7.4/php-fpm.d/www.conf)
listen = 127.0.0.1:9000
Nginx进程信息(ps aux| grep nginx)
ademugnirusmana 16744 0.0 0.0 34130864 468 s000 S+ 11:24am 0:00.01 tail -f /usr/local/var/log/nginx/error.log nobody 15649 0.0 0.0 34161744 1396 ?? S 11:18am 0:00.01 nginx: worker process root 9749 0.0 0.0 34161536 1544 ?? Ss 10:24am 0:00.01 nginx: master process nginx ademugnirusmana 17616 0.0 0.0 34141260 916 s001 S+ 11:35am 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox --exclude-dir=.venv --exclude-dir=venv nginx
目录权限(Yii示例)
drwxr-xr-x 18 nobody nobody 576 25 Nov 10:52 /Users/ademugnirusmana/Projects/www/wms
Nginx错误日志
2024/11/25 11:29:34 [crit] 15649#0: *52 stat() "/Users/ademugnirusmana/Projects/www/wms/" failed (13: Permission denied), client: 127.0.0.1, server: www.wms.local, request: "GET / HTTP/1.1", host: "www.wms.local" 2024/11/25 11:29:34 [error] 15649#0: *52 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 127.0.0.1, server: www.wms.local, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.wms.local"
PHP-FPM日志
[25-Nov-2024 11:40:35] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root [25-Nov-2024 11:40:35] NOTICE: [pool www] 'user' directive is ignored when FPM is not running as root [25-Nov-2024 11:40:35] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root [25-Nov-2024 11:40:35] NOTICE: [pool www] 'group' directive is ignored when FPM is not running as root [25-Nov-2024 11:40:35] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48) [25-Nov-2024 11:40:35] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (48) [25-Nov-2024 11:40:35] ERROR: FPM initialization failed [25-Nov-2024 11:40:35] ERROR: FPM initialization failed
故障排查与修复方案
1. 解决Nginx目录权限问题
从Nginx错误日志的Permission denied可知,Nginx worker进程以nobody用户运行,无法访问你的用户目录。Mac用户目录默认限制了其他用户的访问权限,可通过以下两种方式修复:
- 修改Nginx运行用户:编辑Nginx主配置文件(通常是
/usr/local/etc/nginx/nginx.conf),将user nobody;改为user ademugnirusmana staff;,然后执行sudo nginx -s reload重启Nginx。 - 开放目录权限:执行命令
chmod +a "nobody allow execute,list,search" /Users/ademugnirusmana,临时开放用户目录的基础访问权限。
2. 解决PHP-FPM端口占用问题
PHP-FPM日志显示Address already in use,说明9000端口已被其他进程占用:
- 查找占用进程:
lsof -i :9000 - 杀死进程:
kill -9 <进程PID>(替换<进程PID>为查询到的进程ID) - 重启PHP-FPM:
brew services restart php@7.4(若使用brew安装PHP)
3. 验证路径一致性
检查Nginx配置中的项目路径:问题描述里Yii路径是/Users/ademugnirusmana/Project/www/wms,但Nginx配置中是/Users/ademugnirusmana/Projects/www/wms(多了一个s),确认实际目录路径,修正拼写错误。
4. 确认PHP-FPM运行状态
重启PHP-FPM后,执行ps aux | grep php-fpm,确认有php-fpm进程在运行。
内容的提问来源于stack exchange,提问作者Ade Mugni Rusmana
相关产品推荐
相关产品推荐

