安装
先安装brew再安装nginx
brew
注意下面的所有 /opt/homebrew/, 应该根据自己电脑实际情况调整:
如intel的电脑 改成 /usr/local/opt/
- /opt/homebrew/bin 改成 usr/bin
- /opt/homebrew/sbin 改成 usr/sbin
# 安装 Homebrew,官网 brew.sh
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 根据输出可能需要将一两句话追加到你的 ~/.zshrc 去,可能是 ~/.bash_profile
echo 'export PATH="usr/bin:$PATH"' >> ~/.zshrc
echo 'export PATH="usr/sbin:$PATH"' >> ~/.zshrc
# 重新加载 profile 文件
source ~/.zshrc # 可能是 ~/.bash_profile
# 关于 profile 文件名的选择,和修改之后需要重新加载 profile 文件,以后会用很多次,不会再专门提及了
# 查看版本
brew -v
# 使用 brew 来安装两个软件试试
# wget,mac 竟然没有自带这个命令
brew install wget
wget --version
# git
# mac 是自带 git 的,第一次在命令行输入 git,会提示还未安装,然后开始安装
# 也可以使用 brew 来安装,使用 brew 提供的
brew install git
# 查看 git 的路径,如果看到 /opt/homebrew/bin/git 证明当前使用的是 brew 安装的
which git
# 查看版本
git --version
nginx安装
brew install nginx
# 查看版本
nginx -v
# 启动 nginx
brew services start|stop|restart nginx # start 会使得 php-fpm 变为开机自启
# 访问 localhost:8080 验证,会输出 “It works!”
# nginx 是一个 web 服务器,默认会启动一个 web server,占用 8080 端口
nginx使用
# 查看版本
nginx -v
# 启动 nginx 注意mac下不建议使用下面命令,
brew services start|stop|restart nginx # start 会使得 php-fpm 变为开机自启
# 推荐使用nginx 直接操作
nginx -s reload # 直接热加载
# 一般生成的地址
nginx 配置地址 /usr/local/etc/nginx/
# 部署html地址
/opt/homebrew/var/www
# 配置部署路径
/opt/homebrew/etc/nginx/nginx.conf
/opt/homebrew/etc/nginx/servers/
# 其他启动命令
/opt/homebrew/opt/nginx/bin/nginx -g daemon off
系统修改host
# sudo vim /etc/hosts
127.0.0.1 test.my.com
127.0.0.1 test2.my.com
踩过的坑
文件权限问题:
1.*147 open() failed (13: Permission denied)
2023/12/27 18:56:38 [error] 6049#0: *147 open() "/Users/xxx/Desktop/xxxxxx/public/static/images/web/xxx.jpg" failed (13: Permission denied), client: 127.0.0.1, server: dev.xxx.com, request: "GET /static/images/web/xxx.jpg HTTP/1.1", host: "dev.xxx.com", referrer: "http://dev.lostmary.com/"
由于brew安装,很多时候都需要sudo 才有足够的权限启动,但是sudo相当于root启动,会导致文件权限可能访问时的问题。
sudo nginx # 错误启动的方式
nginx # 正确的方式
所以最好的办法就是启动nginx的时候不要用sudo切换root,尽量保持当前用户
修改配置,让当前用户所在的admin 有足够的权限启动,并且不提示密码登陆
打开免密:
打开root
- 打开“系统偏好设置”,进入“用户与群组”面板,记得把面板左下角的小锁打开
- 然后选择面板里的“登录选项”,在面板右边你会看到“网络账户服务器”,点击它旁边的“加入…”按钮。
- 再点击弹出的窗口中“打开目录实用工具”。这时会弹出一个新窗口,把左下角的小锁打开
- 然后点击菜单栏上的“编辑” – “启用 Root 用户” 设置免密
sudo chmod u+w /etc/sudoers # 加入写权限
#%admin ALL=(ALL) ALL # 替换为
%admin ALL=(ALL) NOPASSWD: ALL
sudo chmod u-w /etc/sudoers # 还原
2. nginx: [emerg] open() "/usr/local/var/run/nginx.pid" failed (13: Permission denied)
./nginx 启动
提示是由于文件权限不够,此时千万不要用加 sudo,会导致上面1.权限问题,只需要给文件赋予权限即可
sudo chmod -R 777 /usr/local/var/run
3.不要使用 brew services restart nginx 更新配置
使用brew命令 配置不会热加载,建议还是老老实实的用回 nginx自带的命令
#启动服务
nginx
#测试配置是否正常
nginx -t
#重新加载
nginx -s reload
#关闭服务
nginx -s stop
#查看nginx服务是否启动成功
ps -ef | grep nginx
4.[emerg] open() "/opt/homebrew/Cellar/nginx/1.25.3/logs/error.log" failed (2: No such file or directory)
nginx.conf
worker_processes 1;
error_log logs/error.log; # 如果打开了默认配置
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
由于在 /opt/homebrew/Cellar/nginx/1.25.3/下面默认是没有 logs文件夹,导致进入不了log
# 方法1 新增这个文件夹
mkdir /opt/homebrew/Cellar/nginx/1.25.3/logs
# 方法2 用回自己指定的路径,建议跟nginx conf配置在一起
mkdir /opt/homebrew/etc/nginx/logs
# 修改 nginx.conf
worker_processes 1;
error_log /opt/homebrew/etc/nginx/error.log; # 建议跟nginx conf配置在 同一个路径
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}