用 Homebrew 在 macOS 上安装 Nginx 仅需 3–5 分钟:先执行 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 安装 Homebrew,再运行 brew install nginx 和 brew services start nginx 启动服务,最后访问 http://localhost:8080 验证成功;默认根目录和配置路径按芯片区分(Apple Silicon 为 /opt/homebrew/var/www/html 和 /opt/homebrew/etc/nginx/nginx.conf,Intel 为 /usr/local/var/www/html 和 /usr/local/etc/nginx/nginx.conf),修改后用 brew services restart nginx 重载。
用 homebrew 在 macos 上装 nginx 很快,3–5 分钟就能跑起来,不用编译、不碰 sudo(除非改端口),适合日常开发和本地测试。
安装 Homebrew(如果还没装)
打开终端,粘贴执行:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"- 安装完运行
brew --version确认成功;若提示缺 Xcode 命令行工具,按提示执行xcode-select --install - M 系列芯片用户建议检查 PATH:运行
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> ~/.zshrc && source ~/.zshrc,确保 brew 命令可用
安装并启动 Nginx
执行三条命令即可:
-
brew install nginx—— 安装完成会显示配置路径和默认端口(8080) -
brew services start nginx—— 后台启动,支持开机自启 -
open http://localhost:8080—— 浏览器打开,看到 “Welcome to nginx!” 就成功了
修改网页内容或配置
默认根目录和配置路径按芯片区分:
- Apple Silicon(M1/M2/M3):
• 网页文件夹:/opt/homebrew/var/www/html
• 主配置文件:/opt/homebrew/etc/nginx/nginx.conf - Intel Mac:
• 网页文件夹:/usr/local/var/www/html
• 主配置文件:/usr/local/etc/nginx/nginx.conf - 替换
index.html即可看到效果;改完配置后,用brew services restart nginx重载生效
验证与排错小技巧
遇到打不开页面时,优先检查这几项:
- 确认服务在运行:
brew services list | grep nginx,状态应为 started - 检查配置语法:
nginx -t,输出 “syntax is ok” 和 “test is successful” 才安全 - 查看是否端口冲突:
lsof -i :8080;Homebrew 默认用 8080 是为了避免需要 sudo - 想用 80 端口?不推荐日常使用,如确需,先
brew services stop nginx,再手动sudo nginx启动


















