Mac安装Nginx
检查nginx是否安装成功
bash
nginx -v或使用 which 命令确认安装路径:
bash
which nginx如果输出类似 /opt/homebrew/bin/nginx 或 /usr/local/bin/nginx,说明已安装。
安装nginx
Mac 上推荐使用 Homebrew 进行安装。如果尚未安装 Homebrew,先执行:
bash
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"然后安装 nginx:
bash
brew install nginxHomebrew 会自动处理 nginx 所需的依赖(如 pcre、zlib、openssl)。
启动nginx
bash
brew services start nginx或手动启动:
bash
nginx在浏览器里输入 http://localhost:8080 将显示 Welcome to nginx! 页面,说明 nginx 安装成功。
注意:Homebrew 安装的 nginx 默认端口是 8080,而非 80,目的是无需 sudo 即可运行。
配置Nginx
配置文件目录
修改 nginx 的默认配置文件:
- Intel 芯片 Mac:
/usr/local/etc/nginx/nginx.conf - Apple Silicon 芯片 Mac(M1/M2/M3):
/opt/homebrew/etc/nginx/nginx.conf
可以使用 nano 或 vi 编辑:
bash
nano /opt/homebrew/etc/nginx/nginx.conf常用目录说明(Apple Silicon 路径)
| 路径 | 说明 |
|---|---|
/opt/homebrew/etc/nginx | 存放配置文件 |
/opt/homebrew/var/www | 存放静态文件(默认网页根目录) |
/opt/homebrew/var/log/nginx | 存放日志 |
/opt/homebrew/etc/nginx/servers/ | 虚拟主机配置目录(自动加载该目录下所有文件) |
常用服务管理命令
bash
# 启动
brew services start nginx
# 停止
brew services stop nginx
# 重载配置
nginx -s reload
# 重启
brew services restart nginx
# 测试配置语法
nginx -s reload
nginx -t