migrate_multiphp.sh 6.1 KB

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