v-add-web-php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #!/bin/bash
  2. # info: add php fpm version
  3. # options: VERSION
  4. #
  5. # example: v-add-web-php 8.0
  6. #
  7. # Install php-fpm for provided version.
  8. #----------------------------------------------------------#
  9. # Variables & Functions #
  10. #----------------------------------------------------------#
  11. # Argument definition
  12. version=$1
  13. # Includes
  14. # shellcheck source=/etc/hestiacp/hestia.conf
  15. source /etc/hestiacp/hestia.conf
  16. # shellcheck source=/usr/local/hestia/func/main.sh
  17. source $HESTIA/func/main.sh
  18. # load config file
  19. source_conf "$HESTIA/conf/hestia.conf"
  20. source_conf "$HESTIA/install/upgrade/upgrade.conf"
  21. #----------------------------------------------------------#
  22. # Verifications #
  23. #----------------------------------------------------------#
  24. check_args '1' "$#" 'VERSION'
  25. if [ -z "$WEB_BACKEND" ]; then
  26. echo "Multiple php versions are not supported for modphp"
  27. fi
  28. # Set file locations
  29. php_fpm="/etc/init.d/php$version-fpm"
  30. # Verify php version format
  31. if [[ ! $version =~ ^[0-9]\.[0-9]+ ]]; then
  32. echo "The specified PHP version format is invalid, it should look like [0-9].[0-9]."
  33. echo "Example: 7.0, 7.4, 8.0"
  34. exit "$E_INVALID"
  35. fi
  36. # Check if php version already exists
  37. if [ -f "$php_fpm" ] && [ -f "$HESTIA/data/templates/web/php-fpm/PHP-${version/\./_}.tpl" ]; then
  38. echo "ERROR: Specified PHP version is already installed."
  39. exit "$E_INVALID"
  40. fi
  41. # Check if php version is supported
  42. if [[ ! "$multiphp_v" =~ $version ]]; then
  43. echo "ERROR: Specified PHP version is not supported or does not exist."
  44. exit "$E_INVALID"
  45. fi
  46. # Perform verification if read-only mode is enabled
  47. check_hestia_demo_mode
  48. #----------------------------------------------------------#
  49. # Action #
  50. #----------------------------------------------------------#
  51. mph="php$version-common php$version-mbstring php$version-bcmath php$version-cli php$version-curl
  52. php$version-fpm php$version-gd php$version-intl php$version-mysql
  53. php$version-soap php$version-xml php$version-zip php$version-json php$version-bz2
  54. php$version-pspell php$version-imagick php$version-pgsql php$version-imap php$version-ldap"
  55. # Check is version is 7.1 or below to add mcrypt
  56. if [[ $(echo "$version 7.2" | awk '{print ($1 < $2)}') == 1 ]]; then
  57. mph="$mph php$version-mcrypt"
  58. fi
  59. # Check if version is 8.0 or higher and drop php json.
  60. if [[ ${version:0:1} == "8" ]]; then
  61. mph=$(echo "$mph" | sed -e "s/php$version-json//")
  62. fi
  63. if ! echo "$DB_SYSTEM" | grep -w 'mysql' > /dev/null; then
  64. mph=$(echo "$mph" | sed -e "s/php$version-mysql//")
  65. fi
  66. if ! echo "$DB_SYSTEM" | grep -w 'pgsql' > /dev/null; then
  67. mph=$(echo "$mph" | sed -e "s/php$version-pgsql//")
  68. fi
  69. # Install php packages
  70. if [ -f '/etc/redhat-release' ]; then
  71. dnf makecache -q
  72. dnf install -q -y $mph > /dev/null 2>&1 &
  73. else
  74. apt-get -qq update
  75. apt-get -y -qq -o Dpkg::Options::="--force-confold" install $mph > /dev/null 2>&1 &
  76. fi
  77. BACK_PID=$!
  78. # Check if package installation is done, print a spinner
  79. echo "Installing PHP-$version, please wait..."
  80. spinner="/-\|"
  81. spin_i=1
  82. while kill -0 $BACK_PID > /dev/null 2>&1; do
  83. printf "\b${spinner:spin_i++%${#spinner}:1}"
  84. sleep 0.5
  85. done
  86. # Do a blank echo to get the \n back
  87. echo
  88. # Check if installation was successful
  89. if [ ! -f "$php_fpm" ]; then
  90. echo "ERROR: Installation failed, please run the following command manually for debugging:"
  91. if [ -f '/etc/redhat-release' ]; then
  92. echo "dnf install $mph"
  93. else
  94. echo "apt-get install $mph"
  95. fi
  96. fi
  97. # Check if required modules for apache2 are enabled
  98. if [ "$WEB_SYSTEM" = "apache2" ]; then
  99. if ! a2query -q -m proxy_fcgi; then
  100. a2enmod -q proxy_fcgi
  101. fi
  102. if ! a2query -q -m setenvif; then
  103. a2enmod -q setenvif
  104. fi
  105. $BIN/v-restart-web "yes"
  106. fi
  107. # Configure fpm
  108. update-rc.d php$version-fpm defaults > /dev/null 2>&1
  109. v_tpl=${version//./}
  110. rm -f /etc/php/$version/fpm/pool.d/*
  111. cp -f $HESTIA_INSTALL_DIR/php-fpm/dummy.conf /etc/php/$version/fpm/pool.d/
  112. sed -i "s/%backend_version%/$version/g" /etc/php/$version/fpm/pool.d/dummy.conf
  113. cp -f $HESTIA_INSTALL_DIR/php-fpm/php-fpm.conf /etc/php/$version/fpm/
  114. sed -i "s/fpm_v/$version/g" /etc/php/$version/fpm/php-fpm.conf
  115. # Increase max upload and max post size
  116. sed -i "s/upload_max_filesize = 2M/upload_max_filesize = 100M/g" /etc/php/$version/fpm/php.ini
  117. sed -i "s/post_max_size = 8M/post_max_size = 100M/g" /etc/php/$version/fpm/php.ini
  118. sed -i "s/max_execution_time = 30/max_execution_time = 60/g" /etc/php/$version/fpm/php.ini
  119. sed -i "s/;max_input_vars = 1000/max_input_vars = 4000/g" /etc/php/$version/fpm/php.ini
  120. # Disable exec and other harmfull php functions
  121. sed -i "s/disable_functions =.*/disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,exec,system,passthru,shell_exec,proc_open,popen/g" /etc/php/$version/fpm/php.ini
  122. sed -i "s/disable_functions =.*/disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority/g" /etc/php/$version/cli/php.ini
  123. # Other use full changes
  124. sed -i "s/;opcache.enable=1/opcache.enable=1/g" /etc/php/$version/fpm/php.ini
  125. sed -i "s/;opcache.memory_consumption=128/opcache.memory_consumption=512/g" /etc/php/$version/fpm/php.ini
  126. sed -i "s/;opcache.max_accelerated_files=10000/opcache.max_accelerated_files=100000/g" /etc/php/$version/fpm/php.ini
  127. # Install backend template
  128. cp -f $HESTIA_INSTALL_DIR/php-fpm/multiphp.tpl \
  129. $HESTIA/data/templates/web/php-fpm/PHP-${version/\./_}.tpl
  130. #----------------------------------------------------------#
  131. # Hestia #
  132. #----------------------------------------------------------#
  133. # Logging
  134. $BIN/v-log-action "system" "Info" "System" "Installed PHP $version."
  135. log_event "$OK" "$ARGUMENTS"
  136. exit