Nginx学习笔记(二)——环境准备(VMware CentOS版)

发布于:2025-08-13 ⋅ 阅读:(21) ⋅ 点赞:(0)

📚 Nginx学习笔记(二)——环境准备(VMware CentOS版)

🔧 1. 系统准备(CentOS 7/8)
# 更新系统
sudo yum update -y

# 安装必备工具
sudo yum install -y yum-utils epel-release
📦 2. 添加Nginx官方仓库
# 创建官方仓库配置文件
cat <<EOF | sudo tee /etc/yum.repos.d/nginx.repo
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

[nginx-mainline]
name=nginx mainline repo
baseurl=http://nginx.org/packages/mainline/centos/\$releasever/\$basearch/
gpgcheck=1
enabled=0
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
EOF
⚙️ 3. 安装Nginx(官方推荐方式)
# 查看可用版本(按需选择)
sudo yum --showduplicates list nginx

# 安装稳定版(默认)
sudo yum install -y nginx

# 或安装mainline版本(新特性)
sudo yum-config-manager --enable nginx-mainline
sudo yum install -y nginx

# 验证安装
nginx -v
# 输出示例:nginx version: nginx/1.24.0
🔍 4. 目录结构(官方包安装路径)
/etc/nginx/               # 主配置目录 ★
├── nginx.conf            # 主配置文件
├── conf.d/               # 额外配置目录
├── modules-available/    # 可用模块
├── modules-enabled/      # 启用模块
├── sites-available/      # 可用站点
├── sites-enabled/        # 启用站点
├── mime.types            # MIME类型

/usr/share/nginx/         # 默认Web根目录
├── html/                 
│   ├── index.html        
│   └── 50x.html          

/var/log/nginx/           # 日志目录 ★
├── access.log
├── error.log

/usr/sbin/nginx           # 主程序
⚡ 5. 启停管理(systemd集成)
# 启动服务
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 常用命令
sudo systemctl status nginx   # 查看状态
sudo systemctl reload nginx   # 重载配置(零停机)
sudo systemctl restart nginx  # 重启服务
sudo systemctl stop nginx     # 停止服务
🛡️ 6. 防火墙配置
# 永久开放HTTP/HTTPS端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https

# 重载防火墙规则
sudo firewall-cmd --reload

# 验证端口
sudo firewall-cmd --list-all
✅ 7. 验证安装
# 访问默认页
curl -I http://localhost

# 预期输出:
HTTP/1.1 200 OK
Server: nginx/1.24.0
Content-Type: text/html
🧪 8. 基础配置示例(/etc/nginx/nginx.conf)
user nginx;                    # 专用系统用户
worker_processes auto;         # 自动匹配CPU核心数
error_log /var/log/nginx/error.log warn; # 错误日志位置

events {
    worker_connections 1024;   # 单进程连接数
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    sendfile on;               # 启用零拷贝
    keepalive_timeout 65;      # 连接保持时间

    server {
        listen 80;
        server_name localhost;

        location / {
            root /usr/share/nginx/html;
            index index.html;
        }

        # 错误页配置
        error_page 500 502 503 504 /50x.html;
        location = /50x.html {
            root /usr/share/nginx/html;
        }
    }

    # 包含额外配置
    include /etc/nginx/conf.d/*.conf;
}
🔐 9. 权限管理(官方推荐)
# 使用专用用户
sudo chown -R nginx:nginx /usr/share/nginx/html

# 配置SELinux(如启用)
sudo setsebool -P httpd_can_network_connect 1
🌐 10. VMware网络配置
  1. 网络模式选择

    • 桥接模式:直接获取局域网IP(推荐)

    NAT模式

    :配置端口转发

    # 查看虚拟机IP
    ip -4 addr show eth0 | grep -oP '(?<=inet\s)\d+(\.\d+){3}'
    
  2. 宿主机访问

    • 浏览器访问 http://<虚拟机IP>
    • 应显示 “Welcome to nginx!” 页面
⚠️ 11. 常见问题解决
  1. 端口冲突

    # 检查端口占用
    sudo ss -tulpn | grep ':80'
    
    # 如被占用,停用其他服务
    sudo systemctl stop httpd
    
  2. 配置测试

    sudo nginx -t  # 必须通过测试才能重载
    # 成功输出:nginx: configuration file /etc/nginx/nginx.conf test is successful
    
  3. 权限问题

    # 检查错误日志
    tail -f /var/log/nginx/error.log
    
📚 官方推荐实践
  1. 配置管理

    • 自定义配置放在 /etc/nginx/conf.d/ 目录
    • 每个站点创建独立 .conf 文件
  2. 模块管理

    # 查看已安装模块
    nginx -V 2>&1 | tr ' ' '\n' | grep module
    
  3. 安全加固

    server_tokens off;  # 隐藏Nginx版本号
    add_header X-Content-Type-Options "nosniff";
    

✅ 完成验证:curl -I http://localhost | grep "200 OK"


下一步建议

  1. 创建测试页面:echo "Hello Nginx!" | sudo tee /usr/share/nginx/html/test.html
  2. 学习配置管理:sudo nano /etc/nginx/conf.d/test.conf
  3. 掌握日志分析:sudo tail -f /var/log/nginx/access.log

官方文档参考:Nginx Linux Packages
生产环境建议:定期更新 sudo yum update nginx


网站公告

今日签到

点亮在社区的每一天
去签到