create-wpsite.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/bin/bash
  2. #### Create WP Site
  3. #### v3 自动创建临时站点shell脚本
  4. #### Author: chenpeiqing
  5. #### Date: 2024-11-07
  6. #### Usage: sh create-wpsite.sh [FILE_NAME] [TEMPLATE_SOURCE_ID]
  7. #### 示例: sh create-wpsite.sh 2408210vakv9 wp063
  8. FILE_NAME=$1
  9. TEMPLATE_SOURCE_ID=$2
  10. #TEMP_DOMAIN=$4
  11. #TEMP_DOMAIN_CNAME=$5
  12. TEMPLATE_DOMAIN='adweb3.topxuetang.com'
  13. RDS_HOST='127.0.0.1'
  14. RDS_ADMIN='root'
  15. RDS_PWD='Initial0_123'
  16. DOMAIN_NAME=$FILE_NAME.$TEMPLATE_DOMAIN
  17. first_time=$(date +'%s')
  18. export PATH=/usr/local/bin:$PATH
  19. #创建站点目录
  20. mkdir /usr/local/wwwroot/$FILE_NAME
  21. #打包模板站包
  22. cd /usr/local/wwwroot/$TEMPLATE_SOURCE_ID
  23. tar -czf $TEMPLATE_SOURCE_ID.tar.gz *
  24. mv $TEMPLATE_SOURCE_ID.tar.gz /usr/local/wwwroot/$FILE_NAME
  25. #下载模板站数据库
  26. 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
  27. echo '解压模板站'
  28. cd /usr/local/wwwroot/$FILE_NAME
  29. tar -xzf $TEMPLATE_SOURCE_ID.tar.gz
  30. echo "解压模板站:"$((end_time - start_time))"s"
  31. #V3 WordPress 服务器支持三级泛域名,无需增加dns记录
  32. #Add Domain
  33. #echo 'add temp domain start'
  34. #start_time=$(date +'%s')
  35. #aliyun alidns AddDomainRecord --DomainName $TEMP_DOMAIN --RR $FILE_NAME --Type CNAME --Value $TEMP_DOMAIN_CNAME --TTL 600
  36. #end_time=$(date +'%s')
  37. #echo "添加临时域名花费:"$((end_time - start_time))"s"
  38. #echo 'add temp domain end'
  39. echo "修改站点【"$FILE_NAME"】wp-config.php配置"
  40. start_time=$(date +'%s')
  41. sed -i "s:root:adweb:g" /usr/local/wwwroot/$FILE_NAME/wp-config.php
  42. sed -i "s:Initial0_123:2024_RqBQxctjakHCJVq8kmmYHQKZQGU4YMUH:g" /usr/local/wwwroot/$FILE_NAME/wp-config.php
  43. sed -i "s:$TEMPLATE_SOURCE_ID:$FILE_NAME:g" /usr/local/wwwroot/$FILE_NAME/wp-config.php
  44. end_time=$(date +'%s')
  45. echo "站点【"$FILE_NAME"】wp-config.php配置修改完成,共计花费:"$((end_time - start_time))"s"
  46. echo 'create_database_start'
  47. start_time=$(date +'%s')
  48. mysql -h$RDS_HOST -u$RDS_ADMIN -p$RDS_PWD<< EOF
  49. DROP DATABASE IF EXISTS $FILE_NAME;
  50. CREATE DATABASE $FILE_NAME CHARACTER SET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  51. GRANT ALL ON $FILE_NAME.* TO 'adweb'@'localhost';
  52. FLUSH PRIVILEGES;
  53. exit
  54. EOF
  55. end_time=$(date +'%s')
  56. echo "创建数据库花费:"$((end_time - start_time))"s"
  57. echo 'create_database_end'
  58. start_time=$(date +'%s')
  59. mysql -u$RDS_ADMIN -p$RDS_PWD $FILE_NAME < /opt/adweb3/temp_package/$TEMPLATE_SOURCE_ID.sql
  60. end_time=$(date +'%s')
  61. echo "【"$FILE_NAME"】网站导入数据库完成,共计花费:"$((end_time - start_time))"s"
  62. echo 'update_wp-config_start'
  63. start_time=$(date +'%s')
  64. cp /opt/adweb3/config/wp-cli.phar /usr/local/wwwroot/$FILE_NAME
  65. sed -i "s:FILE_NAME:$FILE_NAME:g" /usr/local/wwwroot/$FILE_NAME/domain.txt
  66. sed -i "s:'$TEMPLATE_SOURCE_ID':'$FILE_NAME':g" /usr/local/wwwroot/$FILE_NAME/wp-config.php
  67. end_time=$(date +'%s')
  68. echo "更新WP配置文件花费:"$((end_time - start_time))"s"
  69. echo 'update_wp-config_end'
  70. #echo 'update_site_chown_start'
  71. #start_time=$(date +'%s')
  72. #chown -R nginx:nginx /usr/local/wwwroot/$FILE_NAME/
  73. #end_time=$(date +'%s')
  74. #echo "更新站点用户组花费:"$((end_time - start_time))"s"
  75. #echo 'update_site_chown__end'
  76. echo 'WP_UDONM_START'
  77. start_time=$(date +'%s')
  78. cd /usr/local/wwwroot/$FILE_NAME
  79. cp /opt/adweb3/config/wp-cli.phar ./
  80. php ./wp-cli.phar search-replace "$TEMPLATE_SOURCE_ID.$TEMPLATE_DOMAIN" "$DOMAIN_NAME" --allow-root
  81. rm -rf wp-cli.phar
  82. end_time=$(date +'%s')
  83. echo "更新WP域名花费:"$((end_time - start_time))"s"
  84. echo 'WP_UDONM_END'
  85. #nginx
  86. echo 'UPDATA_NGINX_START'
  87. start_time=$(date +'%s')
  88. cp /opt/adweb3/nginx_template/wp-site.conf /usr/local/openresty/nginx/vhost/$FILE_NAME.conf
  89. sed -i "s:CUSTOM_DOMAIN:$DOMAIN_NAME:g" /usr/local/openresty/nginx/vhost/$FILE_NAME.conf
  90. sed -i "s:SITE_CODE:$FILE_NAME:g" /usr/local/openresty/nginx/vhost/$FILE_NAME.conf
  91. echo "开始重启nginx配置"
  92. /usr/local/openresty/nginx/sbin/nginx -s reload
  93. echo "nginx配置重启完成"echo "开始重启nginx配置"
  94. end_time=$(date +'%s')
  95. echo "本次拉取临时站点共计花费: "$((end_time - first_time))"s"
  96. cp /opt/adweb3/config/wp-cli.phar /usr/local/wwwroot/$FILE_NAME
  97. cd /usr/local/wwwroot/$FILE_NAME
  98. php ./wp-cli.phar search-replace "http://$DOMAIN_NAME" "https://$DOMAIN_NAME" --allow-root
  99. #echo "开始更新SSL证书"
  100. #start_time=$(date +'%s')
  101. #cd /home/ubuntu/remote_old_devcli
  102. #sh https-wesite-deploy.sh $FILE_NAME
  103. #echo "更新SSL证书花费:"$((end_time - start_time))"s"
  104. #更新目录权限组
  105. cd /usr/local/wwwroot
  106. chown -R www-data:www-data $FILE_NAME