_ynh_add_fpm_config 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/bin/bash
  2. # Create a dedicated php-fpm config
  3. #
  4. # usage 1: ynh_add_fpm_config [--phpversion=7.X] [--use_template]
  5. # | arg: -v, --phpversion - Version of php to use.
  6. # | arg: -t, --use_template - Use this helper in template mode.
  7. #
  8. # -----------------------------------------------------------------------------
  9. #
  10. # usage 2: ynh_add_fpm_config [--phpversion=7.X] --usage=usage --footprint=footprint
  11. # | arg: -v, --phpversion - Version of php to use.#
  12. # | arg: -f, --footprint - Memory footprint of the service (low/medium/high).
  13. # low - Less than 20Mb of ram by pool.
  14. # medium - Between 20Mb and 40Mb of ram by pool.
  15. # high - More than 40Mb of ram by pool.
  16. # Or specify exactly the footprint, the load of the service as Mb by pool instead of having a standard value.
  17. # To have this value, use the following command and stress the service.
  18. # watch -n0.5 ps -o user,cmd,%cpu,rss -u APP
  19. #
  20. # | arg: -u, --usage - Expected usage of the service (low/medium/high).
  21. # low - Personal usage, behind the sso.
  22. # medium - Low usage, few people or/and publicly accessible.
  23. # high - High usage, frequently visited website.
  24. #
  25. # Requires YunoHost version 2.7.2 or higher.
  26. ynh_add_fpm_config () {
  27. # Declare an array to define the options of this helper.
  28. local legacy_args=vtuf
  29. declare -Ar args_array=( [v]=phpversion= [t]=use_template [u]=usage= [f]=footprint= )
  30. local phpversion
  31. local use_template
  32. local usage
  33. local footprint
  34. # Manage arguments with getopts
  35. ynh_handle_getopts_args "$@"
  36. # The default behaviour is to use the template.
  37. use_template="${use_template:-1}"
  38. usage="${usage:-}"
  39. footprint="${footprint:-}"
  40. if [ -n "$usage" ] || [ -n "$footprint" ]; then
  41. use_template=0
  42. fi
  43. # Configure PHP-FPM 7.0 by default
  44. phpversion="${phpversion:-7.0}"
  45. local fpm_config_dir="/etc/php/$phpversion/fpm"
  46. local fpm_service="php${phpversion}-fpm"
  47. # Configure PHP-FPM 5 on Debian Jessie
  48. if [ "$(ynh_get_debian_release)" == "jessie" ]; then
  49. fpm_config_dir="/etc/php5/fpm"
  50. fpm_service="php5-fpm"
  51. fi
  52. ynh_app_setting_set --app=$app --key=fpm_config_dir --value="$fpm_config_dir"
  53. ynh_app_setting_set --app=$app --key=fpm_service --value="$fpm_service"
  54. finalphpconf="$fpm_config_dir/pool.d/$app.conf"
  55. ynh_backup_if_checksum_is_different --file="$finalphpconf"
  56. if [ $use_template -eq 1 ]
  57. then
  58. # Usage 1, use the template in ../conf/php-fpm.conf
  59. sudo cp ../conf/php-fpm.conf "$finalphpconf"
  60. ynh_replace_string --match_string="__NAMETOCHANGE__" --replace_string="$app" --target_file="$finalphpconf"
  61. ynh_replace_string --match_string="__FINALPATH__" --replace_string="$final_path" --target_file="$finalphpconf"
  62. ynh_replace_string --match_string="__USER__" --replace_string="$app" --target_file="$finalphpconf"
  63. ynh_replace_string --match_string="__PHPVERSION__" --replace_string="$phpversion" --target_file="$finalphpconf"
  64. else
  65. # Store settings
  66. ynh_app_setting_set --app=$app --key=fpm_footprint --value=$footprint
  67. ynh_app_setting_set --app=$app --key=fpm_usage --value=$usage
  68. # Usage 2, generate a php-fpm config file with ynh_get_scalable_phpfpm
  69. ynh_get_scalable_phpfpm --usage=$usage --footprint=$footprint
  70. # Copy the default file
  71. sudo cp "$fpm_config_dir/pool.d/www.conf" "$finalphpconf"
  72. # Replace standard variables into the default file
  73. ynh_replace_string --match_string="^\[www\]" --replace_string="[$app]" --target_file="$finalphpconf"
  74. ynh_replace_string --match_string=".*listen = .*" --replace_string="listen = /var/run/php/php7.0-fpm-$app.sock" --target_file="$finalphpconf"
  75. ynh_replace_string --match_string="^user = .*" --replace_string="user = $app" --target_file="$finalphpconf"
  76. ynh_replace_string --match_string="^group = .*" --replace_string="group = $app" --target_file="$finalphpconf"
  77. ynh_replace_string --match_string=".*chdir = .*" --replace_string="chdir = $final_path" --target_file="$finalphpconf"
  78. # Configure fpm children
  79. ynh_replace_string --match_string=".*pm = .*" --replace_string="pm = $php_pm" --target_file="$finalphpconf"
  80. ynh_replace_string --match_string=".*pm.max_children = .*" --replace_string="pm.max_children = $php_max_children" --target_file="$finalphpconf"
  81. ynh_replace_string --match_string=".*pm.max_requests = .*" --replace_string="pm.max_requests = 500" --target_file="$finalphpconf"
  82. ynh_replace_string --match_string=".*request_terminate_timeout = .*" --replace_string="request_terminate_timeout = 1d" --target_file="$finalphpconf"
  83. if [ "$php_pm" = "dynamic" ]
  84. then
  85. ynh_replace_string --match_string=".*pm.start_servers = .*" --replace_string="pm.start_servers = $php_start_servers" --target_file="$finalphpconf"
  86. ynh_replace_string --match_string=".*pm.min_spare_servers = .*" --replace_string="pm.min_spare_servers = $php_min_spare_servers" --target_file="$finalphpconf"
  87. ynh_replace_string --match_string=".*pm.max_spare_servers = .*" --replace_string="pm.max_spare_servers = $php_max_spare_servers" --target_file="$finalphpconf"
  88. elif [ "$php_pm" = "ondemand" ]
  89. then
  90. ynh_replace_string --match_string=".*pm.process_idle_timeout = .*" --replace_string="pm.process_idle_timeout = 10s" --target_file="$finalphpconf"
  91. fi
  92. # Comment unused parameters
  93. if [ "$php_pm" != "dynamic" ]
  94. then
  95. ynh_replace_string --match_string=".*\(pm.start_servers = .*\)" --replace_string=";\1" --target_file="$finalphpconf"
  96. ynh_replace_string --match_string=".*\(pm.min_spare_servers = .*\)" --replace_string=";\1" --target_file="$finalphpconf"
  97. ynh_replace_string --match_string=".*\(pm.max_spare_servers = .*\)" --replace_string=";\1" --target_file="$finalphpconf"
  98. fi
  99. if [ "$php_pm" != "ondemand" ]
  100. then
  101. ynh_replace_string --match_string=".*\(pm.process_idle_timeout = .*\)" --replace_string=";\1" --target_file="$finalphpconf"
  102. fi
  103. # Concatene the extra config.
  104. if [ -e ../conf/extra_php-fpm.conf ]; then
  105. cat ../conf/extra_php-fpm.conf >> "$finalphpconf"
  106. fi
  107. fi
  108. sudo chown root: "$finalphpconf"
  109. ynh_store_file_checksum --file="$finalphpconf"
  110. if [ -e "../conf/php-fpm.ini" ]
  111. then
  112. echo "Packagers ! Please do not use a separate php ini file, merge your directives in the pool file instead." >&2
  113. finalphpini="$fpm_config_dir/conf.d/20-$app.ini"
  114. ynh_backup_if_checksum_is_different "$finalphpini"
  115. sudo cp ../conf/php-fpm.ini "$finalphpini"
  116. sudo chown root: "$finalphpini"
  117. ynh_store_file_checksum "$finalphpini"
  118. fi
  119. ynh_systemd_action --service_name=$fpm_service --action=reload
  120. }