Skip to content

Commit ff023e2

Browse files
authored
Merge pull request #167 from andrewnicols/phpini
Add simple environment-based PHP configuration
2 parents 9185c69 + af93b08 commit ff023e2

File tree

5 files changed

+79
-6
lines changed

5 files changed

+79
-6
lines changed

.github/workflows/test_buildx_and_publish.yml

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ jobs:
2727
docker run --name test0 -d -p 8000:80 \
2828
-v $PWD/tests/fixtures:/var/www/html \
2929
-v $PWD/tests/docker-entrypoint.d:/docker-entrypoint.d \
30+
-e PHP_INI-memory_limit=256M \
31+
-e PHP_INI-apc.enabled=0 \
3032
moodle-php-apache
3133
docker exec test0 php /var/www/html/test.php
3234
docker exec test0 php /var/www/html/check-ini.php

README.md

+16
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ docker run \
7474

7575
These initialization files will be executed in sorted name order as defined by the current locale, which defaults to en_US.utf8.
7676

77+
## PHP Configuration
78+
79+
As a lightweight alternative to a full PHP configuration file, you can specify a set of prefixed environment variables when starting your container with these variables turned into ini-format configuration.
80+
81+
Any environment variable whose name is prefixed with `PHP_INI-` will have the prefix removed, and will be added to a new ini file before the main command starts.
82+
83+
```
84+
docker run \
85+
--name web0 \
86+
-p 8080:80 \
87+
-v $PWD/moodle:/var/www/html
88+
-e PHP_INI-upload_max_filesize=200M \
89+
-e PHP_INI-post_max_size=210M \
90+
moodle-php-apache:latest
91+
```
92+
7793
## See also
7894
This container is part of a set of containers for Moodle development, see also:
7995

root/usr/local/bin/moodle-docker-php-entrypoint

+4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ docker_process_init_files() {
2727
done
2828
}
2929

30+
echo "Running PHP Configuration fetcher"
31+
/usr/local/bin/moodle-docker-php-ini
32+
echo
33+
3034
echo "Running entrypoint files from /docker-entrypoint.d/*"
3135
docker_process_init_files /docker-entrypoint.d/*
3236
echo
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
echo "Checking for php configuration in environment"
6+
7+
localinifile="/usr/local/etc/php/conf.d/10-local.ini"
8+
9+
cat <<'EOF' > $localinifile
10+
; --
11+
; Automatically generated php ini configuration for Moodle
12+
; --
13+
14+
EOF
15+
16+
env | while IFS= read -r line; do
17+
value=${line#*=}
18+
fullname=${line%%=*}
19+
if [[ $fullname = PHP_INI-* ]]; then
20+
name=`echo $fullname | sed 's/^PHP_INI-//'`
21+
echo "=> Found '${name}' with value '${value}'"
22+
23+
cat << EOF >> $localinifile
24+
; $fullname=$value
25+
$name = $value
26+
27+
EOF
28+
fi
29+
done

tests/fixtures/check-ini.php

+28-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
11
<?php
22

3-
$uploadsEnabled = ini_get('file_uploads');
3+
$fileuploads = ini_get('file_uploads');
4+
$apcenabled = ini_get('apc.enabled');
5+
$memorylimit = ini_get('memory_limit');
6+
7+
$allokay = true;
8+
$message = [];
9+
if (!empty($fileuploads)) {
10+
$allokay = false;
11+
$message[] = "Uploads are enabled and should be disabled.";
12+
$message[] = var_export($fileuploads, true);
13+
}
14+
15+
if (!empty($apcenabled)) {
16+
$allokay = false;
17+
$message[] = "apc.enabled is not Off (0): ({$apcenabled})";
18+
}
19+
20+
if ($memorylimit !== '256M') {
21+
$allokay = false;
22+
$message[] = "Memory limit not set to 256M: ({$memorylimit})";
23+
}
424

525
if (php_sapi_name() === 'cli') {
6-
if (empty($uploadsEnabled)) {
26+
if ($allokay) {
727
echo "OK\n";
828
exit(0);
929
}
10-
echo "Uploads are enabled and should be disabled.";
11-
var_dump($uploadsEnabled);
30+
31+
echo implode("\n", $message) . "\n";
1232
exit(1);
1333
} else {
14-
if (empty($uploadsEnabled)) {
34+
if ($allokay) {
1535
header('HTTP/1.1 200 - OK');
1636
exit(0);
1737
}
18-
header('HTTP/1.1 500 - Uploads are enabled and should be disabled: ' . var_export($uploadsEnabled, true));
38+
39+
header('HTTP/1.1 500 - ' . implode(", ", $message));
40+
echo implode("<br>", $message);
1941
exit(1);
2042
}

0 commit comments

Comments
 (0)