



























在 Linux 下安装 Nginx 主要有两种主流方式:**包管理器安装(推荐)** 和 **源码编译安装**。包管理器方式最简单,适合绝大多数场景;源码编译方式更灵活,适合需要定制模块或特定版本的场景。
---
### 📦 方式一:包管理器安装(推荐)
**适用系统**:Ubuntu/Debian、CentOS/RHEL/Alibaba Cloud Linux
**优点**:自动解决依赖,方便后续升级,与系统服务管理集成。
#### Ubuntu/Debian 系统
```bash
# 1. 更新软件包列表
sudo apt update
# 2. 安装 Nginx
sudo apt install nginx -y
# 3. 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
```
执行 `sudo systemctl status nginx` 可以查看运行状态。
#### CentOS/RHEL/Alibaba Cloud Linux 系统
```bash
# 1. 启用 EPEL 仓库(CentOS 7 需要,Alibaba Cloud Linux 通常已包含)
sudo yum install epel-release -y
# 2. 安装 Nginx
sudo yum install nginx -y
# 3. 启动并设置开机自启
sudo systemctl start nginx
sudo systemctl enable nginx
```
> **说明**:CentOS 8+ 推荐使用 `dnf` 代替 `yum`,命令格式相同。
---
### 🛠️ 方式二:源码编译安装
**适用场景**:需要自定义编译参数、安装第三方模块、或使用特定老版本。
**优点**:定制化程度高;**缺点**:升级和维护需要手动操作。
#### 完整安装步骤
**1. 安装编译依赖**
```bash
# CentOS/RHEL
sudo yum install gcc make pcre-devel zlib-devel openssl-devel -y
# Ubuntu/Debian
sudo apt install build-essential libpcre3-dev zlib1g-dev libssl-dev -y
```
**2. 下载并解压源码**
```bash
# 下载稳定版(以 1.26.2 为例,可去官网查看最新版本)
wget https://nginx.org/download/nginx-1.26.2.tar.gz
tar -zxvf nginx-1.26.2.tar.gz
cd nginx-1.26.2
```
**3. 配置编译参数**
```bash
./configure \
--prefix=/usr/local/nginx \
--sbin-path=/usr/sbin/nginx \
--conf-path=/etc/nginx/nginx.conf \
--user=nginx \
--group=nginx \
--with-http_ssl_module \
--with-http_v2_module \
--with-stream \
--with-threads
```
* `--prefix`:指定安装目录
* `--with-http_ssl_module`:启用 HTTPS 支持
* `--with-stream`:启用 TCP/UDP 代理(四层负载均衡)
* `--user` 和 `--group`:指定运行用户,需提前创建 `nginx` 用户
**4. 编译并安装**
```bash
make && sudo make install
```
**5. 启动 Nginx**
```bash
# 使用安装路径启动
sudo /usr/local/nginx/sbin/nginx
```
如果提示 `getpwnam("nginx") failed`,需要先创建 `nginx` 用户:
```bash
sudo useradd -r -s /sbin/nologin nginx
```
---
### 📁 安装后的关键目录与文件
| 项目 | 包管理器安装路径 | 源码编译默认路径 | 说明 |
| :--- | :--- | :--- | :--- |
| **主配置文件** | `/etc/nginx/nginx.conf` | `/usr/local/nginx/conf/nginx.conf` | 核心配置 |
| **站点配置** | `/etc/nginx/conf.d/` | `/usr/local/nginx/conf/` | 建议将站点配置独立存放 |
| **默认网页根目录** | `/usr/share/nginx/html/` | `/usr/local/nginx/html/` | 存放静态文件 |
| **可执行文件** | `/usr/sbin/nginx` | `/usr/local/nginx/sbin/nginx` | Nginx 命令 |
---
### 🚀 常用管理命令
```bash
# 启动 Nginx
sudo systemctl start nginx # systemd 方式(推荐)
sudo /usr/sbin/nginx # 手动启动(源码编译方式)
# 停止 Nginx
sudo systemctl stop nginx # systemd 方式
sudo /usr/sbin/nginx -s stop # 手动停止
# 重载配置(不中断服务,修改配置后常用)
sudo systemctl reload nginx # systemd 方式
sudo /usr/sbin/nginx -s reload # 手动重载
# 测试配置文件语法
sudo nginx -t # 或 sudo /usr/sbin/nginx -t
```
**建议**:每次修改配置后,先用 `nginx -t` 测试语法,确认无误后再 `reload`,避免因配置错误导致服务中断。
---
### ⚠️ 注意事项
1. **防火墙端口放行**:安装后需放行 80(HTTP)和 443(HTTPS)端口。
```bash
# firewalld(CentOS/RHEL)
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
# ufw(Ubuntu/Debian)
sudo ufw allow 'Nginx Full'
```
2. **访问测试**:浏览器访问 `http://你的服务器IP`,看到 "Welcome to nginx!" 页面即安装成功。
3. **包管理器 vs 源码编译**:对大多数用户,**包管理器安装是首选**。如果你的项目需要特定模块(如 `--with-http_v2_module`)或需要将 Nginx 安装到特定目录,再考虑源码编译。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。