CentOS-Stream-9 编译安装 Nginx

作者:胖鲨鱼 发布时间: 2025-11-09 阅读量:7

安装 Nginx

创建用户
[root@localhost ~]# useradd test001
安装依赖包
[root@localhost ~]# yum groupinstall -y "Development tools"
[root@localhost ~]# yum install -y gcc gcc-c++ autoconf automake
[root@localhost ~]# yum install -y openssl-devel expat-devel
[root@localhost ~]# yum install -y libev libev-devel zlib-devel
[root@localhost ~]# yum install -y gd-devel
安装 pcre
[root@localhost ~]# wget https://sourceforge.net/projects/pcre/files/pcre/8.45/pcre-8.44.tar.gz
[root@localhost ~]# tar zxvf pcre-8.44.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/pcre-8.44/
[root@localhost pcre-8.44]# ./configure --prefix=/usr/local/pcre
[root@localhost pcre-8.44]# make -j $(nproc) && make install
安装 apr
[root@localhost ~]# wget https://dlcdn.apache.org/apr/apr-1.7.4.tar.gz
[root@localhost ~]# tar zxvf apr-1.7.4.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/apr-1.7.4/
​
## 【 报错 】rm: cannot remove ‘libtoolT’: No such file or directory
## 修改 configure 文件
[root@localhost apr-1.7.4]# sed -i 's|$RM "$cfgfile"|# $RM "$cfgfile"|g' ./configure
​
## 继续编译
[root@localhost apr-1.7.4]# ./configure --prefix=/usr/local/apr
[root@localhost apr-1.7.4]# make -j $(nproc) && make install
安装 apr-util
[root@localhost ~]# wget https://dlcdn.apache.org/apr/apr-util-1.6.3.tar.gz
[root@localhost ~]# tar -zxvf apr-util-1.6.3.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/apr-util-1.6.3/
[root@localhost apr-util-1.6.3]# ./configure --prefix=/usr/local/apr-util --with-apr=/usr/local/apr
[root@localhost apr-util-1.6.3]# make -j $(nproc) && make install
安装 nghttp2
[root@localhost ~]# dnf install -y jemalloc jemalloc-devel c-ares-devel
[root@localhost ~]# wget https://github.com/nghttp2/nghttp2/releases/download/v1.65.0/nghttp2-1.65.0.tar.gz
[root@localhost ~]# tar -zxvf nghttp2-1.65.0.tar.gz -C /usr/src/
[root@localhost ~]# cd /usr/src/nghttp2-1.65.0/
[root@localhost nghttp2-1.65.0]# ./configure --prefix=/usr/local/nghttp2
[root@localhost nghttp2-1.65.0]# make -j $(nproc) && make install
#> 配置环境变量
[root@localhost nghttp2-1.65.0]# cat >> /etc/profile << EOF
​
# Nghttp2 Start
export PKG_CONFIG_PATH=/usr/local/nghttp2/lib/pkgconfig:$PKG_CONFIG_PATH
# Nghttp2 End
EOF
​
[root@localhost nghttp2-1.65.0]# source /etc/profile
下载解压
## 下载 nginx-1.27.3.tar.gz 安装包,但下载的时候很慢
[root@localhost ~]# wget https://nginx.org/download/nginx-1.28.0.tar.gz
## 解压 nginx 到指定目录,,假设 nginx 包在当前目录
[root@localhost ~]# tar -zxvf nginx-1.28.0.tar.gz -C /usr/src/
配置选项
## 配置 nginx
## 参数详解:https://blog.csdn.net/lizhengyu891231/article/details/136477249
## --prefix=<path>              Nginx 安装根目录
## --sbin-path=<path>           Nginx 二进制可执行文件目录
## --conf-path=<path>           Nginx 主配置文件目录
## --pid-path=<path>            存储Nginx主进程进程号,可随时改变文件名
## --error-log-path=<path>      主错误、警告、诊断文件名称,可随时改变文件名
## --http-log-path=<path>       Nginx 服务日志文件,可随时改变文件名
## --lock-path=<path>           共享存储器互斥锁文件所在目录          
## --user=<name>                Nginx 进程运行用户
## --group=<groupname>          Nginx 进程运行用户组
## --with-http_ssl_module       启用HTTPS支持,能够处理SSL/TLS加密连接
## --with-http_rewrite_module   会报错,这个不要
​
## 可以通过 nginx -V 查看历史编译选项
​
[root@localhost ~]# cd /usr/src/nginx-1.28.0/
[root@localhost nginx-1.28.0]# ./configure --prefix=/usr/local/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx/nginx.pid --lock-path=/var/lock/nginx.lock --user=test001 --group=test001 --with-http_ssl_module --with-pcre --with-http_stub_status_module --with-http_v2_module --with-http_image_filter_module --with-http_gzip_static_module
编译安装
## 编译 &&  安装
[root@localhost nginx-1.28.0]# make -j $(nproc) && make install
​
## 编译异常,清理编译
[root@localhost nginx-1.28.0]# make clean all
配置文件
[root@localhost ~]# vim /etc/nginx/nginx.conf
user                    test001 test001;  # 运行服务的用户名和用户组
worker_processes        auto;           # 自动根据CPU核心数设置工作进程
worker_cpu_affinity     auto;           # 进程与CPU核心绑定,减少切换
​
# 并发连接限制:避免超CPU/内存承载能力
events {
    worker_connections  1024;           # 每个工作进程的最大连接数(2核建议1024 - 2048)
    use                 epoll;          # 高效事件模型(Linux推荐)
}
​
http {
    # 引入外部配置文件
    include             mime.types;
    
    # 默认文件类型
    default_type        application/octet-stream;
    # 开启文件传输模式
    sendfile            on;
​
    # http2 可以放在 http 进行全局配置,也可以放在 server 针对特定服务器进行局部配置;
    # 新版本的 http2 如以下配置,不再会有报警信息;
    # large_client_header_buffers 替换原有的 http2_max_field_size 和 http2_max_header_size
    http2               on;             # 启用 HTTP/2 协议,优化并发请求处理,降低移动端延迟;
    large_client_header_buffers 4 16k;  # 4 个缓冲区,单个缓冲区16k(建议值,可根据业务调整);
​
    # 添加这一行来为.mjs文件设置正确的MIME类型
    types {
        text/javascript mjs;
    }
​
    # 连接池优化:减少连接创建销毁开销
    keepalive_timeout 65;          # 长连接超时时间,默认65s,无需过长
    keepalive_requests 100;        # 单个长连接最多处理100个请求后关闭
    
    server {
        listen      80;
        server_name localhost;
        root        /var/www/html/test001;
        index       index.html index.htm;
​
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
​
}
配置服务
服务文件
[root@localhost ~]# cat >> /etc/systemd/system/nginx.service << EOF
[Unit]
Description=Nginx Server
After=network.target
​
[Service]
Type=forking
RuntimeDirectory=nginx
PIDFile=/var/run/nginx/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -q
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=-/sbin/start-stop-daemon --quiet --stop --retry QUIT/5 --pidfile /var/run/nginx/nginx.pid
TimeoutStopSec=5
KillMode=mixed
​
[Install]
WantedBy=multi-user.target
​
EOF
配置服务
## 随系统启动
[root@localhost ~]# systemctl enable nginx
​
## 启动服务
[root@localhost ~]# systemctl start nginx
站点文件
[root@localhost ~]#  mkdir -p /var/www/html/test001/
[root@localhost ~]#  cat >> /var/www/html/test001/h.html << EOF
<html>
    <head>
        <title>Welcome to TestWeb</title>
    </head>
    <body>
        <h1>Hello from mytest.example.com!</h1>
    </body>
</html>
EOF
防火墙配置
## 开放 80 端口,如果非常规 80 端口,除了防火墙要配置外,还需要通过 semange 配置 selinux
[root@localhost ~]# firewall-cmd --add-port=80/tcp --permanent
[root@localhost ~]# firewall-cmd --reload

到这里,重启以下 Nginx 服务,就完成了在CentOS-Stream-9系统编译安装Nginx。