惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
D
DataBreaches.Net
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
D
Docker
N
Netflix TechBlog - Medium
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
C
Check Point Blog
腾讯CDC
Stack Overflow Blog
Stack Overflow Blog
V
Visual Studio Blog
IT之家
IT之家
月光博客
月光博客
U
Unit 42
K
Kaspersky official blog
T
Threatpost
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
GbyAI
GbyAI
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
云风的 BLOG
云风的 BLOG
酷 壳 – CoolShell
酷 壳 – CoolShell
I
InfoQ
Engineering at Meta
Engineering at Meta
Recorded Future
Recorded Future
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
Security @ Cisco Blogs
MyScale Blog
MyScale Blog
大猫的无限游戏
大猫的无限游戏
Security Archives - TechRepublic
Security Archives - TechRepublic
Webroot Blog
Webroot Blog
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
S
Schneier on Security
S
Secure Thoughts
The Register - Security
The Register - Security
B
Blog RSS Feed
The Last Watchdog
The Last Watchdog
P
Palo Alto Networks Blog
爱范儿
爱范儿
B
Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
LINUX DO - 热门话题
C
Cisco Blogs
Spread Privacy
Spread Privacy
F
Full Disclosure
博客园 - 聂微东
T
The Blog of Author Tim Ferriss

博客园 - 上校

nacos安装教程 minio安装教程 GIT笔记 CC教程 芋道笔记 SpringCloud MySqlSugar常用写法 linux查找大目录和大文件 .NET代码混淆——开源.net 混淆器ConfuserEx介绍 微软云SQL数据库创建只读用户 nginx配置ssl证书实现https访问 nexus搭建私服 Linux下创建用户并设置权限 ActiveMQ修改密码 shiro源码篇 - shiro的session共享,你值得拥有 google guava Docker虚拟化管理:30分钟教你学会用Docker Shiro结合Redis实现分布式或集群环境下的Session共享 Springboot整合redis spring boot2整合shiro安全框架实现前后端分离的JWT token登录验证 http post scp命令 Linux和Windows文件互传
nginx安装教程
上校 · 2026-06-27 · via 博客园 - 上校

在 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 安装到特定目录,再考虑源码编译。