使用编译安装可以定义编译的参数,相较于yum或者dnf安装更灵活。
一、查看要安装的版本
http://nginx.org/en/download.html
这里我们选择的稳定版1.28.0
二、先安装依赖
安装依赖之前最好先执行下update
dnf update
#添加用户
useradd -M -s /sbin/nologin nginx
安装相关依赖
sudo dnf install -y gcc gcc-c++ make zlib-devel openssl-devel wget tar
sudo dnf install pcre2*
三、下载源码
切换到/opt目录下,下载源代码解压
cd /opt/
wget https://nginx.org/download/nginx-1.28.0.tar.gz
tar -zxvf nginx-1.28.0.tar.gz
四、编译源代码
切换目录
cd /opt/nginx-1.28.0
编译nginx,功能
./configure \
--prefix=/usr/local/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-mail \
--with-mail_ssl_module \
--with-file-aio \
--with-stream \
--with-stream_ssl_module \
--with-pcre \
--user=nginx \
--group=nginx
编译成功后的截图
编译安装
make -j$(nproc)
sudo make install
五、创建系统服务文件,以便通过 systemd 管理 Nginx
sudo tee /usr/lib/systemd/system/nginx.service << 'EOF'
[Unit]
Description=The NGINX HTTP and reverse proxy server
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
ExecStart=/usr/local/nginx/sbin/nginx
ExecReload=/usr/local/nginx/sbin/nginx -s reload
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
启动 Nginx 并设置开机自启:
sudo systemctl daemon-reload
sudo systemctl start nginx
sudo systemctl enable nginx
验证 Nginx 是否安装成功:
sudo systemctl status nginx
/usr/local/nginx/sbin/nginx -v
配置防火墙允许 HTTP 和 HTTPS 流量:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --reload
现在,你已经在 Rocky Linux 10 上成功编译安装了最新版的 Nginx。Nginx 的配置文件位于
/usr/local/nginx/conf/nginx.conf,你可以根据需要进行修改。
编译报错的解决办法,没有报错直接跳过
- 报错:./configure: error: the HTTP rewrite module requires the PCRE library.
问题原因:编译安装nginx需要pcre包,未安装的话会报错
解决办法:
sudo dnf install pcre2*