1、nginxhttps://nginx.org/download/nginx-1.28.1.tar.gz2、zlibhttps://www.zlib.net/zlib-1.3.1.tar.gz3、pcre2https://github.com/PCRE2Project/pcre2/releases/download/pcre2-10.47/pcre2-10.47.tar.gz4、opensslhttps://github.com/openssl/openssl/releases/download/openssl-3.5.4/openssl-3.5.4.tar.gz
Rocky Linux默认使用的是firewall作为防火墙firewall-cmd --list-all #显示所有规则(含服务、端口、区域)systemctl status firewalld #检查 firewalld 状态#开放80 443 端口firewall-cmd --permanent --add-port=80/tcpfirewall-cmd --permanent --add-port=443/tcpfirewall-cmd --reload #重新加载防火墙配置
vi /etc/selinux/config#SELINUX=enforcing #注释掉#SELINUXTYPE=targeted #注释掉SELINUX=disabled #增加:wq! #保存退出setenforce 0 #使配置立即生效getenforce #查看 SELinux 当前运行模式
mkdir -p /data/server/nginxmkdir -p /data/server/nginx/packagesmkdir -p /data/server/nginx/install上传安装包到/data/server/nginx/packages目录
yum install tar make gcc gcc-c++ perl pcre2-devel zlib-devel glibc-develcd /data/server/nginx/packagestar zxvf pcre2-10.47.tar.gz源码解压后路径是;/data/server/nginx/packages/pcre2-10.47不需要安装
cd /data/server/nginx/packagestar zxvf zlib-1.3.1.tar.gz源码解压后路径是;/data/server/nginx/packages/zlib-1.3.1不需要安装
cd /data/server/nginx/packagestar zxvf openssl-3.5.4.tar.gz源码解压后路径是;/data/server/nginx/packages/openssl-3.5.4不需要安装
#nginx默认运行账号和组是Linux系统的内置账号和组nobody#创建nginx运行账号和组groupadd wwwuseradd -g www www -s /sbin/nologincd /data/server/nginx/packagestar zxvf nginx-1.28.1.tar.gzcd nginx-1.28.1./configure \--prefix=/data/server/nginx \--user=www \--group=www \--without-http_memcached_module \--with-http_stub_status_module \--with-http_ssl_module \--with-http_v2_module \--with-http_gzip_static_module \--with-http_realip_module \--with-stream \--with-stream_ssl_preread_module \--with-http_flv_module \--with-http_mp4_module \--with-http_sub_module \--http-client-body-temp-path=/data/server/nginx/client \--http-proxy-temp-path=/data/server/nginx/proxy \--http-fastcgi-temp-path=/data/server/nginx/fcgi \--http-uwsgi-temp-path=/data/server/nginx/uwsgi \--with-openssl=/data/server/nginx/packages/openssl-3.5.4 \--with-zlib=/data/server/nginx/packages/zlib-1.3.1 \--with-pcre=/data/server/nginx/packages/pcre2-10.47makemake install
注意:这个几个参数的路径指向的是源码包解压的路径,不是安装的路径。--with-openssl=/data/server/nginx/packages/openssl-3.5.4--with-zlib=/data/server/nginx/packages/zlib-1.3.1--with-pcre=/data/server/nginx/packages/pcre2-10.47
/data/server/nginx/sbin/nginx #启动Nginx#查看nginx版本和安装模块信息/data/server/nginx/sbin/nginx -V

vi /data/server/nginx/nginx.sh#!/bin/bashNGINX_PATH="/data/server/nginx/sbin/nginx"PID_FILE="/data/server/nginx/logs/nginx.pid"function start_nginx() {if [ -f $PID_FILE ]; thenecho "Nginx is already running."elseecho "Starting Nginx..."$NGINX_PATHecho "Nginx started."fi}function stop_nginx() {if [ -f $PID_FILE ]; thenecho "Stopping Nginx..."$NGINX_PATH -s stopecho "Nginx stopped."elseecho "Nginx is not running."fi}function restart_nginx() {if [ -f $PID_FILE ]; thenecho "Restarting Nginx..."$NGINX_PATH -s stopsleep 1$NGINX_PATHecho "Nginx restarted."elseecho "Nginx is not running. Starting it now..."$NGINX_PATHecho "Nginx started."fi}function reload_nginx() {if [ -f $PID_FILE ]; thenecho "Reloading Nginx configuration..."$NGINX_PATH -s reloadecho "Nginx configuration reloaded."elseecho "Nginx is not running. Cannot reload the configuration."fi}function status_nginx() {if [ -f $PID_FILE ]; thenecho "Nginx is running with PID $(cat $PID_FILE)."elseecho "Nginx is stopped."fi}case "$1" instart)start_nginx;;stop)stop_nginx;;restart)restart_nginx;;reload)reload_nginx;;status)status_nginx;;*)echo "Usage: $0 {start|stop|restart|reload|status}"exit 1;;esac:wq! #保存退出#添加执行权限chmod +x /data/server/nginx/nginx.sh#添加开机启动vi /etc/rc.d/rc.local/bin/sh /data/server/nginx/nginx.sh start:wq! #保存退出#默认/etc/rc.local没有执行权限,需要手动添加执行权限chmod +x /etc/rc.d/rc.local/bin/sh /data/server/nginx/nginx.sh restart
vi /lib/systemd/system/nginx.service #添加以下代码[Unit]Description=The NGINX HTTP and reverse proxy serverAfter=syslog.target network.target remote-fs.target nss-lookup.target[Service]Type=forkingPIDFile=/data/server/nginx/logs/nginx.pidExecStartPre=/data/server/nginx/sbin/nginx -tExecStart=/data/server/nginx/sbin/nginxExecReload=/data/server/nginx/sbin/nginx -s reloadExecStop=/bin/kill -s QUIT $MAINPIDExecStartPost=/bin/sleep 0.1PrivateTmp=true[Install]WantedBy=multi-user.target:wq! #保存退出/data/server/nginx/sbin/nginx -s stop #停止systemctl daemon-reload #重载 systemd 配置systemctl enable nginx.service #设置开机自启动systemctl start nginx.service #启动systemctl stop nginx.service #关闭systemctl restart nginx.service #重启systemctl reload nginx.service #重新加载配置文件
2.7测试nginx
浏览器打开http://192.168.21.159/
