选Nginx更优,因其性能好、配置现代、内存占用低,尤其搭配PHP-FPM时响应更稳;系统自带Apache则启动快、零依赖,适合快速验证;两者均可通过Homebrew独立安装互不干扰。
macos 上搭建本地 web 服务器,apache 和 nginx 都很适合,选哪个主要看你的使用习惯和项目需求。系统自带 apache 启动快、零依赖;nginx 性能好、配置更现代,尤其搭配 php-fpm 时响应更稳。两者都可通过 homebrew 独立安装,互不干扰。
启用系统自带 Apache(最轻量)
macOS 仍预装 Apache(10.13 及以后),无需额外下载,适合快速验证或简单静态页开发:
- 终端执行 sudo apachectl start 启动服务
- 浏览器访问 http://localhost,看到 “It works!” 即成功
- 默认根目录是 /Library/WebServer/Documents/,放 HTML 文件即可访问
- 如需改为自己目录(例如
~/Sites),编辑/etc/apache2/httpd.conf,修改DocumentRoot和对应<Directory>路径,并确保该目录存在且权限为755
用 Homebrew 安装独立 Apache(更灵活)
适合需要新版模块(如 HTTP/2)、避免系统版本限制,或想与系统 Apache 并存的场景:
- 先装 Homebrew(若未装):/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- 运行 brew install httpd
- 启动:brew services start httpd
- 默认监听 8080 端口,访问 http://localhost:8080
- 主配置文件在 /opt/homebrew/etc/httpd/httpd.conf(Apple Silicon)或 /usr/local/etc/httpd/httpd.conf(Intel),可自由调整
Listen、DocumentRoot、加载模块等
用 Homebrew 安装 Nginx(推荐用于 PHP 或高并发测试)
Nginx 启动快、内存占用低,配合 PHP-FPM 是本地 PHP 开发的高效组合:
- 确保已装 Homebrew,然后执行 brew install nginx
- 启动服务:brew services start nginx
- 默认访问地址是 http://localhost:8080(Homebrew 自动避让 80 端口)
- 默认根目录:/opt/homebrew/var/www/html/(Apple Silicon)或 /usr/local/var/www/html/(Intel)
- 如需支持 PHP,还需 brew install php@8.3 并配置 Nginx 的
location ~ \.php$块,指向fastcgi_pass 127.0.0.1:9000,再启动 brew services start php
小贴士:端口与权限处理
绑定 80 端口需 root 权限,开发中建议改用非特权端口(如 8000、8080)避免频繁输密码:
- Apache:编辑对应
httpd.conf,把Listen 80改成Listen 8000,同时更新<VirtualHost *:80>为<VirtualHost *:8000> - Nginx:编辑
/opt/homebrew/etc/nginx/nginx.conf,修改listen 8080行即可 - 改完配置后,分别用 sudo apachectl restart 或 brew services restart nginx 生效


















