Mac 上 Nginx 设置开机自启动
问题表现
- 拷贝启动配置文件
sudo cp /usr/local/Cellar/nginx/1.21.6_1/homebrew.mxcl.nginx.plist /Library/LaunchDaemons
- 设置开机启动
sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.nginx.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>/usr/local/opt/nginx/bin/nginx</string>
<string>-g</string>
<string>daemon off;</string>
</array>
<key>RunAtLoad</key>
<true/>
<key>WorkingDirectory</key>
<string>/usr/local</string>
</dict>
</plist>
此时查看服务列表, 发现nginx未启动
sudo brew services lis
# 显示内容
# Name Status User File
# dnsmasq started root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
# httpd none
# nginx error 256 root /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
# php none root
# php@7.1 none root
# php@7.4 none root
# redis none root
重启之后, nginx正常工作
sudo brew services lis
# 显示内容
# Name Status User File
# dnsmasq started root /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist
# httpd none
# nginx started root /Library/LaunchDaemons/homebrew.mxcl.nginx.plist
# php none root
# php@7.1 none root
# php@7.4 none root
# redis none root
注意:
操作 brew
的时候要带上 sudo
sudo brew services stop nginx
sudo brew services start nginx
sudo brew services restart nginx
获取帮助
联系邮箱: nilsir@qq.com
联系微信: xiaoshilitong