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

推荐订阅源

T
Tenable Blog
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
Engineering at Meta
Engineering at Meta
H
Help Net Security
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
宝玉的分享
宝玉的分享
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 司徒正美
量子位
N
Netflix TechBlog - Medium
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
Recorded Future
Recorded Future
博客园 - 三生石上(FineUI控件)
Vercel News
Vercel News
aimingoo的专栏
aimingoo的专栏
I
InfoQ
Microsoft Security Blog
Microsoft Security Blog
Scott Helme
Scott Helme
The Last Watchdog
The Last Watchdog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
IT之家
IT之家
AI
AI
WordPress大学
WordPress大学
Security Archives - TechRepublic
Security Archives - TechRepublic
Google Online Security Blog
Google Online Security Blog
U
Unit 42
V2EX - 技术
V2EX - 技术
MongoDB | Blog
MongoDB | Blog
Schneier on Security
Schneier on Security
博客园 - Franky
H
Heimdal Security Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Jina AI
Jina AI
W
WeLiveSecurity
P
Privacy & Cybersecurity Law Blog
Cloudbric
Cloudbric
B
Blog RSS Feed
N
News | PayPal Newsroom
S
Securelist
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
I
Intezer
Hacker News - Newest:
Hacker News - Newest: "LLM"
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
博客园_首页
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
雷峰网
雷峰网

博客园 - 上校

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