macOS(Intel & Apple Silicon) 上从零到上线的一条龙配置流程,全部基于 Homebrew,可直接复制执行。
- 安装 / 更新 Homebrew(如果已装可跳过)
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 安装 Nginx
brew update
brew install nginx
安装完 Homebrew 会提示 3 个关键路径,先记下来:
- 配置文件:
/opt/homebrew/etc/nginx/nginx.conf(Apple Silicon)
或/usr/local/etc/nginx/nginx.conf(Intel) - 站点根目录:
/opt/homebrew/var/www或/usr/local/var/www - 日志目录:
/opt/homebrew/var/log/nginx/或/usr/local/var/log/nginx/
- 启动 Nginx 并设为开机自启
# 前台调试(先看能否跑起来)
nginx
# 正式作为服务运行
sudo brew services start nginx
浏览器访问 http://localhost:8080 出现 “Welcome to nginx” 即成功。
- 修改端口到 80(可选)
macOS 自带 Apache 可能已占 80,先停掉:
sudo apachectl stop
编辑配置:
# Apple Silicon
nano /opt/homebrew/etc/nginx/nginx.conf
# Intel
nano /usr/local/etc/nginx/nginx.conf
把 listen 8080 改成 listen 80,保存后
sudo nginx -s reload
此时访问 http://localhost 即可。
- 配置虚拟主机示例
在nginx.conf末尾http {}里加:
server {
listen 80;
server_name test.local;
root /opt/homebrew/var/www/test; # Intel 换成 /usr/local/var/www/test
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}
创建目录并写个首页:
mkdir -p /opt/homebrew/var/www/test
echo "Hello macOS Nginx" > /opt/homebrew/var/www/test/index.html
重载生效:
sudo nginx -s reload
把 test.local 写到 /etc/hosts:
127.0.0.1 test.local
浏览器访问 http://test.local 应可见 “Hello macOS Nginx”。
- 常用管理命令速查
sudo brew services start nginx # 启动
sudo brew services stop nginx # 停止
sudo brew services restart nginx # 重启
sudo nginx -t # 检查语法
sudo nginx -s reload # 平滑重载
sudo nginx -s stop # 立即退出
- 目录结构一览(Apple Silicon 为例)
/opt/homebrew/
├── etc/nginx/nginx.conf # 主配置
├── etc/nginx/servers/ # 虚拟主机片段,*.conf 自动 include
├── var/www/ # 网站根目录
└── var/log/nginx/ # access.log & error.log
Intel 把 /opt/homebrew 换成 /usr/local 即可。
至此,macOS 上的 Nginx 已全部配置完毕并具备生产级可用性。
附录
brew install nginx
















