Skip to content

Commit 25394f6

Browse files
authored
Merge pull request #62 from cron-eu/ini-from-env-vars
Allow setting php.ini and php-fpm pool setting via individual env vars
2 parents f03ca92 + d571b9c commit 25394f6

File tree

4 files changed

+96
-1
lines changed

4 files changed

+96
-1
lines changed

README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,30 @@ 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__SESSION__SAVE_PATH='"tcp://redis:6379?persistent=1&weight=1&database=10&prefix=PHPSESSID:"'
180+
PHPINI__REDIS__SESSION__LOCKING_ENABLED=1
181+
```
182+
183+
Or (lower case also fine):
184+
```
185+
PHPFPM__request_terminate_timeout=30s
186+
PHPFPM__pm__max_children=15
187+
```
188+
189+
**Note**: make sure that potentially required double quotes `"` are part of the
190+
"string" that ends up in the ENV variable, so quote as necessary, because this
191+
string is copied "as is" to the ini files. See example above for the
192+
`PHPINI__SESSION__SAVE_PATH` - this would be the syntax for a `.env` file.
169193

170194
## Example usage
171195

example-app/.env.example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,16 @@ 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, UPPER CASED
51+
PHPINI__SESSION__SAVE_HANDLER=redis
52+
PHPINI__SESSION__SAVE_PATH='"tcp://redis:6379?persistent=1&weight=1&database=10&prefix=PHPSESSID:"'
53+
PHPINI__REDIS__SESSION__LOCKING_ENABLED=1
54+
55+
# Example of setting PHP-FPM settings, lower cased also possible
56+
PHPFPM__request_terminate_timeout=30s
57+
PHPFPM__pm__max_children=15

files/entrypoint-extras.sh

Lines changed: 35 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

@@ -84,11 +86,43 @@ if [ ! -z "${APPLICATION_UID}" ] || [ ! -z "${APPLICATION_GID}" ]; then
8486
test -d /home/application && find /home/application/ -mount -not -user application -exec chown application: {} \;
8587
fi
8688

89+
# Start with a clean custom php.ini:
90+
rm -f "$CUSTOM_INI"
91+
8792
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
93+
echo "${PHP_INI_OVERRIDE}" | sed -e 's/\\n/\n/g' > "$CUSTOM_INI"
8994
fi
9095
unset PHP_INI_OVERRIDE
9196

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

94128
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=$(printf '%s' "$key" | tr '[:upper:]' '[:lower:]')
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)