Nginx 在 macOS 上有多种安装方式,本章节将介绍最常用的安装方法。
Homebrew 是 macOS 上最流行的包管理器,使用它安装 Nginx 非常简单。
如果尚未安装 Homebrew,请在终端中运行:
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# 更新 Homebrew
brew update
# 安装 Nginx
brew install nginx
# 查看安装信息
brew info nginx
# 启动 Nginx
brew services start nginx
# 停止 Nginx
brew services stop nginx
# 重启 Nginx
brew services restart nginx
# 查看运行状态
brew services list
# 查看版本
nginx -v
# 启动 Nginx(前台运行)
nginx
# 启动 Nginx(后台运行)
nginx -s start
# 重新加载配置
nginx -s reload
# 停止 Nginx
nginx -s stop
# 优雅退出
nginx -s quit
.pkg 安装包# 主配置文件
/usr/local/etc/nginx/nginx.conf
# 额外配置文件目录
/usr/local/etc/nginx/servers/
# 日志文件目录
/usr/local/var/log/nginx/
# 默认网站根目录
/usr/local/var/www/
# 安装 Xcode 命令行工具
xcode-select --install
# 安装 PCRE
brew install pcre
# 安装 zlib
brew install zlib
# 安装 OpenSSL
brew install openssl
# 下载 Nginx 源码
cd /usr/local/src
curl -O http://nginx.org/download/nginx-1.24.0.tar.gz
tar -xzvf nginx-1.24.0.tar.gz
cd nginx-1.24.0
# 配置编译选项
./configure \
--prefix=/usr/local/nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-http_realip_module \
--with-http_gzip_static_module \
--with-file-aio \
--with-http_secure_link_module \
--with-pcre=/usr/local/opt/pcre \
--with-zlib=/usr/local/opt/zlib \
--with-openssl=/usr/local/opt/openssl
# 编译
make
# 安装
sudo make install
# 创建 nginx 命令的符号链接
sudo ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
# 创建配置目录的符号链接
sudo ln -s /usr/local/nginx/conf /usr/local/etc/nginx
# 检查 Nginx 版本
nginx -v
# 检查配置文件语法
nginx -t
# 测试 Nginx 服务
curl http://localhost
在浏览器中访问 http://localhost,看到 "Welcome to nginx!" 页面即表示安装成功。
| 目录 | 说明 |
|---|---|
/usr/local/etc/nginx/ | 配置文件目录 |
/usr/local/etc/nginx/nginx.conf | 主配置文件 |
/usr/local/var/log/nginx/ | 日志文件目录 |
/usr/local/var/www/ | 默认网站根目录 |
# /usr/local/etc/nginx/nginx.conf
user staff;
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 8080;
server_name localhost;
location / {
root /usr/local/var/www;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/local/var/www;
}
}
}
macOS 上 80 端口可能被其他服务占用,可以修改为其他端口:
# /usr/local/etc/nginx/nginx.conf
server {
listen 8080; # 修改为 8080 端口
server_name localhost;
location / {
root /usr/local/var/www;
index index.html index.htm;
}
}
修改后重新加载配置:
nginx -t
nginx -s reload
如果需要使用 80 端口,可以使用 sudo 运行:
# 停止当前 Nginx
brew services stop nginx
# 使用 sudo 启动
sudo nginx
# 或者修改配置后重新加载
sudo nginx -s reload
# 使用 Homebrew 服务管理(推荐)
brew services start nginx
# 添加到登录项
# 系统偏好设置 -> 用户与群组 -> 登录项 -> 添加 nginx
# 停止服务
brew services stop nginx
# 卸载 Nginx
brew uninstall nginx
# 删除配置文件和数据
rm -rf /usr/local/etc/nginx
rm -rf /usr/local/var/log/nginx
rm -rf /usr/local/var/www
# 停止 Nginx
sudo nginx -s stop
# 删除安装目录
sudo rm -rf /usr/local/nginx
# 删除符号链接
sudo rm /usr/local/bin/nginx
sudo rm /usr/local/etc/nginx
# 查看错误日志
tail -f /usr/local/var/log/nginx/error.log
# 查看访问日志
tail -f /usr/local/var/log/nginx/access.log
# 检查配置文件
nginx -t
# 查看进程
ps aux | grep nginx
# 查看端口占用
lsof -i :8080
# 或
netstat -an | grep 8080
# 更新 Homebrew
brew update
# 升级 Nginx
brew upgrade nginx
# 重启服务
brew services restart nginx