在Rocky Linux 9系统中编译安装Nginx
作者 hxwsq_华 于 2024-12-10 09:37:00.0 发表于 浙江 最近修改于 2025-04-07 15:17:31.0 本文浏览量:145人次
一、说明
1.1 操作系统版本
本文使用的操作系统版本:Rocky Linux 9.5
cat /etc/rocky-release

1.2 准备工作
二、步骤
2.1 获取Nginx最新源码包并解压
wget https://nginx.org/download/nginx-1.26.2.tar.gz
tar -zxf nginx-1.26.2.tar.gz

2.2 安装所需的依赖包
dnf -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel

2.3 创建系统用户nginx
groupadd nginx
useradd -r -g nginx -s /sbin/nologin nginx

2.4 配置Nginx
具体的Nginx配置选项可参考官方文档
cd nginx-1.26.2
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-threads \
--with-file-aio \
--with-http_ssl_module \
--with-http_realip_module \
--with-http_sub_module \
--with-http_gunzip_module \
--with-http_stub_status_module \
--with-stream \
--with-stream_ssl_module \
--with-stream_realip_module

2.5 编译Nginx
make

2.6 安装Nginx
make install

2.7 将Nginx配置成系统服务
sed -i 's/^#pid/pid/g' /usr/local/nginx/conf/nginx.conf
cat << 'EOF' > /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/usr/local/nginx/logs/nginx.pid
ExecStartPre=/usr/local/nginx/sbin/nginx -t -c /usr/local/nginx/conf/nginx.conf
ExecStart=/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF

2.8 启动Nginx并配置开机自启动
systemctl daemon-reload
systemctl enable --now nginx.service
