#!/bin/bash
#-----------------------------------------------
# 脚本名称:     LNMP_WordPress.sh
# 作者:             whao
# 创建时间:     2025-04-26
# 版本:           v0.3
# 描述：Ubuntu系统使用LNMP架构搭建WordPress博客
#------------------------------------------------

# 颜色定义
W="\e[0m"
R="\e[31m"
G="\e[32m"
Y="\e[33m"
B="\e[34m"
P="\e[35m"

echo -e "$R=============使用脚本之前请先修改脚本==============$W"
echo -e "$R       脚本第144行：将IP改为您自己的IP或域名       $W"
echo -e "$R      注意！！！ 本脚本目前仅限ubuntu系统使用      $W"
echo -e "$R===================================================$W"
echo "可以使用命令 vim LNMP_wordpress.sh +144 "
read -p "确定已经修改过脚本吗？(y/n) " QURE
if [ "${QURE}" = "y" ]; then
    echo "好的！脚本即将执行"
else
    echo "麻烦您修改脚本后执行"
    exit 1
fi




# 更新源
echo -e "$R=============进入软件服务安装阶段==============$W"
read -t1

apt update &>/dev/null
if [ $? -eq 0 ]; then
    echo "软件包列表更新成功"
else
    echo "软件包列表更新失败"
    exit 1
fi

# 安装和配置防火墙
apt install firewalld -y &>/dev/null
if [ $? -eq 0 ]; then
    echo "防火墙下载安装完成"
    systemctl stop firewalld
    echo "关闭防火墙"
    systemctl disable firewalld
    echo "已设置防火墙开机不自启"
else
    systemctl disable firewalld
    echo "已设置防火墙开机不自启"
fi

# 下载服务软件
apt install nginx -y &>/dev/null
if [ $? -eq 0 ]; then
    echo "Nginx下载安装完成"
    systemctl start nginx
    systemctl enable nginx
    echo "Nginx已设置开启"
else
    echo "Nginx下载失败"
    exit 1
fi

apt install mysql-server -y &>/dev/null
if [ $? -eq 0 ]; then
    echo "MySQL下载安装完成"
    systemctl start mysql
    if [ $? -eq 0 ]; then
        echo "MySQL启动成功"
        systemctl enable mysql
        echo "MySQL已设置开机自启"
    else
        echo "MySQL启动失败"
        exit 1
    fi
else
    echo "MySQL下载失败"
    exit 1
fi

apt install php-fpm php-mysql php-curl php-gd php-intl php-mbstring php-soap php-xml php-xmlrpc php-zip -y  &>/dev/null
if [ $? -eq 0 ]; then
    echo "php依赖 下载安装完成"
else
    echo "php依赖下载失败"
    exit 1
fi

# 进入工具安装阶段
echo -e "$R=============进入工具安装阶段==============$W"
declare -A tools=( ["wget"]="wget" ["curl"]="curl" ["ifconfig"]="net-tools" ["unzip"]="unzip" )
for tool in "${!tools[@]}"; do
    which $tool &>/dev/null
    if [ $? -eq 0 ]; then
        echo "$tool 命令已有"
    else
        echo "$tool 命令下载中"
        apt install ${tools[$tool]} -y &>/dev/null
        if [ $? -eq 0 ]; then
            echo "$tool 命令下载完成"
        else
            echo "$tool 命令下载失败"
            exit 1
        fi
    fi
done

# 进入MySQL定制阶段
echo -e "$G==============进入MySQL定制阶段=============$W"
# 设定MySQL登录信息
MYSQL_USER="root"
MYSQL_PASSWORD="123456"

# 设定MySQL命令
MySQL_COMMANDS="
CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
CREATE USER 'wordpressuser'@'localhost' IDENTIFIED BY '123456';
GRANT ALL ON wordpress.* TO 'wordpressuser'@'localhost';
FLUSH PRIVILEGES;
"
# 执行MySQL命令
mysql -u "$MYSQL_USER" -p"$MYSQL_PASSWORD" <<< "$MySQL_COMMANDS"
if [ $? -eq 0 ]; then
    echo "MySQL数据库和用户创建成功"
else
    echo "MySQL数据库和用户创建失败"
    exit 1
fi

# 进入PHP配置阶段
echo -e "$R=============进入PHP配置阶段==============$W"
touch /etc/nginx/sites-available/wordpress

echo 'server {
    listen 80;
    # 设置域名或IP地址
    server_name 10.0.0.16;

    # WordPress安装目录
    root /var/www/wordpress;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.3-fpm.sock;
        # 根据你的PHP版本调整
    }

    location ~ /\.ht {
        deny all;
    }
    }' > /etc/nginx/sites-available/wordpress

ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
if [ $? -eq 0 ]; then
    echo "已设置软链接"
else
    echo "软链接设置失败"
    exit 1
fi

nginx -t
if [ $? -eq 0 ]; then
    systemctl restart nginx
    echo "nginx配置设置成功"
else
    echo "nginx配置有问题，请注意"
    exit 1
fi

# 安装wordpress
echo -e "$G==============安装wordpress=============$W"
mkdir -p /var/www/wordpress
chown -R www-data:www-data /var/www/wordpress

wget -P /var/www/wordpress https://cn.wordpress.org/latest-zh_CN.zip
if [ $? -eq 0 ]; then
    echo "博客系统安装包下载完成"
else
    echo "博客系统安装包下载失败"
    exit 1
fi

unzip /var/www/wordpress/latest-zh_CN.zip -d /var/www/wordpress
if [ $? -eq 0 ]; then
    echo "博客系统安装包解压完成"
else
    echo "博客系统安装包解压失败"
    exit 1
fi

rm /var/www/wordpress/latest-zh_CN.zip

mv /var/www/wordpress/wordpress/*  /var/www/wordpress/
if [ $? -eq 0 ]; then
    echo "文件移动完成"
else
    echo "文件移动失败"
    exit 1
fi

rm -r /var/www/wordpress/wordpress/
if [ $? -eq 0 ]; then
    echo "临时目录删除完成"
else
    echo "临时目录删除失败"
    exit 1
fi

cp /var/www/wordpress/wp-config-sample.php  /var/www/wordpress/wp-config.php
if [ $? -eq 0 ]; then
    echo "配置文件复制完成"
else
    echo "配置文件复制失败"
    exit 1
fi

sed -i "s/define( 'DB_NAME', '.*' );/define( 'DB_NAME', 'wordpress' );/; s/define( 'DB_USER', '.*' );/define( 'DB_USER', 'wordpressuser' );/; s/define( 'DB_PASSWORD', '.*' );/define( 'DB_PASSWORD', '123456' );/" /var/www/wordpress/wp-config.php
if [ $? -eq 0 ]; then
    echo "配置文件修改完成"
else
    echo "配置文件修改失败"
    exit 1
fi
chown -R www-data:www-data /var/www/wordpress/
if [ $? -eq 0 ]; then
    echo "配置文件权限修改完成"
else
    echo "配置文件权限修改失败"
    exit 1
fi


echo -e "$G=======================================================================$W"
echo -e "$R     WordPress安装完成，请在浏览器中访问服务器IP地址完成后续配置       $W"
echo -e "$R 网页提示502，请更改/etc/nginx/sites-available/wordpress文件中的php版本$W"
echo -e "$G=======================================================================$W"
sleep 1
echo -e "$P\n=====================================================================$W"
echo -e "$P\n        自筹划伊始，便如执笔绘山河，一砖一瓦，皆倾注心血。           $W"
sleep 1
echo -e "$P\n 看那云端广厦，渐次拔地而起，飞檐斗拱，雕梁画栋，恍若天上宫阙落人间。$W"
sleep 1
echo -e "$P\n              今时落成，恰似瑶池初现，玉宇生辉。                     $W"
sleep 1
echo -e "$P\n            诸位雅士，且整衣冠，共赴这场云端盛宴。                   $W"
sleep 1
echo -e "$P\n            同赏这数字仙境之妙，不负此番心血与韶光。                 $W"
echo -e "$P\n=====================================================================$W"
sleep 2

exit 0