From 9381b463d094501a32a29127096c2fffdc0d65ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guilherme=20Capil=C3=A9?= Date: Fri, 6 Dec 2024 11:30:32 -0300 Subject: [PATCH] FIX: Adjust php-fpm pool: enable the increase of maximum number of processes #190 (#191) --- core/files/entrypoint.sh | 5 ++++ core/files/entrypoint_fpm.sh | 29 +++++++++++++++++++ core/files/entrypoint_nginx.sh | 9 ++++++ .../etc/nginx/sites-available/php-fpm-status | 8 +++++ template.env | 16 +++++++++- 5 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 core/files/etc/nginx/sites-available/php-fpm-status diff --git a/core/files/entrypoint.sh b/core/files/entrypoint.sh index 015232f..07a65b5 100755 --- a/core/files/entrypoint.sh +++ b/core/files/entrypoint.sh @@ -40,6 +40,11 @@ export FASTCGI_READ_TIMEOUT=${FASTCGI_READ_TIMEOUT:-300s} export FASTCGI_SEND_TIMEOUT=${FASTCGI_SEND_TIMEOUT:-300s} export FASTCGI_CONNECT_TIMEOUT=${FASTCGI_CONNECT_TIMEOUT:-300s} +export PHP_FCGI_CHILDREN=${PHP_FCGI_CHILDREN:-5} +export PHP_FCGI_START_SERVERS=${PHP_FCGI_START_SERVERS:-2} +export PHP_FCGI_SPARE_SERVERS=${PHP_FCGI_SPARE_SERVERS:-1} +export PHP_FCGI_MAX_REQUESTS=${PHP_FCGI_MAX_REQUESTS:-0} + export PHP_MEMORY_LIMIT=${PHP_MEMORY_LIMIT:-2048M} export PHP_MAX_EXECUTION_TIME=${PHP_MAX_EXECUTION_TIME:-300} export PHP_UPLOAD_MAX_FILESIZE=${PHP_UPLOAD_MAX_FILESIZE:-50M} diff --git a/core/files/entrypoint_fpm.sh b/core/files/entrypoint_fpm.sh index 1b78d2d..5afe22f 100755 --- a/core/files/entrypoint_fpm.sh +++ b/core/files/entrypoint_fpm.sh @@ -29,6 +29,35 @@ change_php_vars() { sed -i "s/session.sid_length = .*/session.sid_length = 64/" "$FILE" sed -i "s/session.use_strict_mode = .*/session.use_strict_mode = 1/" "$FILE" done + + for FILE in /etc/php/*/fpm/pool.d/www.conf + do + [[ -e $FILE ]] || break + echo "Configure PHP | Setting 'pm.max_children = ${PHP_FCGI_CHILDREN}'" + sed -i -E "s/;?pm.max_children = .*/pm.max_children = ${PHP_FCGI_CHILDREN}/" "$FILE" + echo "Configure PHP | Setting 'pm.start_servers = ${PHP_FCGI_START_SERVERS}'" + sed -i -E "s/;?pm.start_servers = .*/pm.start_servers = ${PHP_FCGI_START_SERVERS}/" "$FILE" + echo "Configure PHP | Setting 'pm.(min|max)_spare_servers = ${PHP_FCGI_START_SERVERS}'" + sed -i -E "s/;?pm.min_spare_servers = .*/pm.min_spare_servers = ${PHP_FCGI_SPARE_SERVERS}/" "$FILE" + if [[ "$PHP_FCGI_START_SERVERS" -gt "$PHP_FCGI_SPARE_SERVERS" ]]; then + sed -i -E "s/;?pm.max_spare_servers = .*/pm.max_spare_servers = ${PHP_FCGI_START_SERVERS}/" "$FILE" + else + sed -i -E "s/;?pm.max_spare_servers = .*/pm.max_spare_servers = ${PHP_FCGI_SPARE_SERVERS}/" "$FILE" + fi + echo "Configure PHP | Setting 'pm.max_requests = ${PHP_FCGI_MAX_REQUESTS}'" + sed -i -E "s/;?pm.max_requests = .*/pm.max_requests = ${PHP_FCGI_MAX_REQUESTS}/" "$FILE" + if [[ "$FASTCGI_STATUS_LISTEN" != "" ]]; then + echo "Configure PHP | Setting 'pm.status_path = /status'" + sed -i -E "s/;?pm.status_path = .*/pm.status_path = \/status/" "$FILE" + echo "Configure PHP | Setting 'pm.status_path = /run/php/php-fpm-status.sock'" + sed -i -E "s/;?pm.status_listen = .*/pm.status_listen = \/run\/php\/php-fpm-status.sock/" "$FILE" + else + echo "Configure PHP | Disabling 'pm.status_path'" + sed -i -E "s/^pm.status_path = /;pm.status_path = /" "$FILE" + echo "Configure PHP | Disabling 'pm.status_listen'" + sed -i -E "s/^pm.status_listen =/;pm.status_listen =/" "$FILE" + fi + done } echo "Configure PHP | Change PHP values ..." && change_php_vars diff --git a/core/files/entrypoint_nginx.sh b/core/files/entrypoint_nginx.sh index 6c4f5af..24be374 100755 --- a/core/files/entrypoint_nginx.sh +++ b/core/files/entrypoint_nginx.sh @@ -340,6 +340,15 @@ init_nginx() { echo "... DH parameters found" fi + if [[ "$FASTCGI_STATUS_LISTEN" != "" ]]; then + echo "... enabling php-fpm status page" + ln -s /etc/nginx/sites-available/php-fpm-status /etc/nginx/sites-enabled/php-fpm-status + sed -i -E "s/ listen [^;]+/ listen $FASTCGI_STATUS_LISTEN" /etc/nginx/sites-enabled/php-fpm-status + elif [[ -f /etc/nginx/sites-enabled/php-fpm-status ]]; then + echo "... disabling php-fpm status page" + rm /etc/nginx/sites-enabled/php-fpm-status + fi + flip_nginx false false } diff --git a/core/files/etc/nginx/sites-available/php-fpm-status b/core/files/etc/nginx/sites-available/php-fpm-status new file mode 100644 index 0000000..a45a56f --- /dev/null +++ b/core/files/etc/nginx/sites-available/php-fpm-status @@ -0,0 +1,8 @@ +server { + listen 8999; + location ~ ^/status$ { + fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; + include fastcgi_params; + fastcgi_pass unix:/run/php/php-fpm-status.sock; + } +} \ No newline at end of file diff --git a/template.env b/template.env index c036872..3e4d5ea 100644 --- a/template.env +++ b/template.env @@ -179,10 +179,13 @@ SYNCSERVERS_1_PULL_RULES= # 2 - Debug on + SQL dump # DEBUG= -# FastCGI configuration +# FastCGI configuration on nginx # FASTCGI_READ_TIMEOUT=300s # FASTCGI_SEND_TIMEOUT=300s # FASTCGI_CONNECT_TIMEOUT=300s +# Whete to listen to PHP-FPM status. Can be a port or a ip:port. If not set the status page will not be shown. +# Do not expose this page in public networks. +# FASTCGI_STATUS_LISTEN="" # PHP FPM configuration @@ -198,6 +201,17 @@ SYNCSERVERS_1_PULL_RULES= # Maximum time PHP spends parsing input data in seconds. # PHP_MAX_INPUT_TIME=300 +## PHP FPM pool setup +# Maximum number of php-fpm processes, limits the number of simultaneous requests. +PHP_FCGI_CHILDREN="5" +# Number of processes created on startup. +PHP_FCGI_START_SERVERS="2" +# The desired number of idle server processes. +PHP_FCGI_SPARE_SERVERS="1" +# The number of requests each process should execute before respawning. "0" means endless request processing. +PHP_FCGI_MAX_REQUESTS="0" + + ## Additional PHP settings # Timeout (in minutes) for user session inactivity before it expires. # PHP_SESSION_TIMEOUT=60