CentOS Stream 9 专用
Nginx 1.26.2 + MySQL 8.4.9 + PHP 8.2 三合一 LNMP 全自动一键脚本
全程无需干预,跑完就能用,支持 PHP、MySQL 远程、Nginx 开机自启,适配你当前环境。
bash
#!/bin/bash
set -euo pipefail
# ==================== 版本配置 ====================
NGINX_VER="1.26.2"
MYSQL_VER="8.4.9"
PHP_VER="8.2.27"
# MySQL 密码
MYSQL_ROOT_PWD="Root@123456"
# 路径
NGINX_DIR="/usr/local/nginx"
MYSQL_DIR="/usr/local/mysql"
MYSQL_DATA="/data/mysql"
PHP_DIR="/usr/local/php"
WEB_DIR="/usr/local/nginx/html"
# ==================================================
echo -e "\033[32m===== 关闭防火墙 & SELinux =====\033[0m"
systemctl stop firewalld && systemctl disable firewalld
setenforce 0
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
echo -e "\033[32m===== 安装基础依赖 =====\033[0m"
dnf install -y epel-release
dnf install -y gcc gcc-c++ make autoconf \
pcre pcre-devel zlib zlib-devel openssl-devel \
libxml2-devel sqlite-devel libpng-devel \
libjpeg-turbo-devel freetype-devel \
libcurl-devel libicu-devel oniguruma-devel \
libaio-devel numactl wget tar
echo -e "\033[32m===== 安装 Nginx ${NGINX_VER} =====\033[0m"
cd /usr/local/src
wget -N http://nginx.org/download/nginx-${NGINX_VER}.tar.gz
tar zxf nginx-${NGINX_VER}.tar.gz
cd nginx-${NGINX_VER}
./configure --prefix=${NGINX_DIR} \
--with-http_ssl_module \
--with-http_gzip_static_module
make -j$(nproc)
make install
ln -sf ${NGINX_DIR}/sbin/nginx /usr/local/sbin/
cat >/usr/lib/systemd/system/nginx.service <<EOF
[Unit]
Description=Nginx
After=network.target
[Service]
Type=forking
ExecStart=${NGINX_DIR}/sbin/nginx
ExecReload=${NGINX_DIR}/sbin/nginx -s reload
ExecStop=${NGINX_DIR}/sbin/nginx -s stop
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now nginx
echo -e "\033[32m===== 安装 MySQL ${MYSQL_VER} =====\033[0m"
cd /usr/local/src
MYSQL_PKG="mysql-${MYSQL_VER}-linux-glibc2.28-x86_64"
wget -N https://cdn.mysql.com/Downloads/MySQL-8.4/${MYSQL_PKG}.tar.xz
tar xf ${MYSQL_PKG}.tar.xz
mv ${MYSQL_PKG} ${MYSQL_DIR}
id -u mysql >/dev/null 2>&1 || useradd -r -s /sbin/nologin mysql
mkdir -p ${MYSQL_DATA}
chown -R mysql:mysql ${MYSQL_DATA}
chmod 750 ${MYSQL_DATA}
cat >/etc/my.cnf <<EOF
[mysqld]
user=mysql
basedir=${MYSQL_DIR}
datadir=${MYSQL_DATA}
socket=/tmp/mysql.sock
port=3306
character-set-server=utf8mb4
bind-address=0.0.0.0
EOF
${MYSQL_DIR}/bin/mysqld --initialize-insecure --user=mysql --datadir=${MYSQL_DATA} --basedir=${MYSQL_DIR}
cat >/usr/lib/systemd/system/mysqld.service <<EOF
[Unit]
Description=MySQL Server
After=network.target
[Service]
User=mysql
Group=mysql
ExecStart=${MYSQL_DIR}/support-files/mysql.server start
ExecStop=${MYSQL_DIR}/support-files/mysql.server stop
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now mysqld
sleep 3
${MYSQL_DIR}/bin/mysql -uroot -e "
ALTER USER 'root'@'localhost' IDENTIFIED BY '${MYSQL_ROOT_PWD}';
CREATE USER 'root'@'%' IDENTIFIED BY '${MYSQL_ROOT_PWD}';
GRANT ALL ON *.* TO 'root'@'%' WITH GRANT OPTION;
FLUSH PRIVILEGES;
"
echo "export PATH=\$PATH:${MYSQL_DIR}/bin" >/etc/profile.d/mysql.sh
source /etc/profile.d/mysql.sh
echo -e "\033[32m===== 安装 PHP ${PHP_VER} =====\033[0m"
cd /usr/local/src
wget -N https://www.php.net/distributions/php-${PHP_VER}.tar.gz
tar zxf php-${PHP_VER}.tar.gz
cd php-${PHP_VER}
./configure --prefix=${PHP_DIR} \
--with-fpm-user=nginx \
--with-fpm-group=nginx \
--enable-fpm \
--with-mysqli \
--with-pdo-mysql \
--with-openssl \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg \
--with-freetype \
--enable-mbstring \
--enable-xml \
--enable-bcmath \
--enable-ftp \
--enable-opcache
make -j$(nproc)
make install
cp php.ini-production ${PHP_DIR}/lib/php.ini
cp ${PHP_DIR}/etc/php-fpm.conf.default ${PHP_DIR}/etc/php-fpm.conf
cp ${PHP_DIR}/etc/php-fpm.d/www.conf.default ${PHP_DIR}/etc/php-fpm.d/www.conf
cat >/usr/lib/systemd/system/php-fpm.service <<EOF
[Unit]
Description=PHP FastCGI Process Manager
After=network.target
[Service]
ExecStart=${PHP_DIR}/sbin/php-fpm --nodaemonize
ExecReload=/bin/kill -USR2 \$MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
systemctl daemon-reload
systemctl enable --now php-fpm
echo -e "\033[32m===== 配置 Nginx 支持 PHP =====\033[0m"
cat >${NGINX_DIR}/conf/nginx.conf <<EOF
worker_processes auto;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
root ${WEB_DIR};
index index.php index.html index.htm;
location / {
try_files \$uri \$uri/ /index.php?\$query_string;
}
location ~ \\.php\$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
include fastcgi_params;
}
}
}
EOF
nginx -t
nginx -s reload
echo -e "\033[32m===== 测试 PHP 页面 =====\033[0m"
echo "<?php phpinfo(); ?>" > ${WEB_DIR}/info.php
echo -e "\033[32m=======================================\033[0m"
echo -e "\033[32m LNMP 安装完成!\033[0m"
echo -e "\033[32m=======================================\033[0m"
echo "Nginx: ${NGINX_DIR}"
echo "MySQL: ${MYSQL_DIR} 密码: ${MYSQL_ROOT_PWD}"
echo "PHP: ${PHP_DIR}"
echo "网站根目录: ${WEB_DIR}"
echo "访问测试: http://本机IP/info.php"
echo "MySQL 登录: mysql -uroot -p${MYSQL_ROOT_PWD}"
使用方法
bash
# 1. 创建脚本
vim lnmp_onekey.sh
# 2. 粘贴上面全部代码,保存
# 3. 加执行权限
chmod +x lnmp_onekey.sh
# 4. 运行(时间较长,耐心等)
./lnmp_onekey.sh
安装完成后
- Nginx 1.26.2 ✅
- MySQL 8.4.9 ✅ 密码:
Root@123456 - PHP 8.2.27 + php-fpm ✅
- 支持
.php解析 ✅ - 支持 MySQL 远程连接 ✅
- 全部开机自启 ✅
访问测试:
http://你的IP/info.php