_ynh_add_fpm_config.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #!/bin/bash
  2. # To be removed after the 4.3
  3. ynh_add_fpm_config () {
  4. # Declare an array to define the options of this helper.
  5. local legacy_args=vtufpd
  6. local -A args_array=( [v]=phpversion= [t]=use_template [u]=usage= [f]=footprint= [p]=package= [d]=dedicated_service )
  7. local phpversion
  8. local use_template
  9. local usage
  10. local footprint
  11. local package
  12. local dedicated_service
  13. # Manage arguments with getopts
  14. ynh_handle_getopts_args "$@"
  15. package=${package:-}
  16. # The default behaviour is to use the template.
  17. use_template="${use_template:-1}"
  18. usage="${usage:-}"
  19. footprint="${footprint:-}"
  20. if [ -n "$usage" ] || [ -n "$footprint" ]; then
  21. use_template=0
  22. fi
  23. # Do not use a dedicated service by default
  24. dedicated_service=${dedicated_service:-0}
  25. # Set the default PHP-FPM version by default
  26. phpversion="${phpversion:-$YNH_PHP_VERSION}"
  27. local old_phpversion=$(ynh_app_setting_get --app=$app --key=phpversion)
  28. # If the PHP version changed, remove the old fpm conf
  29. if [ -n "$old_phpversion" ] && [ "$old_phpversion" != "$phpversion" ]
  30. then
  31. local old_php_fpm_config_dir=$(ynh_app_setting_get --app=$app --key=fpm_config_dir)
  32. local old_php_finalphpconf="$old_php_fpm_config_dir/pool.d/$app.conf"
  33. ynh_backup_if_checksum_is_different --file="$old_php_finalphpconf"
  34. ynh_remove_fpm_config
  35. fi
  36. # If the requested PHP version is not the default version for YunoHost
  37. if [ "$phpversion" != "$YNH_DEFAULT_PHP_VERSION" ]
  38. then
  39. # If the argument --package is used, add the packages to ynh_install_php to install them from sury
  40. if [ -n "$package" ]
  41. then
  42. local additionnal_packages="--package=$package"
  43. else
  44. local additionnal_packages=""
  45. fi
  46. # Install this specific version of PHP.
  47. ynh_install_php --phpversion="$phpversion" "$additionnal_packages"
  48. elif [ -n "$package" ]
  49. then
  50. # Install the additionnal packages from the default repository
  51. ynh_add_app_dependencies --package="$package"
  52. fi
  53. if [ $dedicated_service -eq 1 ]
  54. then
  55. local fpm_service="${app}-phpfpm"
  56. local fpm_config_dir="/etc/php/$phpversion/dedicated-fpm"
  57. else
  58. local fpm_service="php${phpversion}-fpm"
  59. local fpm_config_dir="/etc/php/$phpversion/fpm"
  60. fi
  61. # Create the directory for FPM pools
  62. mkdir --parents "$fpm_config_dir/pool.d"
  63. ynh_app_setting_set --app=$app --key=fpm_config_dir --value="$fpm_config_dir"
  64. ynh_app_setting_set --app=$app --key=fpm_service --value="$fpm_service"
  65. ynh_app_setting_set --app=$app --key=fpm_dedicated_service --value="$dedicated_service"
  66. ynh_app_setting_set --app=$app --key=phpversion --value=$phpversion
  67. # Migrate from mutual PHP service to dedicated one.
  68. if [ $dedicated_service -eq 1 ]
  69. then
  70. local old_fpm_config_dir="/etc/php/$phpversion/fpm"
  71. # If a config file exist in the common pool, move it.
  72. if [ -e "$old_fpm_config_dir/pool.d/$app.conf" ]
  73. then
  74. ynh_print_info --message="Migrate to a dedicated php-fpm service for $app."
  75. # Create a backup of the old file before migration
  76. ynh_backup_if_checksum_is_different --file="$old_fpm_config_dir/pool.d/$app.conf"
  77. # Remove the old PHP config file
  78. ynh_secure_remove --file="$old_fpm_config_dir/pool.d/$app.conf"
  79. # Reload PHP to release the socket and allow the dedicated service to use it
  80. ynh_systemd_action --service_name=php${phpversion}-fpm --action=reload
  81. fi
  82. fi
  83. if [ $use_template -eq 1 ]
  84. then
  85. # Usage 1, use the template in conf/php-fpm.conf
  86. local phpfpm_path="$YNH_APP_BASEDIR/conf/php-fpm.conf"
  87. # Make sure now that the template indeed exists
  88. [ -e "$phpfpm_path" ] || ynh_die --message="Unable to find template to configure PHP-FPM."
  89. else
  90. # Usage 2, generate a PHP-FPM config file with ynh_get_scalable_phpfpm
  91. # Store settings
  92. ynh_app_setting_set --app=$app --key=fpm_footprint --value=$footprint
  93. ynh_app_setting_set --app=$app --key=fpm_usage --value=$usage
  94. # Define the values to use for the configuration of PHP.
  95. ynh_get_scalable_phpfpm --usage=$usage --footprint=$footprint
  96. local phpfpm_path="$YNH_APP_BASEDIR/conf/php-fpm.conf"
  97. echo "
  98. [__APP__]
  99. user = __APP__
  100. group = __APP__
  101. chdir = __FINALPATH__
  102. listen = /var/run/php/php__PHPVERSION__-fpm-__APP__.sock
  103. listen.owner = www-data
  104. listen.group = www-data
  105. pm = __PHP_PM__
  106. pm.max_children = __PHP_MAX_CHILDREN__
  107. pm.max_requests = 500
  108. request_terminate_timeout = 1d
  109. " > $phpfpm_path
  110. if [ "$php_pm" = "dynamic" ]
  111. then
  112. echo "
  113. pm.start_servers = __PHP_START_SERVERS__
  114. pm.min_spare_servers = __PHP_MIN_SPARE_SERVERS__
  115. pm.max_spare_servers = __PHP_MAX_SPARE_SERVERS__
  116. " >> $phpfpm_path
  117. elif [ "$php_pm" = "ondemand" ]
  118. then
  119. echo "
  120. pm.process_idle_timeout = 10s
  121. " >> $phpfpm_path
  122. fi
  123. # Concatene the extra config.
  124. if [ -e $YNH_APP_BASEDIR/conf/extra_php-fpm.conf ]; then
  125. cat $YNH_APP_BASEDIR/conf/extra_php-fpm.conf >> "$phpfpm_path"
  126. fi
  127. fi
  128. local finalphpconf="$fpm_config_dir/pool.d/$app.conf"
  129. ynh_add_config --template="$phpfpm_path" --destination="$finalphpconf"
  130. if [ -e "$YNH_APP_BASEDIR/conf/php-fpm.ini" ]
  131. then
  132. ynh_print_warn --message="Packagers ! Please do not use a separate php ini file, merge your directives in the pool file instead."
  133. ynh_add_config --template="$YNH_APP_BASEDIR/conf/php-fpm.ini" --destination="$fpm_config_dir/conf.d/20-$app.ini"
  134. fi
  135. if [ $dedicated_service -eq 1 ]
  136. then
  137. # Create a dedicated php-fpm.conf for the service
  138. local globalphpconf=$fpm_config_dir/php-fpm-$app.conf
  139. echo "[global]
  140. pid = /run/php/php__PHPVERSION__-fpm-__APP__.pid
  141. error_log = /var/log/php/fpm-php.__APP__.log
  142. syslog.ident = php-fpm-__APP__
  143. include = __FINALPHPCONF__
  144. " > $YNH_APP_BASEDIR/conf/php-fpm-$app.conf
  145. ynh_add_config --template="$YNH_APP_BASEDIR/conf/php-fpm-$app.conf" --destination="$globalphpconf"
  146. # Create a config for a dedicated PHP-FPM service for the app
  147. echo "[Unit]
  148. Description=PHP __PHPVERSION__ FastCGI Process Manager for __APP__
  149. After=network.target
  150. [Service]
  151. Type=notify
  152. PIDFile=/run/php/php__PHPVERSION__-fpm-__APP__.pid
  153. ExecStart=/usr/sbin/php-fpm__PHPVERSION__ --nodaemonize --fpm-config __GLOBALPHPCONF__
  154. ExecReload=/bin/kill -USR2 \$MAINPID
  155. [Install]
  156. WantedBy=multi-user.target
  157. " > $YNH_APP_BASEDIR/conf/$fpm_service
  158. # Create this dedicated PHP-FPM service
  159. ynh_add_systemd_config --service=$fpm_service --template=$fpm_service
  160. # Integrate the service in YunoHost admin panel
  161. yunohost service add $fpm_service --log /var/log/php/fpm-php.$app.log --description "Php-fpm dedicated to $app"
  162. # Configure log rotate
  163. ynh_use_logrotate --logfile=/var/log/php
  164. # Restart the service, as this service is either stopped or only for this app
  165. ynh_systemd_action --service_name=$fpm_service --action=restart
  166. else
  167. # Validate that the new php conf doesn't break php-fpm entirely
  168. if ! php-fpm${phpversion} --test 2>/dev/null
  169. then
  170. php-fpm${phpversion} --test || true
  171. ynh_secure_remove --file="$finalphpconf"
  172. ynh_die --message="The new configuration broke php-fpm?"
  173. fi
  174. ynh_systemd_action --service_name=$fpm_service --action=reload
  175. fi
  176. }