CentOS 如何安装并配置 Nginx

从零开始,在 CentOS 上装好 Nginx,配置好虚拟主机,十分钟上线。


一、为什么选 Nginx?

如果你在 CentOS 上搭建 Web 服务,Nginx 几乎是首选。

对比项 Nginx Apache
并发能力 极强,事件驱动架构 较弱,进程/线程模型
内存占用 低(约 2~3MB/进程)
静态文件 非常快 一般
反向代理 原生支持,配置简单 需要额外模块
配置语法 简洁清晰 相对复杂

一句话:高性能、低资源、易配置。 适合做 Web 服务器、反向代理、负载均衡。


二、安装前准备

开始之前,确认以下条件:

  • ✅ CentOS 7 / 8 / 9(本文以 CentOS 7 为主,8/9 差异会标注)
  • ✅ root 或 sudo 权限
  • ✅ 系统已更新:sudo yum update -y
  • ✅ 关闭 SELinux(可选但推荐,避免权限问题):
bash 复制代码
sudo setenforce 0
sudo sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

三、安装 Nginx

CentOS 7 --- 使用 EPEL 源(推荐)

CentOS 7 官方源里的 Nginx 版本较老(1.16),建议用 EPEL 源安装最新稳定版。

bash 复制代码
# 1. 安装 EPEL 源
sudo yum install -y epel-release

# 2. 安装 Nginx
sudo yum install -y nginx

# 3. 查看安装的版本
nginx -v

输出类似:nginx version: nginx/1.24.0


CentOS 8 / 9 --- 使用 AppStream 源

bash 复制代码
sudo dnf install -y nginx

CentOS 8/9 自带的 Nginx 版本已经比较新(1.18+),一般不需要额外源。


如果需要最新版(所有版本通用)

使用官方源安装最新稳定版:

bash 复制代码
# CentOS 7
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://nginx.org/packages/centos/7/x86_64/
sudo yum install -y nginx

# CentOS 8/9
sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --add-repo https://nginx.org/packages/centos/8/x86_64/
sudo dnf install -y nginx

⚠️ 用官方源后,系统自带的 yum install nginx 会被覆盖。如需切回,删除 /etc/yum.repos.d/nginx.repo 即可。


四、启动 Nginx 并设置开机自启

bash 复制代码
# 启动
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 查看状态
sudo systemctl status nginx

看到 active (running) 就说明启动成功了。


五、配置防火墙

CentOS 默认开启 firewalld,需要放行 HTTP 和 HTTPS 端口:

bash 复制代码
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload

验证:

bash 复制代码
sudo firewall-cmd --list-all

应该能看到 httphttps 在 services 列表里。


六、验证安装

在浏览器中访问服务器 IP:

复制代码
http://你的服务器IP

如果看到 Nginx 默认欢迎页面:

Welcome to nginx!

说明安装成功 ✅

默认网页文件位置:

复制代码
/usr/share/nginx/html/index.html

七、Nginx 目录结构(先搞清楚文件在哪)

目录 用途
/etc/nginx/nginx.conf 主配置文件,所有配置从这里开始
/etc/nginx/conf.d/ 额外配置文件目录,虚拟主机放这里
/usr/share/nginx/html/ 默认网站根目录
/var/log/nginx/ 访问日志和错误日志
/var/run/nginx.pid Nginx 进程 PID 文件

八、配置虚拟主机(核心)

Nginx 的虚拟主机叫 server block,和 Apache 的 VirtualHost 一个意思。

1. 创建网站目录

bash 复制代码
sudo mkdir -p /var/www/example.com
sudo chown -R $USER:$USER /var/www/example.com
sudo chmod -R 755 /var/www/example.com

2. 创建测试页面

bash 复制代码
echo "<h1>Hello, example.com!</h1>" > /var/www/example.com/index.html

3. 创建虚拟主机配置

bash 复制代码
sudo vim /etc/nginx/conf.d/example.com.conf

写入以下内容:

nginx 复制代码
server {
    listen       80;
    server_name  example.com www.example.com;

    root   /var/www/example.com;
    index  index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    # 访问日志(可选)
    access_log  /var/log/nginx/example.com_access.log;
    error_log   /var/log/nginx/example.com_error.log;
}
指令 含义
listen 80 监听 80 端口(HTTP)
server_name 绑定的域名,支持多个用空格隔开
root 网站根目录
index 默认首页文件
try_files 找不到文件时返回 404(防止目录遍历)

4. 检查配置是否正确

bash 复制代码
sudo nginx -t

看到以下输出就没问题:

复制代码
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

5. 重载配置

bash 复制代码
sudo systemctl reload nginx

reload 不中断服务,restart 会短暂中断。日常改配置用 reload


九、配置 HTTPS(接 Let's Encrypt 免费证书)

HTTPS 配置和 HTTP 几乎一样,只是多了 SSL 相关指令。

方式一:Certbot 一键配置(最简单)

bash 复制代码
sudo yum install -y epel-release
sudo yum install -y certbot python3-certbot-nginx

sudo certbot --nginx -d example.com -d www.example.com

按提示输入邮箱,选择是否强制 HTTPS,一条命令搞定证书申请 + Nginx 配置 + 自动重定向

方式二:手动配置

nginx 复制代码
server {
    listen       443 ssl http2;
    server_name  example.com www.example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    root   /var/www/example.com;
    index  index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }
}

# HTTP → HTTPS 301 重定向
server {
    listen 80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

十、常用操作命令速查

操作 命令
启动 sudo systemctl start nginx
停止 sudo systemctl stop nginx
重载配置 sudo systemctl reload nginx
重启 sudo systemctl restart nginx
查看状态 sudo systemctl status nginx
检查配置 sudo nginx -t
查看版本 nginx -v
测试配置并退出 sudo nginx -t && sudo systemctl reload nginx

十一、配置反向代理(高频使用场景)

如果你的应用跑在 127.0.0.1:3000,想通过 example.com 访问:

nginx 复制代码
server {
    listen       80;
    server_name  example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

常用后端场景:

后端 proxy_pass
Node.js http://127.0.0.1:3000
Python/Gunicorn http://127.0.0.1:8000
Java/Tomcat http://127.0.0.1:8080
Docker 容器 http://172.17.0.1:端口

十二、常见问题排查

问题 原因 解决方案
浏览器打不开 防火墙没放行 firewall-cmd --add-service=http --permanent && firewall-cmd --reload
502 Bad Gateway 后端服务没启动 检查后端进程是否运行:curl http://127.0.0.1:3000
403 Forbidden 目录权限不对 sudo chmod -R 755 /var/www/example.com
配置改了不生效 没 reload sudo systemctl reload nginx
SELinux 报错 权限被拦截 sudo setenforce 0 或设置正确的 SELinux 上下文
端口被占用 80 端口被其他程序占用 `sudo netstat -tlnp

十三、总结

步骤 命令 耗时
安装 Nginx yum install -y nginx 30 秒
启动 + 开机自启 systemctl start nginx && systemctl enable nginx 5 秒
放行防火墙 firewall-cmd --add-service=http --permanent && reload 5 秒
配置虚拟主机 创建 .conf 文件 2 分钟
检查 + 重载 nginx -t && systemctl reload nginx 5 秒
总计 --- 不到 3 分钟

Nginx 的配置逻辑非常清晰:一个 server 块 = 一个网站 ,改完 nginx -t 验一下,reload 一下,就完事了。

装好 Nginx 只是第一步,后面搭配 Let's Encrypt 证书 + 反向代理 + 自动续期,就是一套完整的生产级 Web 服务方案。

现在就去把你的网站跑起来 🚀