migrate_multiphp.sh 6.6 KB

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