1. 安裝(可以用 brew 安裝)
sudo brew install nginx
2. 查看 nginx 版本
nginx -v
3. 啟動 nginx
sudo nginx
也可以使用下面的命令啟動,但是配置文件nginx.conf修改后用這個命令執(zhí)行不生效,故不建議使用:
sudo brew services start nginx
4. 查看 nginx 是否啟動成功
在瀏覽器中訪問 http://localhost:8080感凤,如果出現(xiàn)如下界面,則說明啟動成功.
備注:端口號是在配置文件 nginx.conf 里面配置的旺订,默認端口是 8080 该编,配置文件的位置 /usr/local/etc/nginx
5. 關(guān)閉nginx
sudo nginx -s stop
也可以使用下面的命令啟動,但是配置文件nginx.conf修改后用這個命令執(zhí)行不生效爷辙,故不建議使用:
sudo brew services stop nginx
6. 重新加載nginx
sudo nginx -s reload
7. 可能遇到的問題
-
端口被占用
nginx: [emerg] bind() to 0.0.0.0:80 failed (48: Address already in use)
解決方法:修改 nginx.conf 文件里的端口號
-
權(quán)限不夠
nginx: [alert] could not open error log file: open() “/usr/local/var/log/nginx/error.log” failed (13: Permission denied)
解決方法:在命令前加上 sudo,這時可能會要求輸入密碼朦促,密碼就是電腦的開機密碼啦~
cd /usr/local/cellar/nginx/1.12.1/bin
? bin sudo brew services start nginx
bin chmod a+x ./nginx
chmod: Unable to change file mode on ./nginx: Operation not permitted
//將nginx文件添加權(quán)限
? bin sudo chmod a+x ./nginx
? bin sudo ./nginx
nginx: [emerg] getgrnam("root") failed in /usr/local/etc/nginx/nginx.conf:2
//修改nginx.conf文件膝晾,然后重新啟動nginx
? 1.12.1 cd bin
? bin sudo ./nginx
? bin sudo nginx -s reload
? bin sudo nginx -s stop
8. 補充
安裝 homebrew ,將以上命令粘貼至terminal务冕,然后回車即可
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
常用的指令有:
nginx -s reload 重新加載配置
nginx -s reopen 重啟
nginx -s stop 停止
nginx -s quit 退出
nginx -V 查看版本血当,以及配置文件地址
nginx -v 查看版本
nginx -c filename 指定配置文件
nginx -h 幫助