LNMP 一键部署脚本 shell脚本
CentOS 7.9 一键部署 Nginx + PHP7.4 + MariaDB + WordPress 中文
#!/bin/bash
# CentOS 7.9 一键部署 Nginx + PHP7.4 + MariaDB + WordPress 中文(含多次重试下载和写入权限设置)
# 作者:yuc79122@gmail.com
set -e
DB_PASS="Zxc-1234"
DB_NAME="wordpress"
CMS_PATH="/usr/share/nginx/html"
LOCAL_IP=$(hostname -I | awk '{print $1}')
echo ">>> 更换为阿里云YUM源..."
yum install -y epel-release yum-utils
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache fast
echo ">>> 安装 Remi Repo,准备安装 PHP7.4..."
yum install -y http://rpms.remirepo.net/enterprise/remi-release-7.rpm
yum-config-manager --disable 'remi-php*'
yum-config-manager --enable remi-php74
yum clean all
yum makecache fast
echo ">>> 安装 Nginx、MariaDB 和 PHP7.4 及常用扩展..."
yum install -y nginx mariadb-server \
php php-fpm php-cli php-mysqlnd php-mbstring php-xml php-gd php-curl php-zip php-opcache
echo ">>> 启动并开机自启 nginx、php-fpm、mariadb..."
systemctl enable nginx php-fpm mariadb
systemctl start nginx php-fpm mariadb
echo ">>> 初始化 MariaDB,设置root密码及创建wordpress数据库..."
mysql -u root <<EOF
UPDATE mysql.user SET Password=PASSWORD('${DB_PASS}') WHERE User='root';
DELETE FROM mysql.user WHERE User='';
DROP DATABASE IF EXISTS test;
DELETE FROM mysql.db WHERE Db='test' OR Db='test\\_%';
CREATE DATABASE IF NOT EXISTS ${DB_NAME} DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '${DB_PASS}' WITH GRANT OPTION;
FLUSH PRIVILEGES;
EOF
echo ">>> 配置 MariaDB 允许远程连接..."
if grep -q '^bind-address' /etc/my.cnf; then
sed -i 's/^bind-address.*/bind-address=0.0.0.0/' /etc/my.cnf
else
sed -i '/^\[mysqld\]/a bind-address=0.0.0.0' /etc/my.cnf
fi
systemctl restart mariadb
download_wordpress() {
local url="https://cn.wordpress.org/latest-zh_CN.tar.gz"
local file="latest-zh_CN.tar.gz"
local max_retries=5
local count=0
while [ $count -lt $max_retries ]; do
echo "尝试下载 WordPress(第 $((count+1)) 次)..."
wget -c "$url" -O "$file" && break
count=$((count+1))
echo "下载失败,等待3秒后重试..."
sleep 3
done
if [ ! -f "$file" ]; then
echo "错误:下载 WordPress 失败,退出部署!"
exit 1
fi
}
echo ">>> 下载并部署 WordPress 中文版..."
rm -rf ${CMS_PATH}/*
cd ${CMS_PATH}
download_wordpress
tar -xzf latest-zh_CN.tar.gz
mv wordpress/* ./
rm -rf wordpress latest-zh_CN.tar.gz
echo ">>> 配置 WordPress wp-config.php..."
cp wp-config-sample.php wp-config.php
sed -i "s/database_name_here/${DB_NAME}/" wp-config.php
sed -i "s/username_here/root/" wp-config.php
sed -i "s/password_here/${DB_PASS}/" wp-config.php
echo ">>> 设置 WordPress 目录权限..."
chown -R nginx:nginx ${CMS_PATH}
find ${CMS_PATH} -type d -exec chmod 755 {} \;
find ${CMS_PATH} -type f -exec chmod 644 {} \;
chmod -R 775 ${CMS_PATH}/wp-content
echo ">>> 配置 Nginx 虚拟主机..."
cat > /etc/nginx/conf.d/wordpress.conf <<EOF
server {
listen 80;
server_name localhost;
root ${CMS_PATH};
index index.php index.html;
location / {
try_files \$uri \$uri/ /index.php?\$args;
}
location ~ \.php\$ {
include fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg)\$ {
expires max;
log_not_found off;
}
}
EOF
echo ">>> 设置 PHP 时区为上海..."
sed -i 's/;date.timezone =.*/date.timezone = Asia\/Shanghai/' /etc/php.ini
echo ">>> 重启服务..."
nginx -t && systemctl restart nginx php-fpm mariadb
echo ">>> 生成 PHP 测试页..."
echo "<?php phpinfo(); ?>" > ${CMS_PATH}/test.php
chown nginx:nginx ${CMS_PATH}/test.php
echo
echo "✅ 部署完成!"
echo "访问地址:http://${LOCAL_IP}/"
echo "PHP测试页:http://${LOCAL_IP}/test.php"
echo "数据库:root / ${DB_PASS}(远程访问请开放3306端口)"
echo "WordPress 安装时自行设置管理员用户名和密码"
echo "---------------------------------------------"
echo "注意:安装完成后请删除 test.php 以确保安全。"