CentOS7安装Nginx

CentOS7安装Nginx

      • [Nginx 1.28.0 源码编译安装详细过程](#Nginx 1.28.0 源码编译安装详细过程)
        • [1. 准备工作](#1. 准备工作)
        • [2. 解压源码包](#2. 解压源码包)
        • [3. 配置编译选项](#3. 配置编译选项)
        • [4. 编译和安装](#4. 编译和安装)
        • [5. 创建 Nginx 用户和组](#5. 创建 Nginx 用户和组)
        • [6. 创建 Nginx 配置文件](#6. 创建 Nginx 配置文件)
        • [7. 创建 mime.types 文件](#7. 创建 mime.types 文件)
        • [8. 创建默认首页](#8. 创建默认首页)
        • [9. 创建 Systemd 服务文件](#9. 创建 Systemd 服务文件)
        • [10. 创建缓存目录](#10. 创建缓存目录)
        • [11. 启动 Nginx 服务](#11. 启动 Nginx 服务)
        • [12. 配置防火墙](#12. 配置防火墙)
        • [13. 验证安装](#13. 验证安装)
        • [14. 创建管理脚本](#14. 创建管理脚本)
        • [15. 创建环境变量](#15. 创建环境变量)
        • [16. 最终验证](#16. 最终验证)
        • [17. 目录结构说明](#17. 目录结构说明)
        • [18. 常用管理命令](#18. 常用管理命令)
        • [19. 卸载方法(如果需要)](#19. 卸载方法(如果需要))

Nginx 1.28.0 源码编译安装详细过程

1. 准备工作
bash 复制代码
# 切换到安装目录
cd /opt/nginx

# 安装编译依赖
sudo yum install -y gcc gcc-c++ make pcre pcre-devel zlib zlib-devel openssl openssl-devel

# 创建必要的目录
sudo mkdir -p /opt/nginx/{bin,conf,logs,html,cache,ssl}
2. 解压源码包
bash 复制代码
# 解压 Nginx 源码
sudo tar -xzf nginx-1.28.0.tar.gz

# 进入源码目录
cd nginx-1.28.0
3. 配置编译选项
bash 复制代码
# 配置编译参数
sudo ./configure \
    --prefix=/opt/nginx \
    --sbin-path=/opt/nginx/bin/nginx \
    --conf-path=/opt/nginx/conf/nginx.conf \
    --error-log-path=/opt/nginx/logs/error.log \
    --http-log-path=/opt/nginx/logs/access.log \
    --pid-path=/opt/nginx/logs/nginx.pid \
    --lock-path=/opt/nginx/logs/nginx.lock \
    --http-client-body-temp-path=/opt/nginx/cache/client_temp \
    --http-proxy-temp-path=/opt/nginx/cache/proxy_temp \
    --http-fastcgi-temp-path=/opt/nginx/cache/fastcgi_temp \
    --http-uwsgi-temp-path=/opt/nginx/cache/uwsgi_temp \
    --http-scgi-temp-path=/opt/nginx/cache/scgi_temp \
    --user=nginx \
    --group=nginx \
    --with-http_ssl_module \
    --with-http_v2_module \
    --with-http_realip_module \
    --with-http_addition_module \
    --with-http_sub_module \
    --with-http_dav_module \
    --with-http_flv_module \
    --with-http_mp4_module \
    --with-http_gunzip_module \
    --with-http_gzip_static_module \
    --with-http_random_index_module \
    --with-http_secure_link_module \
    --with-http_stub_status_module \
    --with-http_auth_request_module \
    --with-threads \
    --with-stream \
    --with-stream_ssl_module \
    --with-mail \
    --with-mail_ssl_module \
    --with-file-aio \
    --with-http_slice_module \
    --with-compat
4. 编译和安装
bash 复制代码
# 编译(使用多核加速)
sudo make -j$(nproc)

# 安装
sudo make install
5. 创建 Nginx 用户和组
bash 复制代码
# 创建 nginx 用户和组
sudo groupadd nginx
sudo useradd -r -g nginx -s /bin/false nginx

# 设置目录权限
sudo chown -R nginx:nginx /opt/nginx
sudo chmod -R 755 /opt/nginx
6. 创建 Nginx 配置文件
bash 复制代码
# 创建主配置文件
sudo vi /opt/nginx/conf/nginx.conf

配置文件内容:

nginx 复制代码
user nginx nginx;
worker_processes auto;
error_log /opt/nginx/logs/error.log warn;
pid /opt/nginx/logs/nginx.pid;

events {
    worker_connections 1024;
    use epoll;
    multi_accept on;
}

http {
    include /opt/nginx/conf/mime.types;
    default_type application/octet-stream;

    log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /opt/nginx/logs/access.log main;

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

    # 虚拟主机配置
    server {
        listen 80;
        server_name localhost;
        root /opt/nginx/html;
        index index.html index.htm;

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

        location /status {
            stub_status on;
            access_log off;
            allow 127.0.0.1;
            deny all;
        }
    }

    # 包含其他配置文件
    include /opt/nginx/conf/conf.d/*.conf;
}
7. 创建 mime.types 文件
bash 复制代码
# 复制 mime.types 文件
sudo cp conf/mime.types /opt/nginx/conf/
8. 创建默认首页
bash 复制代码
# 创建默认 HTML 页面
sudo tee /opt/nginx/html/index.html <<'EOF'
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Nginx 1.28.0</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 40px; }
        h1 { color: #333; }
    </style>
</head>
<body>
    <h1>Welcome to Nginx 1.28.0</h1>
    <p>If you see this page, the nginx web server is successfully installed and working.</p>
    <p>Thank you for using nginx.</p>
    <hr>
    <p><em>Server: Nginx/1.28.0</em></p>
</body>
</html>
EOF

# 设置权限
sudo chown nginx:nginx /opt/nginx/html/index.html
9. 创建 Systemd 服务文件
bash 复制代码
sudo vi /etc/systemd/system/nginx.service

服务文件内容:

ini 复制代码
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/opt/nginx/logs/nginx.pid
ExecStartPre=/opt/nginx/bin/nginx -t
ExecStart=/opt/nginx/bin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
PrivateTmp=true
User=nginx
Group=nginx

[Install]
WantedBy=multi-user.target
10. 创建缓存目录
bash 复制代码
# 创建缓存目录
sudo mkdir -p /opt/nginx/cache/{client_temp,proxy_temp,fastcgi_temp,uwsgi_temp,scgi_temp}

# 设置缓存目录权限
sudo chown -R nginx:nginx /opt/nginx/cache
sudo chmod -R 755 /opt/nginx/cache
11. 启动 Nginx 服务
bash 复制代码
# 重新加载 systemd
sudo systemctl daemon-reload

# 测试配置文件
sudo /opt/nginx/bin/nginx -t

# 启动 Nginx
sudo systemctl start nginx

# 设置开机自启
sudo systemctl enable nginx

# 检查服务状态
sudo systemctl status nginx
12. 配置防火墙
bash 复制代码
# 开放 HTTP 端口
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
13. 验证安装
bash 复制代码
# 检查进程
ps -ef | grep nginx

# 检查端口
netstat -tulnp | grep nginx

# 测试访问
curl -I http://localhost

# 检查版本
/opt/nginx/bin/nginx -v

# 查看编译参数
/opt/nginx/bin/nginx -V
14. 创建管理脚本
bash 复制代码
# 创建管理脚本
sudo tee /opt/nginx/nginx-manager.sh <<'EOF'
#!/bin/bash

NGINX_BIN="/opt/nginx/bin/nginx"
NGINX_CONF="/opt/nginx/conf/nginx.conf"

case "$1" in
    start)
        $NGINX_BIN -c $NGINX_CONF
        ;;
    stop)
        $NGINX_BIN -s stop
        ;;
    reload)
        $NGINX_BIN -s reload
        ;;
    restart)
        $NGINX_BIN -s stop
        sleep 2
        $NGINX_BIN -c $NGINX_CONF
        ;;
    test)
        $NGINX_BIN -t -c $NGINX_CONF
        ;;
    status)
        ps -ef | grep nginx | grep -v grep
        ;;
    *)
        echo "Usage: $0 {start|stop|reload|restart|test|status}"
        exit 1
        ;;
esac
EOF

sudo chmod +x /opt/nginx/nginx-manager.sh
15. 创建环境变量
bash 复制代码
# 添加到 ~/.bashrc 或 /etc/profile
echo 'export PATH=$PATH:/opt/nginx/bin' | sudo tee -a /etc/profile
source /etc/profile
16. 最终验证
bash 复制代码
#!/bin/bash

echo "=== Nginx 1.28.0 安装验证 ==="

echo "1. 服务状态:"
sudo systemctl status nginx --no-pager -l

echo -e "\n2. 进程检查:"
ps -ef | grep nginx | grep -v grep

echo -e "\n3. 端口监听:"
netstat -tulnp | grep nginx

echo -e "\n4. 版本信息:"
/opt/nginx/bin/nginx -v

echo -e "\n5. 目录结构:"
tree /opt/nginx -L 2

echo -e "\n6. 测试访问:"
curl -s http://localhost | grep -o "<title>.*</title>"

echo -e "\n7. 状态页面:"
curl -s http://localhost/status
17. 目录结构说明

安装完成后,目录结构如下:

复制代码
/opt/nginx/
├── bin/
│   └── nginx                 # Nginx 可执行文件
├── conf/
│   ├── nginx.conf           # 主配置文件
│   └── mime.types           # MIME 类型文件
├── html/
│   └── index.html           # 默认首页
├── logs/
│   ├── access.log           # 访问日志
│   ├── error.log            # 错误日志
│   ├── nginx.pid            # 进程ID文件
│   └── nginx.lock           # 锁文件
└── cache/
    ├── client_temp/         # 客户端临时文件
    ├── proxy_temp/          # 代理临时文件
    ├── fastcgi_temp/        # FastCGI 临时文件
    ├── uwsgi_temp/          # uWSGI 临时文件
    └── scgi_temp/           # SCGI 临时文件
18. 常用管理命令
bash 复制代码
# 启动服务
sudo systemctl start nginx

# 停止服务
sudo systemctl stop nginx

# 重启服务
sudo systemctl restart nginx

# 重新加载配置
sudo systemctl reload nginx

# 查看服务状态
sudo systemctl status nginx

# 测试配置文件
sudo /opt/nginx/bin/nginx -t

# 查看版本和编译参数
/opt/nginx/bin/nginx -V
19. 卸载方法(如果需要)
bash 复制代码
# 停止服务
sudo systemctl stop nginx
sudo systemctl disable nginx

# 删除服务文件
sudo rm -f /etc/systemd/system/nginx.service
sudo systemctl daemon-reload

# 删除安装目录
sudo rm -rf /opt/nginx

# 删除用户和组
sudo userdel nginx
sudo groupdel nginx

通过以上详细步骤,您已成功在 /opt/nginx 目录下编译安装了 Nginx 1.28.0。现在可以通过 http://服务器IP 访问您的 Nginx 服务器。

相关推荐
qetfw1 小时前
Bash 基础:脚本执行、引号与命令替换验证
linux·开发语言·chrome·bash
怪力左手1 小时前
cuda-toolkit离线安装
linux·运维·人工智能
鬼手点金1 小时前
带工具调用的多轮交互:Agent 的核心循环
服务器·人工智能·windows·深度学习·microsoft·机器学习·交互
无人生还别怕1 小时前
如何搭建k8s服务(docker部署)
linux·运维·docker·云原生·容器·kubernetes·openanolisos
Lsetea1 小时前
curl下载HTTPS正文是乱码:--compressed与Content-Encoding分层排查
linux·http·https·ssl证书·curl
AnTrust1 小时前
代码签名证书品牌价格对比:DigiCert、GlobalSign、Comodo、Certum怎么选
运维·服务器·ssl
硅基手札1 小时前
操作系统设计思想系列 05 · RTOS 核心矛盾 — 资源受限 × 时间确定性的设计决策树
linux·mcu·架构·操作系统·嵌入式开发·rtos·设计思想
程序员-Benothing1 小时前
Linux磁盘管理教程:MBR、GPT、fdisk、parted分区实战
linux·运维·服务器
j7~2 小时前
【Linux网络加餐】《HTTP 会话保持:Cookie 与 Session 原理及实验验证》---详解
linux·网络·网络协议·http·cookie·session