#!/bin/bash #### Create WP Site #### v3 自动创建临时站点shell脚本 #### Author: chenpeiqing #### Date: 2024-11-07 #### Usage: sh create-wpsite.sh [FILE_NAME] [TEMPLATE_SOURCE_ID] #### 示例: sh create-wpsite.sh 2408210vakv9 wp063 FILE_NAME=$1 TEMPLATE_SOURCE_ID=$2 #TEMP_DOMAIN=$4 #TEMP_DOMAIN_CNAME=$5 TEMPLATE_DOMAIN='adweb3.topxuetang.com' RDS_HOST='127.0.0.1' RDS_ADMIN='root' RDS_PWD='Initial0_123' DOMAIN_NAME=$FILE_NAME.$TEMPLATE_DOMAIN first_time=$(date +'%s') export PATH=/usr/local/bin:$PATH #创建站点目录 mkdir /usr/local/wwwroot/$FILE_NAME #打包模板站包 cd /usr/local/wwwroot/$TEMPLATE_SOURCE_ID tar -czf $TEMPLATE_SOURCE_ID.tar.gz * mv $TEMPLATE_SOURCE_ID.tar.gz /usr/local/wwwroot/$FILE_NAME #下载模板站数据库 mysqldump --user=$RDS_ADMIN --password=$RDS_PWD --host=localhost --port=3306 --no-create-db --default-character-set=utf8 $TEMPLATE_SOURCE_ID > /opt/adweb3/temp_package/$TEMPLATE_SOURCE_ID.sql echo '解压模板站' cd /usr/local/wwwroot/$FILE_NAME tar -xzf $TEMPLATE_SOURCE_ID.tar.gz echo "解压模板站:"$((end_time - start_time))"s" #V3 WordPress 服务器支持三级泛域名,无需增加dns记录 #Add Domain #echo 'add temp domain start' #start_time=$(date +'%s') #aliyun alidns AddDomainRecord --DomainName $TEMP_DOMAIN --RR $FILE_NAME --Type CNAME --Value $TEMP_DOMAIN_CNAME --TTL 600 #end_time=$(date +'%s') #echo "添加临时域名花费:"$((end_time - start_time))"s" #echo 'add temp domain end' echo "修改站点【"$FILE_NAME"】wp-config.php配置" start_time=$(date +'%s') sed -i "s:root:adweb:g" /usr/local/wwwroot/$FILE_NAME/wp-config.php sed -i "s:Initial0_123:2024_RqBQxctjakHCJVq8kmmYHQKZQGU4YMUH:g" /usr/local/wwwroot/$FILE_NAME/wp-config.php sed -i "s:$TEMPLATE_SOURCE_ID:$FILE_NAME:g" /usr/local/wwwroot/$FILE_NAME/wp-config.php end_time=$(date +'%s') echo "站点【"$FILE_NAME"】wp-config.php配置修改完成,共计花费:"$((end_time - start_time))"s" echo 'create_database_start' start_time=$(date +'%s') mysql -h$RDS_HOST -u$RDS_ADMIN -p$RDS_PWD<< EOF DROP DATABASE IF EXISTS $FILE_NAME; CREATE DATABASE $FILE_NAME CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci; GRANT ALL ON $FILE_NAME.* TO 'adweb'@'localhost'; FLUSH PRIVILEGES; exit EOF end_time=$(date +'%s') echo "创建数据库花费:"$((end_time - start_time))"s" echo 'create_database_end' start_time=$(date +'%s') mysql -u$RDS_ADMIN -p$RDS_PWD $FILE_NAME < /opt/adweb3/temp_package/$TEMPLATE_SOURCE_ID.sql end_time=$(date +'%s') echo "【"$FILE_NAME"】网站导入数据库完成,共计花费:"$((end_time - start_time))"s" echo 'update_wp-config_start' start_time=$(date +'%s') cp /opt/adweb3/config/wp-cli.phar /usr/local/wwwroot/$FILE_NAME sed -i "s:FILE_NAME:$FILE_NAME:g" /usr/local/wwwroot/$FILE_NAME/domain.txt sed -i "s:'$TEMPLATE_SOURCE_ID':'$FILE_NAME':g" /usr/local/wwwroot/$FILE_NAME/wp-config.php end_time=$(date +'%s') echo "更新WP配置文件花费:"$((end_time - start_time))"s" echo 'update_wp-config_end' #echo 'update_site_chown_start' #start_time=$(date +'%s') #chown -R nginx:nginx /usr/local/wwwroot/$FILE_NAME/ #end_time=$(date +'%s') #echo "更新站点用户组花费:"$((end_time - start_time))"s" #echo 'update_site_chown__end' echo 'WP_UDONM_START' start_time=$(date +'%s') cd /usr/local/wwwroot/$FILE_NAME cp /opt/adweb3/config/wp-cli.phar ./ php ./wp-cli.phar search-replace "$TEMPLATE_SOURCE_ID.$TEMPLATE_DOMAIN" "$DOMAIN_NAME" --allow-root rm -rf wp-cli.phar end_time=$(date +'%s') echo "更新WP域名花费:"$((end_time - start_time))"s" echo 'WP_UDONM_END' #nginx echo 'UPDATA_NGINX_START' start_time=$(date +'%s') cp /opt/adweb3/nginx_template/wp-site.conf /usr/local/openresty/nginx/vhost/$FILE_NAME.conf sed -i "s:CUSTOM_DOMAIN:$DOMAIN_NAME:g" /usr/local/openresty/nginx/vhost/$FILE_NAME.conf sed -i "s:SITE_CODE:$FILE_NAME:g" /usr/local/openresty/nginx/vhost/$FILE_NAME.conf echo "开始重启nginx配置" /usr/local/openresty/nginx/sbin/nginx -s reload echo "nginx配置重启完成"echo "开始重启nginx配置" end_time=$(date +'%s') echo "本次拉取临时站点共计花费: "$((end_time - first_time))"s" cp /opt/adweb3/config/wp-cli.phar /usr/local/wwwroot/$FILE_NAME cd /usr/local/wwwroot/$FILE_NAME php ./wp-cli.phar search-replace "http://$DOMAIN_NAME" "https://$DOMAIN_NAME" --allow-root #echo "开始更新SSL证书" #start_time=$(date +'%s') #cd /home/ubuntu/remote_old_devcli #sh https-wesite-deploy.sh $FILE_NAME #echo "更新SSL证书花费:"$((end_time - start_time))"s" #更新目录权限组 cd /usr/local/wwwroot chown -R www-data:www-data $FILE_NAME