diff --git a/Dockerfile-alpine.template b/Dockerfile-alpine.template index 5fe764bb3..cb647cb84 100644 --- a/Dockerfile-alpine.template +++ b/Dockerfile-alpine.template @@ -113,6 +113,13 @@ RUN { \ echo 'memory_limit=${PHP_MEMORY_LIMIT}'; \ echo 'upload_max_filesize=${PHP_UPLOAD_LIMIT}'; \ echo 'post_max_size=${PHP_UPLOAD_LIMIT}'; \ + echo; \ + echo '; Use Redis for PHP sessions when configured by the entrypoint'; \ + echo 'session.save_handler=${PHP_REDIS_SESSION_HANDLER}'; \ + echo 'session.save_path="${PHP_REDIS_SESSION_SAVE_PATH}"'; \ + echo 'redis.session.locking_enabled=${PHP_REDIS_SESSION_LOCKING_ENABLED}'; \ + echo 'redis.session.lock_retries=${PHP_REDIS_SESSION_LOCK_RETRIES}'; \ + echo 'redis.session.lock_wait_time=${PHP_REDIS_SESSION_LOCK_WAIT_TIME}'; \ } > "${PHP_INI_DIR}/conf.d/nextcloud.ini"; \ \ mkdir /var/www/data; \ diff --git a/Dockerfile-debian.template b/Dockerfile-debian.template index 72e4d08ce..b1d44ac10 100644 --- a/Dockerfile-debian.template +++ b/Dockerfile-debian.template @@ -120,6 +120,13 @@ RUN { \ echo 'memory_limit=${PHP_MEMORY_LIMIT}'; \ echo 'upload_max_filesize=${PHP_UPLOAD_LIMIT}'; \ echo 'post_max_size=${PHP_UPLOAD_LIMIT}'; \ + echo; \ + echo '; Use Redis for PHP sessions when configured by the entrypoint'; \ + echo 'session.save_handler=${PHP_REDIS_SESSION_HANDLER}'; \ + echo 'session.save_path="${PHP_REDIS_SESSION_SAVE_PATH}"'; \ + echo 'redis.session.locking_enabled=${PHP_REDIS_SESSION_LOCKING_ENABLED}'; \ + echo 'redis.session.lock_retries=${PHP_REDIS_SESSION_LOCK_RETRIES}'; \ + echo 'redis.session.lock_wait_time=${PHP_REDIS_SESSION_LOCK_WAIT_TIME}'; \ } > "${PHP_INI_DIR}/conf.d/nextcloud.ini"; \ \ mkdir /var/www/data; \ diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index 303aa0052..23a65272b 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -83,45 +83,48 @@ file_env() { unset "$fileVar" } -# Write PHP session config for Redis to /usr/local/etc/php/conf.d/redis-session.ini +# Export PHP session config for Redis via environment variables configure_redis_session() { + local redis_save_path + local redis_auth='' + echo "=> Configuring PHP session handler..." + if [ -z "${REDIS_HOST:-}" ]; then - echo "==> Using default PHP session handler" + echo "==> Using default PHP session handler (files)" + # @todo: consider moving to PHP 8.3 missing env variable fallbacks in the ini file itself + export PHP_REDIS_SESSION_HANDLER='files' + export PHP_REDIS_SESSION_SAVE_PATH='' + export PHP_REDIS_SESSION_LOCKING_ENABLED='0' + export PHP_REDIS_SESSION_LOCK_RETRIES='0' + export PHP_REDIS_SESSION_LOCK_WAIT_TIME='0' return 0 fi + file_env REDIS_HOST_PASSWORD + + case "$REDIS_HOST" in + /*) + redis_save_path="unix://${REDIS_HOST}" + ;; + *) + redis_save_path="tcp://${REDIS_HOST}:${REDIS_HOST_PORT:=6379}" + ;; + esac + + if [ -n "${REDIS_HOST_PASSWORD+x}" ] && [ -n "${REDIS_HOST_USER+x}" ]; then + redis_auth="?auth[]=${REDIS_HOST_USER}&auth[]=${REDIS_HOST_PASSWORD}" + elif [ -n "${REDIS_HOST_PASSWORD+x}" ]; then + redis_auth="?auth=${REDIS_HOST_PASSWORD}" + fi + + export PHP_REDIS_SESSION_HANDLER='redis' + export PHP_REDIS_SESSION_SAVE_PATH="${redis_save_path}${redis_auth}" + export PHP_REDIS_SESSION_LOCKING_ENABLED='1' + export PHP_REDIS_SESSION_LOCK_RETRIES='-1' + export PHP_REDIS_SESSION_LOCK_WAIT_TIME='10000' + echo "==> Using Redis as PHP session handler..." - { - file_env REDIS_HOST_PASSWORD - echo 'session.save_handler = redis' - # check if redis host is a unix socket path - if [ "$(echo "$REDIS_HOST" | cut -c1-1)" = "/" ]; then - if [ -n "${REDIS_HOST_PASSWORD+x}" ]; then - if [ -n "${REDIS_HOST_USER+x}" ]; then - echo "session.save_path = \"unix://${REDIS_HOST}?auth[]=${REDIS_HOST_USER}&auth[]=${REDIS_HOST_PASSWORD}\"" - else - echo "session.save_path = \"unix://${REDIS_HOST}?auth=${REDIS_HOST_PASSWORD}\"" - fi - else - echo "session.save_path = \"unix://${REDIS_HOST}\"" - fi - # check if redis password has been set - elif [ -n "${REDIS_HOST_PASSWORD+x}" ]; then - if [ -n "${REDIS_HOST_USER+x}" ]; then - echo "session.save_path = \"tcp://${REDIS_HOST}:${REDIS_HOST_PORT:=6379}?auth[]=${REDIS_HOST_USER}&auth[]=${REDIS_HOST_PASSWORD}\"" - else - echo "session.save_path = \"tcp://${REDIS_HOST}:${REDIS_HOST_PORT:=6379}?auth=${REDIS_HOST_PASSWORD}\"" - fi - else - echo "session.save_path = \"tcp://${REDIS_HOST}:${REDIS_HOST_PORT:=6379}\"" - fi - echo "redis.session.locking_enabled = 1" - echo "redis.session.lock_retries = -1" - # redis.session.lock_wait_time is specified in microseconds. - # Wait 10ms before retrying the lock rather than the default 2ms. - echo "redis.session.lock_wait_time = 10000" - } > /usr/local/etc/php/conf.d/redis-session.ini } ########################################################################