Laravel Valet安装后Nginx无法启动问题排查求助
Laravel Valet 安装后Nginx启动失败问题排查与解决方案
问题描述
我在MacBook上按照官方文档步骤安装Laravel Valet,安装完成后发现80端口无法访问,Nginx服务未正常运行,Valet依赖brew安装Nginx、PHP等必要依赖包。
执行valet install命令返回结果如下:
➜ valet valet install Stopping nginx... Installing nginx... [nginx] is not installed, installing it now via Brew... 🍻 Installing nginx configuration... Installing nginx directory... Updating PHP configuration... Restarting php... Installing dnsmasq... [dnsmasq] is not installed, installing it now via Brew... 🍻 Updating Dnsmasq configuration... Restarting dnsmasq... Valet is configured to serve for TLD [.test] Restarting nginx... Valet installed successfully!
但安装成功后Nginx并未运行,执行sudo brew services list结果如下:
sudo brew services list 256 Name Status User File dnsmasq started root nginx error root php started root
执行配置语法检测sudo nginx -t返回正常:
sudo nginx -t nginx: the configuration file /opt/homebrew/etc/nginx/nginx.conf syntax is ok nginx: configuration file /opt/homebrew/etc/nginx/nginx.conf test is successful
查看Nginx错误日志得到如下报错:
2021/11/27 19:30:41 [emerg] 73996#0: unexpected end of parameter, expecting ";" in command line /opt/homebrew/var/log/nginx/error.log (END)
该报错未明确指向问题根源,仅提示参数意外结束缺少分号,无法定位来源。查询进程列表确认无残留Nginx进程:
ps aux | grep nginx ahoi 74430 99,6 0,1 408788096 24496 s002 R+ 7:37pm 1:13.64 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn --exclude-dir=.idea --exclude-dir=.tox -r nginx . --exclude-dir=Entwicklung ahoi 74553 0,0 0,0 408628336 1632 s001 S+ 7:38pm 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 nginx
执行valet diagnose返回的系统、Valet版本、Homebrew、PHP、DNS等所有配置检测结果均无异常。
排查进展
我已定位到问题可能来源:每次重启Nginx服务时,/Library/LaunchDaemons/homebrew.mxcl.nginx.plist文件都会被自动篡改。
手动修改为正确配置的plist内容如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>homebrew.mxcl.nginx</string> <key>ProgramArguments</key> <array> <string>/opt/homebrew/opt/nginx/bin/nginx</string> <string>-g</string> <string>daemon off;</string> </array> <key>RunAtLoad</key> <true/> <key>WorkingDirectory</key> <string>/opt/homebrew</string> </dict> </plist>
但重启服务后该文件会被自动改写为错误格式:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>homebrew.mxcl.nginx</string> <key>ProgramArguments</key> <array> <string>/opt/homebrew/opt/nginx/bin/nginx</string> <string>-g</string> <string>'daemon off;'</string> </array> <key>RunAtLoad</key> <true/> <key>WorkingDirectory</key> <string>/opt/homebrew</string> </dict> </plist>
可见错误配置中将daemon off;额外包裹了HTML实体编码的单引号,导致Nginx启动参数解析失败完全无法运行。
解决方案
该问题是旧版本Homebrew services的已知缺陷,解析启动参数时会错误添加不必要的转义字符,按以下步骤操作即可永久修复:
- 首先完全停止当前的Nginx服务,卸载已注册的plist启动项:
sudo brew services stop nginx sudo rm /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
- 更新Homebrew core和services组件到最新版本:
brew update brew upgrade homebrew-services
- 重新注册Nginx服务:
sudo brew services start nginx
- 验证修复结果:执行
sudo brew services list确认Nginx状态变为started,执行curl localhost能正常返回Nginx默认页面或Valet的默认站点内容即可。 - 额外校验:打开
/Library/LaunchDaemons/homebrew.mxcl.nginx.plist,确认daemon off;参数外层没有多余的单引号转义,之后无论重启服务还是系统都不会再被自动篡改。
内容的提问来源于stack exchange,提问作者SPQRInc
相关产品推荐
相关产品推荐

