
CentOS Stream 9 一键源码安装 Nginx 脚本复制下面全部内容保存为install_nginx.sh直接运行即可全自动编译、安装、开机自启、无交互。#!/bin/bashset-e# 版本与路径NGINX_VER1.26.2INSTALL_DIR/usr/local/nginxSRC_DIR/usr/local/srcecho 安装依赖 dnfinstall-ygcc gcc-cmakepcre pcre-devel zlib zlib-devel openssl openssl-develecho 下载 Nginx${NGINX_VER}cd${SRC_DIR}wget-Nhttp://nginx.org/download/nginx-${NGINX_VER}.tar.gzecho 解压 tar-zxfnginx-${NGINX_VER}.tar.gzcdnginx-${NGINX_VER}echo 配置编译参数 ./configure\--prefix${INSTALL_DIR}\--with-http_ssl_module\--with-http_gzip_static_module\--with-http_stub_status_moduleecho 编译安装 make-j$(nproc)makeinstallecho 创建命令软链接 ln-sf${INSTALL_DIR}/sbin/nginx /usr/local/sbin/echo 配置 systemd 服务 cat/usr/lib/systemd/system/nginx.serviceEOF [Unit] DescriptionNginx Afternetwork.target [Service] Typeforking ExecStart${INSTALL_DIR}/sbin/nginx ExecReload${INSTALL_DIR}/sbin/nginx -s reload ExecStop${INSTALL_DIR}/sbin/nginx -s stop PrivateTmptrue [Install] WantedBymulti-user.target EOFecho 启动并开机自启 systemctl daemon-reload systemctlenable--nownginxecho 检查状态 nginx-Vsystemctl status nginx --no-pagerecho-e\n\033[32m Nginx 安装完成\033[0mecho安装目录${INSTALL_DIR}echo配置文件${INSTALL_DIR}/conf/nginx.confecho网页目录${INSTALL_DIR}/htmlecho启动/停止/重启systemctl start|stop|restart nginx使用方法创建脚本viminstall_nginx.sh粘贴上面内容保存退出。加执行权限chmodx install_nginx.sh执行一键安装./install_nginx.sh安装完成后访问 IP 即可看到 Nginx 欢迎页支持systemctl start/stop/restart nginx已自动设置开机自启自带 SSL、gzip、状态监控模块