安装 Nginx
约 399 字大约 1 分钟
布欧-Lewyon
2026-05-15
首页 › Nginx › 快速入门(在新窗口打开) › 安装 Nginx
macOS
# Homebrew(推荐)
brew install nginx
# 安装后
sudo nginx # 启动
curl http://localhost:8080 # 默认端口 8080(Homebrew)Homebrew 安装路径
| 路径 | 说明 |
|---|---|
/opt/homebrew/etc/nginx/ | 配置文件目录 |
/opt/homebrew/var/www/ | 默认 Web 根目录 |
/opt/homebrew/opt/nginx/bin/nginx | 可执行文件 |
Ubuntu / Debian
# 安装
sudo apt update
sudo apt install nginx -y
# 启动与开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
# 验证
curl http://localhost:80CentOS / RHEL
# 添加 EPEL 源
sudo yum install epel-release -y
# 安装
sudo yum install nginx -y
# 启动
sudo systemctl start nginx
sudo systemctl enable nginx
# 放行防火墙
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --reloadDocker
# 运行 Nginx 容器
docker run -d -p 80:80 --name nginx nginx:alpine
# 使用自定义配置
docker run -d -p 80:80 \
-v $(pwd)/nginx.conf:/etc/nginx/nginx.conf:ro \
-v $(pwd)/html:/usr/share/nginx/html:ro \
nginx:alpine源码编译(进阶)
# 安装依赖
sudo apt install build-essential libpcre3-dev libssl-dev zlib1g-dev
# 下载源码
wget https://nginx.org/download/nginx-1.26.0.tar.gz
tar xzf nginx-1.26.0.tar.gz
cd nginx-1.26.0
# 配置并编译
./configure \
--prefix=/etc/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--with-http_ssl_module \
--with-http_v2_module \
--with-stream
make
sudo make install
# 验证
nginx -v验证安装
# 查看版本
nginx -v
# nginx version: nginx/1.26.0
# 查看编译参数
nginx -V
# 测试配置
nginx -t
# nginx: the configuration file syntax is ok
# nginx: configuration file test is successful小结
- macOS:
brew install nginx,默认端口 8080。 - Ubuntu:
apt install nginx,默认端口 80。 - CentOS:
yum install nginx,注意防火墙。 - Docker:
docker run nginx:alpine快速启动。 - 编译安装:适合需要自定义模块的场景。
- 安装后执行
nginx -v和nginx -t验证。
