From 5804f68824a7609009ff9c673c5d3c8c94b0a460 Mon Sep 17 00:00:00 2001 From: szaimen Date: Wed, 17 Aug 2022 19:10:54 +0200 Subject: [PATCH] set max connections and max php children based on available hardware Signed-off-by: szaimen --- Containers/nextcloud/Dockerfile | 10 ++++++---- Containers/nextcloud/entrypoint.sh | 7 ++++++- Containers/postgresql/Dockerfile | 2 +- Containers/postgresql/start.sh | 5 +++++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/Containers/nextcloud/Dockerfile b/Containers/nextcloud/Dockerfile index 0776b2e6..39193bab 100644 --- a/Containers/nextcloud/Dockerfile +++ b/Containers/nextcloud/Dockerfile @@ -198,15 +198,17 @@ RUN set -ex; \ git \ postgresql-client \ tzdata \ + mawk \ ; \ rm -rf /var/lib/apt/lists/* RUN set -ex; \ grep -q '^pm = dynamic' /usr/local/etc/php-fpm.d/www.conf; \ - sed -i 's/^pm.max_children =.*/pm.max_children = 100/' /usr/local/etc/php-fpm.d/www.conf; \ - sed -i 's/^pm.start_servers =.*/pm.start_servers = 25/' /usr/local/etc/php-fpm.d/www.conf; \ - sed -i 's/^pm.min_spare_servers =.*/pm.min_spare_servers = 25/' /usr/local/etc/php-fpm.d/www.conf; \ - sed -i 's/^pm.max_spare_servers =.*/pm.max_spare_servers = 75/' /usr/local/etc/php-fpm.d/www.conf + sed -i 's/^pm = dynamic/pm = ondemand/' /usr/local/etc/php-fpm.d/www.conf; \ + sed -i 's/^pm.max_children =.*/pm.max_children = ${PHP_MAX_CHILDREN}/' /usr/local/etc/php-fpm.d/www.conf; \ + sed -i 's/^pm.start_servers =.*/pm.start_servers = 2/' /usr/local/etc/php-fpm.d/www.conf; \ + sed -i 's/^pm.min_spare_servers =.*/pm.min_spare_servers = 1/' /usr/local/etc/php-fpm.d/www.conf; \ + sed -i 's/^pm.max_spare_servers =.*/pm.max_spare_servers = 3/' /usr/local/etc/php-fpm.d/www.conf RUN set -ex; \ rm -rf /tmp/nextcloud-aio && \ diff --git a/Containers/nextcloud/entrypoint.sh b/Containers/nextcloud/entrypoint.sh index c1c4b7e4..d8b7b074 100644 --- a/Containers/nextcloud/entrypoint.sh +++ b/Containers/nextcloud/entrypoint.sh @@ -10,7 +10,7 @@ directory_empty() { [ -z "$(ls -A "$1/")" ] } -echo "Configuring Redis as session handler" +echo "Configuring Redis as session handler..." cat << REDIS_CONF > /usr/local/etc/php/conf.d/redis-session.ini session.save_handler = redis session.save_path = "tcp://${REDIS_HOST}:${REDIS_HOST_PORT:=6379}?auth=${REDIS_HOST_PASSWORD}" @@ -21,6 +21,11 @@ redis.session.lock_retries = -1 redis.session.lock_wait_time = 10000 REDIS_CONF +echo "Setting php max children..." +MEMORY=$(mawk '/MemTotal/ {printf "%d", $2/1024}' /proc/meminfo) +PHP_MAX_CHILDREN=$((MEMORY/50)) +export PHP_MAX_CHILDREN + # Check permissions in ncdata touch "/mnt/ncdata/this-is-a-test-file" if ! [ -f "/mnt/ncdata/this-is-a-test-file" ]; then diff --git a/Containers/postgresql/Dockerfile b/Containers/postgresql/Dockerfile index 346130ca..c6cefa1d 100644 --- a/Containers/postgresql/Dockerfile +++ b/Containers/postgresql/Dockerfile @@ -1,7 +1,7 @@ # From https://github.com/docker-library/postgres/blob/master/13/alpine/Dockerfile FROM postgres:14.5-alpine -RUN apk add --update --no-cache bash openssl shadow netcat-openbsd grep +RUN apk add --update --no-cache bash openssl shadow netcat-openbsd grep mawk # We need to use the same gid and uid as on old installations RUN set -ex; \ diff --git a/Containers/postgresql/start.sh b/Containers/postgresql/start.sh index 6810887e..6d140fdb 100644 --- a/Containers/postgresql/start.sh +++ b/Containers/postgresql/start.sh @@ -110,6 +110,11 @@ if ! [ -f "$DATADIR/PG_VERSION" ] && ! [ -f "$DUMP_FILE" ]; then rm -rf "${DATADIR:?}/"* fi +echo "Setting max connections..." +MEMORY=$(mawk '/MemTotal/ {printf "%d", $2/1024}' /proc/meminfo) +MAX_CONNECTIONS=$((MEMORY/50+3)) +sed -i "s|^max_connections =.*|max_connections = $MAX_CONNECTIONS|" "/var/lib/postgresql/data/postgresql.conf" + # Catch docker stop attempts trap 'true' SIGINT SIGTERM