migrate_multiphp.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #!/bin/bash
  2. # Includes
  3. source $HESTIA/func/main.sh
  4. source $HESTIA/conf/hestia.conf
  5. #
  6. # Migrate legacy multiphp to full php-fpm backend
  7. #
  8. # nginx+fpm (default)
  9. # nothing to be done here,
  10. # (Adding new php backends will make them available on edit/web)
  11. #
  12. # nginx+multiphp,
  13. # nginx+apache+multiphp,
  14. # apache+multiphp:
  15. # Change Hestia WEB_BACKEND from null to php-fpm
  16. # Create backend templates ex: PHP-7_3, PHP-5_6 (in $HESTIA/data/templates/web/php-fpm/)
  17. # v-update-web-templates
  18. # Loop through all web domains
  19. # If official multiphp tpl is used ex: PHP-72, then change backend tpl and set app web template to default
  20. # ( old default.tpl backend maps to PHP-7_3 )
  21. # If not, parse php version from tpl file , fallback to latest version,
  22. # Copy all non-official tpls to php-fpm folder (as app web template includin bash script if present)
  23. #
  24. # a2 (non-fpm) or nginx+a2(non-fpm)
  25. # - Skipped
  26. #
  27. DEFAULT_BTPL="PHP-7_3"
  28. num_php_versions=$(ls -d /etc/php/*/fpm/pool.d 2>/dev/null |wc -l)
  29. echo "Found $num_php_versions php versions"
  30. if [ "$num_php_versions" -gt 1 ] && [ -z "$WEB_BACKEND" ]; then
  31. # Legacy multiphp
  32. sed -i "/^WEB_BACKEND=/d" $HESTIA/conf/hestia.conf
  33. echo "WEB_BACKEND='php-fpm'" >> $HESTIA/conf/hestia.conf
  34. for php_ver in $(ls /etc/php/); do
  35. [ ! -d "/etc/php/$php_ver/fpm/pool.d/" ] && continue
  36. cp -f "$HESTIA_INSTALL_DIR/php-fpm/multiphp.tpl" ${WEBTPL}/php-fpm/PHP-${php_ver/\./_}.tpl
  37. done
  38. if [ ! -z "$WEB_SYSTEM" ]; then
  39. cp -rf "${HESTIA_INSTALL_DIR}/templates/web/$WEB_SYSTEM" "${WEBTPL}/"
  40. fi
  41. # Migrate domains
  42. for user in $($BIN/v-list-sys-users plain); do
  43. # Define user data and get suspended status
  44. USER_DATA=$HESTIA/data/users/$user
  45. SUSPENDED=$(get_user_value '$SUSPENDED')
  46. # Check if user is suspended
  47. if [ "$SUSPENDED" = "yes" ]; then
  48. suspended="yes"
  49. $BIN/v-unsuspend-user $user
  50. fi
  51. echo "Migrating legacy multiphp domains for user: $user"
  52. for domain in $($BIN/v-list-web-domains $user plain |cut -f1); do
  53. SUSPENDED_WEB=$(get_object_value 'web' 'DOMAIN' "$domain" '$SUSPENDED')
  54. # Check if web domain is suspended
  55. if [ "$SUSPENDED_WEB" = "yes" ]; then
  56. suspended_web="yes"
  57. $BIN/v-unsuspend-web-domain $user $domain
  58. fi
  59. echo "Processing domain: $domain"
  60. web_tpl="default"
  61. backend_tpl="$DEFAULT_BTPL"
  62. domain_tpl=$($BIN/v-list-web-domain $user $domain |grep "^TEMPLATE:" |awk '{print $2;}' );
  63. if [ "$domain_tpl" = "PHP-56" ]; then
  64. backend_tpl="PHP-5_6"
  65. elif [ "$domain_tpl" = "PHP-70" ]; then
  66. backend_tpl="PHP-7_0"
  67. elif [ "$domain_tpl" = "PHP-71" ]; then
  68. backend_tpl="PHP-7_1"
  69. elif [ "$domain_tpl" = "PHP-72" ]; then
  70. backend_tpl="PHP-7_2"
  71. elif [ "$domain_tpl" = "PHP-73" ] || [ "$domain_tpl" = "default" ] || [ -z "$domain_tpl" ]; then
  72. backend_tpl="PHP-7_3"
  73. elif [ "$domain_tpl" = "PHP-74" ]; then
  74. backend_tpl="PHP-7_4"
  75. else
  76. # Custom domain template used
  77. echo "Domain is using a custom multiphp template (or non-multiphp one)"
  78. web_tpl="$domain_tpl"
  79. if [ -f "${WEBTPL}/$WEB_SYSTEM/php-fpm/$web_tpl.tpl" ]; then
  80. # php-fpm backend folder allready has a template with the same name
  81. web_tpl="custom-$domain_tpl"
  82. fi
  83. # Copy custom template to php-fpm backend folder
  84. mkdir -p "$WEBTPL/$WEB_SYSTEM/php-fpm"
  85. if [ -f "$WEBTPL/$WEB_SYSTEM/$domain_tpl.sh" ]; then
  86. cp "$WEBTPL/$WEB_SYSTEM/$domain_tpl.sh" "$WEBTPL/$WEB_SYSTEM/php-fpm/$web_tpl.sh"
  87. fi
  88. cp "$WEBTPL/$WEB_SYSTEM/$domain_tpl.tpl" "$WEBTPL/$WEB_SYSTEM/php-fpm/$web_tpl.tpl"
  89. cp "$WEBTPL/$WEB_SYSTEM/$domain_tpl.stpl" "$WEBTPL/$WEB_SYSTEM/php-fpm/$web_tpl.stpl"
  90. if [[ $(grep "unix:/" $WEBTPL/$WEB_SYSTEM/$domain_tpl.tpl |egrep -v "^\s*#" |tail -n1) \
  91. =~ unix:\/run\/php\/php([0-9]+\.[0-9]+)-fpm.+\.sock ]]; then
  92. # Found a custom template that is based on official multiphp one
  93. backend_tpl="PHP-${BASH_REMATCH[1]/\./_}"
  94. echo "Custom multiphp template ($domain_tpl) compatible with backend: $backend_tpl"
  95. # Remove multiphp switching script
  96. rm -f "$WEBTPL/$WEB_SYSTEM/php-fpm/$web_tpl.sh"
  97. # Replace hardcoded php-fpm socket path with tpl variable, ignoring commented lines
  98. sed '/^[[:space:]]*#/!s/unix:.*;/%backend_lsnr%;/g' "$WEBTPL/$WEB_SYSTEM/php-fpm/$web_tpl.tpl"
  99. sed '/^[[:space:]]*#/!s/unix:.*;/%backend_lsnr%;/g' "$WEBTPL/$WEB_SYSTEM/php-fpm/$web_tpl.stpl"
  100. fi
  101. fi
  102. echo "Parsed config: oldTPL=$domain_tpl newTPL:$web_tpl newBackTPL:$backend_tpl"
  103. $BIN/v-change-web-domain-tpl "$user" "$domain" "$web_tpl" "no"
  104. $BIN/v-change-web-domain-backend-tpl "$user" "$domain" "$backend_tpl" "no"
  105. echo -e "--done--\n"
  106. # Suspend domain again, if it was suspended
  107. if [ "$suspended_web" = "yes" ]; then
  108. unset suspended_web
  109. $BIN/v-suspend-web-domain $user $domain
  110. fi
  111. done
  112. # Suspend user again, if he was suspended
  113. if [ "$suspended" = "yes" ]; then
  114. unset suspended
  115. $BIN/v-suspend-user $user
  116. fi
  117. done
  118. # cleanup legacy multiphp templates
  119. for php_ver in $(ls /etc/php/); do
  120. [ ! -d "/etc/php/$php_ver/fpm/pool.d/" ] && continue
  121. echo "Remove legacy multiphp templates for: $php_ver"
  122. [ -f "$WEBTPL/$WEB_SYSTEM/PHP-${php_ver//.}.sh" ] && rm "$WEBTPL/$WEB_SYSTEM/PHP-${php_ver//.}.sh"
  123. [ -f "$WEBTPL/$WEB_SYSTEM/PHP-${php_ver//.}.tpl" ] && rm "$WEBTPL/$WEB_SYSTEM/PHP-${php_ver//.}.tpl"
  124. [ -f "$WEBTPL/$WEB_SYSTEM/PHP-${php_ver//.}.stpl" ] && rm "$WEBTPL/$WEB_SYSTEM/PHP-${php_ver//.}.stpl"
  125. done
  126. # Remove default symlinks
  127. [ -f "$WEBTPL/$WEB_SYSTEM/default.sh" ] && rm "$WEBTPL/$WEB_SYSTEM/default.sh"
  128. [ -f "$WEBTPL/$WEB_SYSTEM/default.tpl" ] && rm "$WEBTPL/$WEB_SYSTEM/default.tpl"
  129. [ -f "$WEBTPL/$WEB_SYSTEM/default.stpl" ] && rm "$WEBTPL/$WEB_SYSTEM/default.stpl"
  130. $BIN/v-update-web-templates 'no'
  131. fi