Skip to content

Commit

Permalink
Add simple environment-based PHP configuration
Browse files Browse the repository at this point in the history
This adds a layer of common sense on top of the other changes I've
worked on to allow easy configuration variable setting.

Fixes #166
  • Loading branch information
andrewnicols committed Mar 17, 2023
1 parent 50f9dd3 commit af93b08
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test_buildx_and_publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ jobs:
docker run --name test0 -d -p 8000:80 \
-v $PWD/tests/fixtures:/var/www/html \
-v $PWD/tests/docker-entrypoint.d:/docker-entrypoint.d \
-e PHP_INI-memory_limit=256M \
-e PHP_INI-apc.enabled=0 \
moodle-php-apache
docker exec test0 php /var/www/html/test.php
docker exec test0 php /var/www/html/check-ini.php
Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,22 @@ docker run \

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

## PHP Configuration

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.

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.

```
docker run \
--name web0 \
-p 8080:80 \
-v $PWD/moodle:/var/www/html
-e PHP_INI-upload_max_filesize=200M \
-e PHP_INI-post_max_size=210M \
moodle-php-apache:latest
```

## See also
This container is part of a set of containers for Moodle development, see also:

Expand Down
4 changes: 4 additions & 0 deletions root/usr/local/bin/moodle-docker-php-entrypoint
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ docker_process_init_files() {
done
}

echo "Running PHP Configuration fetcher"
/usr/local/bin/moodle-docker-php-ini
echo

echo "Running entrypoint files from /docker-entrypoint.d/*"
docker_process_init_files /docker-entrypoint.d/*
echo
Expand Down
29 changes: 29 additions & 0 deletions root/usr/local/bin/moodle-docker-php-ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env bash

set -e

echo "Checking for php configuration in environment"

localinifile="/usr/local/etc/php/conf.d/10-local.ini"

cat <<'EOF' > $localinifile
; --
; Automatically generated php ini configuration for Moodle
; --
EOF

env | while IFS= read -r line; do
value=${line#*=}
fullname=${line%%=*}
if [[ $fullname = PHP_INI-* ]]; then
name=`echo $fullname | sed 's/^PHP_INI-//'`
echo "=> Found '${name}' with value '${value}'"

cat << EOF >> $localinifile
; $fullname=$value
$name = $value
EOF
fi
done
34 changes: 28 additions & 6 deletions tests/fixtures/check-ini.php
Original file line number Diff line number Diff line change
@@ -1,20 +1,42 @@
<?php

$uploadsEnabled = ini_get('file_uploads');
$fileuploads = ini_get('file_uploads');
$apcenabled = ini_get('apc.enabled');
$memorylimit = ini_get('memory_limit');

$allokay = true;
$message = [];
if (!empty($fileuploads)) {
$allokay = false;
$message[] = "Uploads are enabled and should be disabled.";
$message[] = var_export($fileuploads, true);
}

if (!empty($apcenabled)) {
$allokay = false;
$message[] = "apc.enabled is not Off (0): ({$apcenabled})";
}

if ($memorylimit !== '256M') {
$allokay = false;
$message[] = "Memory limit not set to 256M: ({$memorylimit})";
}

if (php_sapi_name() === 'cli') {
if (empty($uploadsEnabled)) {
if ($allokay) {
echo "OK\n";
exit(0);
}
echo "Uploads are enabled and should be disabled.";
var_dump($uploadsEnabled);

echo implode("\n", $message) . "\n";
exit(1);
} else {
if (empty($uploadsEnabled)) {
if ($allokay) {
header('HTTP/1.1 200 - OK');
exit(0);
}
header('HTTP/1.1 500 - Uploads are enabled and should be disabled: ' . var_export($uploadsEnabled, true));

header('HTTP/1.1 500 - ' . implode(", ", $message));
echo implode("<br>", $message);
exit(1);
}

0 comments on commit af93b08

Please sign in to comment.