在 macOS 上安装

Nginx 在 macOS 上有多种安装方式,本章节将介绍最常用的安装方法。

安装方式概览

  • Homebrew:推荐方式,安装和更新最方便
  • 官方二进制包:直接下载预编译版本
  • 源码编译:高度自定义,适合高级用户

使用 Homebrew 安装(推荐)

Homebrew 是 macOS 上最流行的包管理器,使用它安装 Nginx 非常简单。

安装 Homebrew

如果尚未安装 Homebrew,请在终端中运行:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

安装 Nginx

# 更新 Homebrew
brew update

# 安装 Nginx
brew install nginx

# 查看安装信息
brew info nginx

启动和管理 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(后台运行)
nginx -s start

# 重新加载配置
nginx -s reload

# 停止 Nginx
nginx -s stop

# 优雅退出
nginx -s quit

下载官方二进制包

下载安装

  1. 访问 Nginx 官方下载页面
  2. 下载 macOS 版本的 .pkg 安装包
  3. 双击安装包,按照提示完成安装

配置文件位置

# 主配置文件
/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

# 下载 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!" 页面即表示安装成功。

常用目录(Homebrew 安装)

目录说明
/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 端口

如果需要使用 80 端口,可以使用 sudo 运行:

# 停止当前 Nginx
brew services stop nginx

# 使用 sudo 启动
sudo nginx

# 或者修改配置后重新加载
sudo nginx -s reload

开机自启

# 使用 Homebrew 服务管理(推荐)
brew services start nginx

# 添加到登录项
# 系统偏好设置 -> 用户与群组 -> 登录项 -> 添加 nginx

卸载 Nginx

Homebrew 安装的卸载

# 停止服务
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

更新 Nginx

# 更新 Homebrew
brew update

# 升级 Nginx
brew upgrade nginx

# 重启服务
brew services restart nginx