Skip to content

Linux 安装与服务管理指南 ​

本指南旨在提供 Linux 服务器环境下 Nginx 的标准化安装与维护 SOP,涵盖主流的 Ubuntu 与 CentOS 发行版。

1. Ubuntu 20.04 安装流程 ​

Ubuntu 系统推荐使用官方仓库进行安装,以确保获得稳定的安全更新。

1.1 使用 apt 安装 ​

bash
# 更新软件包索引
sudo apt update

# 安装 Nginx
sudo apt install nginx -y

# 验证安装版本
nginx -v

1.2 服务管理 (Systemd) ​

bash
sudo systemctl start nginx    # 启动
sudo systemctl stop nginx     # 停止
sudo systemctl restart nginx   # 重启
sudo systemctl reload nginx    # 平滑重载配置(推荐)
sudo systemctl enable nginx    # 设置开机自启
sudo systemctl status nginx    # 查看运行状态

2. CentOS 7.0 安装流程 ​

CentOS 7 默认仓库不包含 Nginx,需要先安装 EPEL (Extra Packages for Enterprise Linux) 仓库。

2.1 使用 yum 安装 ​

bash
# 安装 EPEL 仓库
sudo yum install epel-release -y

# 安装 Nginx
sudo yum install nginx -y

# 启动并验证
sudo systemctl start nginx
nginx -v

2.2 服务管理 ​

CentOS 7 同样使用 systemctl 命令进行管理,逻辑与 Ubuntu 一致。


3. 核心路径与配置规范 ​

为了保持多端维护的一致性,请遵循以下路径规约:

资源类型默认路径说明
主配置文件/etc/nginx/nginx.conf全局配置入口
站点配置目录/etc/nginx/conf.d/建议将不同业务的 .conf 存放在此
静态资源根目录/var/www/html默认 Web 根目录
日志目录/var/log/nginx/包含 access.log 和 error.log

4. 安全配置建议 ​

4.1 防火墙配置 ​

确保防火墙放行 HTTP (80) 和 HTTPS (443) 端口。

Ubuntu (UFW):

bash
sudo ufw allow 'Nginx Full'

CentOS (Firewalld):

bash
sudo firewall-cmd --permanent --zone=public --add-service=http
sudo firewall-cmd --permanent --zone=public --add-service=https
sudo firewall-cmd --reload

4.2 基础加固 ​

编辑 /etc/nginx/nginx.conf,在 http 块中添加:

nginx
http {
    server_tokens off;          # 隐藏 Nginx 版本号,减少漏洞暴露
    client_max_body_size 50m;   # 限制上传文件大小,防止拒绝服务攻击
    # ...
}

5. 故障排查 SOP ​

当服务无法启动或配置不生效时,请按以下顺序排查:

  1. 检查语法:

    bash
    sudo nginx -t

    这是最重要的一步,修改配置后必须执行。

  2. 查看错误日志:

    bash
    tail -f /var/log/nginx/error.log
  3. 检查端口占用:

    bash
    netstat -tlnp | grep :80

方舟 · Ark