Skip to content

Commit c2a4f4c

Browse files
committed
Allow setting php.ini and php-fpm pool setting via individial env vars
1 parent f03ca92 commit c2a4f4c

File tree

4 files changed

+86
-1
lines changed

4 files changed

+86
-1
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,24 @@ Application root is `/app`. Application runs as user `application` (uid=1000).
166166
| `PHP_FPM_OVERRIDE` | fpm | | Allow overriding php-fpm pool settings. The multiline content for php-fpm.conf here. Use "\n" for multiline i.e. in ECS |
167167
| `PHP_EXTENSIONS` | fpm, ssh | (all) | Comma separated list of PHP extensions to enable (if this is not set, all are enabled). |
168168
| `PHP_DISABLE_EXTENSIONS` | fpm, ssh | | Comma separated list of PHP extensions to disable (in case you keep all enabled, you can disable individual ones, i.e. igbinary). |
169+
| `PHPINI__xxx__yyy` | fpm, ssh | | Set php.ini setting `xxx.yyy` (always lower cased) |
170+
| `PHPFPM__xxx__yyy` | fpm | | Set php-fpm pool setting `xxx.yyy` (always lower cased) |
171+
172+
The `PHPINI__...` and `PHPFPM__...` allow to set individual settings for `php.ini`
173+
and `php-fpm.conf` (pool settings) using individual environment variables. Just
174+
replace the `.` in the settings by `__`. Examples:
175+
176+
Upper case allowed (will be lower-cased):
177+
```
178+
PHPINI__SESSION__SAVE_HANDLER=redis
179+
PHPINI__REDIS__SESSION__LOCKING_ENABLED=1
180+
```
181+
182+
Or:
183+
```
184+
PHPFPM__request_terminate_timeout=30s
185+
PHPFPM__pm__max_children=15
186+
```
169187

170188
## Example usage
171189

example-app/.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,15 @@ slowlog = /tmp/slow.log
4242
request_slowlog_timeout = 3s
4343
pm.max_children = 5
4444
"
45+
46+
# -----------------------------------------
47+
# PHP settings by env vars
48+
# -----------------------------------------
49+
50+
# Example of setting php.ini settings
51+
PHPINI__SESSION__SAVE_HANDLER=redis
52+
PHPINI__REDIS__SESSION__LOCKING_ENABLED=1
53+
54+
# Example of setting PHP-FPM settings
55+
PHPFPM__REQUEST_terminate_timeout=30s
56+
PHPFPM__pm__max_children=15

files/entrypoint-extras.sh

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
# Controls which extensions are enabled.
99

10+
CUSTOM_INI="/usr/local/etc/php/conf.d/zz-02-custom.ini"
11+
1012
if [ ! -z "${PHP_EXTENSIONS}" ]; then
1113
# If PHP_EXTENSIONS is set: only enable the ones specified
1214

@@ -85,10 +87,39 @@ if [ ! -z "${APPLICATION_UID}" ] || [ ! -z "${APPLICATION_GID}" ]; then
8587
fi
8688

8789
if [ ! -z "${PHP_INI_OVERRIDE}" ]; then
88-
echo "${PHP_INI_OVERRIDE}" | sed -e 's/\\n/\n/g' > /usr/local/etc/php/conf.d/zz-02-custom.ini
90+
echo "${PHP_INI_OVERRIDE}" | sed -e 's/\\n/\n/g' > "$CUSTOM_INI"
8991
fi
9092
unset PHP_INI_OVERRIDE
9193

94+
# Fill from ENV variables prefixed with PHPINI__
95+
# Example: PHPINI__session__save_handler=redis -> session.save_handler = redis
96+
# PHPINI__redis__session__locking_enabled=1 -> redis.session.locking_enabled = 1
97+
if env | grep -q '^PHPINI__'; then
98+
# Ensure the custom ini exists (and keep any content already written above)
99+
touch "$CUSTOM_INI"
100+
# Iterate over all matching env var names only
101+
for name in $(printenv | awk -F= '/^PHPINI__/ {print $1}'); do
102+
value=$(printenv "$name")
103+
# Transform key: PHPINI__this__setting => this.setting
104+
key=${name#PHPINI__}
105+
key=$(printf '%s' "$key" | sed 's/__/./g')
106+
key=${key,,}
107+
# Append as "key = value" (value is written as-is; quote in ENV if needed)
108+
printf '* setting in php.ini: %s = %s\n' "$key" "$value"
109+
printf '%s = %s\n' "$key" "$value" >> "$CUSTOM_INI"
110+
# Unset them, not relevant to the running containers
111+
unset "$name"
112+
done
113+
fi
114+
115+
if [ -s "$CUSTOM_INI" ]
116+
then
117+
echo "* Custom php.ini settings ($CUSTOM_INI)"
118+
echo "- - - 8< - - -"
119+
cat $CUSTOM_INI
120+
echo "- - - 8< - - -"
121+
fi
122+
92123
# Remove ENV variables that are meant only for the SSH container
93124

94125
unset SSH_PRIVATE_KEY

files/entrypoint.sh

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,29 @@ if [ ! -z "${PHP_FPM_OVERRIDE}" ]; then
3232
fi
3333
unset PHP_FPM_OVERRIDE
3434

35+
# Fill from ENV variables prefixed with PHPFPM__
36+
# Examples:
37+
# PHPFPM__pm__max_children=15 => pm.max_children = 15
38+
# PHPFPM__request_terminate_timeout=30s => request_terminate_timeout = 30s
39+
# PHPFPM__slowlog=/data/php-logs/slow.log => slowlog = /data/php-logs/slow.log
40+
#
41+
if env | grep -q '^PHPFPM__'; then
42+
# Ensure the custom ini exists (and keep any content already written above)
43+
touch "$CUSTOM_INI"
44+
# Iterate over all matching env var names only
45+
for name in $(printenv | awk -F= '/^PHPFPM__/ {print $1}'); do
46+
value=$(printenv "$name")
47+
# Transform key: PHPINI__this__setting => this.setting
48+
key=${name#PHPFPM__}
49+
key=$(printf '%s' "$key" | sed 's/__/./g')
50+
key=${key,,}
51+
# Append as "key = value" (value is written as-is; quote in ENV if needed)
52+
printf '* PHP-FPM pool setting: %s = %s\n' "$key" "$value"
53+
printf '%s = %s\n' "$key" "$value" >> "$PHP_FPM_POOL_CONF"
54+
# Unset them, not relevant to the running containers
55+
unset "$name"
56+
done
57+
fi
58+
3559
# Start the "real" entrypoint
3660
. /usr/local/bin/docker-php-entrypoint

0 commit comments

Comments
 (0)