Skip to content

Commit 12891cc

Browse files
committed
Allow specification of DocumentRoot with intelligent fallback
1 parent e5a3879 commit 12891cc

File tree

10 files changed

+147
-26
lines changed

10 files changed

+147
-26
lines changed

.github/workflows/test_buildx_and_publish.yml

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Run tests
2626
run: |
2727
docker run --name test0 -d -p 8000:80 \
28-
-v $PWD/tests/fixtures:/var/www/html \
28+
-v $PWD/tests/fixtures/entrypoint:/var/www/html \
2929
-v $PWD/tests/docker-entrypoint.d:/docker-entrypoint.d \
3030
-e PHP_INI-memory_limit=256M \
3131
-e PHP_INI-apc.enabled=0 \
@@ -40,14 +40,45 @@ jobs:
4040
curl --fail http://127.0.0.1:8000/test.php
4141
curl --fail http://127.0.0.1:8000/check-ini.php
4242
43+
# Test the default DocumentRoot when it contains a public directory.
44+
docker run --name test1 -d -p 8002:80 \
45+
-v $PWD/tests/fixtures/documentroot:/var/www/html \
46+
moodle-php-apache
47+
sleep 1 # Wait for the server to start
48+
# Test the DocumentRoot when it contains a public directory.
49+
curl --fail http://127.0.0.1:8002/test.php
50+
51+
# Test the DocumentRoot when it does not contain a public directory.
52+
docker run --name test2 -d -p 8003:80 \
53+
-v $PWD/tests/fixtures/documentroot/public:/var/www/html \
54+
moodle-php-apache
55+
sleep 1 # Wait for the server to start
56+
curl --fail http://127.0.0.1:8003/test.php
57+
58+
# Test the DocumentRoot when it is has been overridden and contains a public directory.
59+
docker run --name test3 -d -p 8004:80 \
60+
-v $PWD/tests/fixtures/documentroot:/srv/moodle \
61+
-e APACHE_DOCUMENT_ROOT=/srv/moodle \
62+
moodle-php-apache
63+
sleep 1 # Wait for the server to start
64+
curl --fail http://127.0.0.1:8004/notpublic.php
65+
66+
# Test the DocumentRoot is not guessed when it has been overriden and does not contain a public directory.
67+
docker run --name test4 -d -p 8005:80 \
68+
-v $PWD/tests/fixtures/documentroot/public:/srv/moodle \
69+
-e APACHE_DOCUMENT_ROOT=/srv/moodle \
70+
moodle-php-apache
71+
sleep 1 # Wait for the server to start
72+
curl --fail http://127.0.0.1:8005/test.php
73+
4374
- name: Display container logs on failure
4475
if: failure()
4576
run: |
4677
docker logs test0
47-
48-
- name: Cleanup docker images
49-
run: |
50-
docker rm -f test0
78+
docker logs test1
79+
docker logs test2
80+
docker logs test3
81+
docker logs test4
5182
5283
Publish:
5384
# Completely avoid forks and pull requests to try this job.

Dockerfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,19 @@ RUN mkdir /var/www/moodledata && chown www-data /var/www/moodledata && \
4646

4747
ADD root/usr /usr
4848
ADD root/etc /etc
49+
ADD root/system-docker-entrypoint.d/wwwroot.sh /system-docker-entrypoint.d/10-wwwroot.sh
4950

5051
# Fix the original permissions of /tmp, the PHP default upload tmp dir.
5152
RUN chmod 777 /tmp && chmod +t /tmp
5253

54+
# Allow configuration of the Apache DocumentRoot via environment variable.
55+
# Note: Do not specify a default value here, as it will be set in the
56+
# `wwwroot.sh` script, which will be run before the Apache server starts.
57+
# This allows the user to override the default value by setting the
58+
# `APACHE_DOCUMENT_ROOT` environment variable when running the container.
59+
60+
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf
61+
RUN sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
62+
5363
CMD ["apache2-foreground"]
5464
ENTRYPOINT ["moodle-docker-php-entrypoint"]

README.md

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,62 @@ A Moodle PHP environment configured for Moodle development based on [Official PH
1111
For a complete list of supported versions, look to the [master README](https://github.com/moodlehq/moodle-php-apache/tree/master).
1212

1313
## Example usage
14+
1415
The following command will expose the current working directory on port 8080:
1516
```bash
1617
$ docker run --name web0 -p 8080:80 -v $PWD:/var/www/html moodlehq/moodle-php-apache:7.1
1718
```
1819

1920
## Features
21+
2022
* Preconfigured with all php extensions required for Moodle development and all database drivers
21-
* Serves wwroot configured at /var/www/html/
23+
* Serves content from `/var/www/html` or `/var/www/html/public` (for Moodle 5.1 onwards) by default.
24+
* Document root can be overridden
2225
* For PHP 7.3 and up, both `linux/amd64` and `linux/arm64` images are being built. Note that `linux/arm64` doesn't support the sqlsrv and oci extensions yet. Other than that, both architectures work exactly the same.
2326
* Verified by [automated tests](https://travis-ci.com/moodlehq/moodle-php-apache).
2427
* Autobuilt from GHA, on push.
2528
* Support for entrypoint scripts and PHP Configuration
2629
* Many common extensions available
2730
* Note that PHP 8.4 images do not include oci extensions as these are no longer supported by Moodle 5.0 onwards.
2831

32+
## Configuration
33+
34+
### Apache Configuration
35+
36+
This image makes use of the Apache HTTPD server to serve all content. It requires minimal manual configuration.
37+
38+
The Apache `DocumentRoot` directive can be configured using the `APACHE_DOCUMENT_ROOT` environment variable, for example:
39+
40+
```bash
41+
docker run \
42+
--name web0 \
43+
-p 8080:80 \
44+
-v $PWD/moodle:/srv/moodle
45+
-e APACHE_DOCUMENT_ROOT=/srv/moodle \
46+
moodle-php-apache:latest
47+
```
48+
49+
Note: Specifying a `DocumentRoot` will override the default root, and will prevent the ability for the image to automatically configure any Moodle-specific configuration.
50+
51+
### PHP Configuration
52+
53+
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.
54+
55+
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.
56+
57+
```bash
58+
docker run \
59+
--name web0 \
60+
-p 8080:80 \
61+
-v $PWD/moodle:/var/www/html
62+
-e PHP_INI-upload_max_filesize=200M \
63+
-e PHP_INI-post_max_size=210M \
64+
moodle-php-apache:latest
65+
```
66+
67+
2968
## Directories
69+
3070
To facilitate testing and easy setup the following directories are created and owned by www-data by default:
3171

3272
* `/var/www/moodledata`
@@ -36,9 +76,19 @@ To facilitate testing and easy setup the following directories are created and o
3676

3777
## Initialisation scripts
3878

39-
If you would like to do additional initialization, you can add one or more `*.sh`, or `*.ini` scripts under `/docker-entrypoint.d` (creating the directory if necessary). When the entrypoint script is called, it will run any executable `*.sh` script, source any non-executable `*.sh` scripts found in that directory, and will copy any `*.ini` scripts into the PHP Configuration directory (`/usr/local/etc/php/conf.d`).
79+
This image supports custom initialisation scripts using the the `docker-entrypoint.d` directory. These may be in the following formats:
80+
81+
* a non-executable `.sh` script, which will be _sourced_ and alter the current context;
82+
* an executable `.sh` script, which will be _executed_ in the current context;
83+
* a `.ini` file. which will be copied into the PHP Configuration directory (`/usr/local/etc/php/conf.d`.)
4084

41-
For example, to configure PHP to support a higher `upload_max_filesize` option you might add the following to a `config/10-uploads.ini` file:
85+
The following scripts are included as standard:
86+
87+
* `10-wwwroot.sh` - a non-executable script used to guess the `APACHE_DOCUMENT_ROOT` if one is not provided.
88+
89+
These scripts cannot be removed, but may be disabled by creating a file with a matching file name in your own `docker-entrypoint.d` location.
90+
91+
Other scripts may also be provided, for example, to configure PHP to support a higher `upload_max_filesize` option you might add the following to a `config/10-uploads.ini` file:
4292

4393
```
4494
; Specify a max filesize of 200M for uploads.
@@ -59,22 +109,6 @@ docker run \
59109

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

62-
## PHP Configuration
63-
64-
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.
65-
66-
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.
67-
68-
```
69-
docker run \
70-
--name web0 \
71-
-p 8080:80 \
72-
-v $PWD/moodle:/var/www/html
73-
-e PHP_INI-upload_max_filesize=200M \
74-
-e PHP_INI-post_max_size=210M \
75-
moodle-php-apache:latest
76-
```
77-
78112
## Extensions
79113

80114
The following extensions are included as standard:
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
echo
2+
echo "#######################################"
3+
echo "# moodle-php-apache wwwroot setup"
4+
echo "#######################################"
5+
echo "#"
6+
echo "# Setting up Apache DocumentRoot"
7+
8+
if [ -z "$APACHE_DOCUMENT_ROOT" ]; then
9+
echo "# No value set for \$APACHE_DOCUMENT_ROOT. Creating default value."
10+
export APACHE_DOCUMENT_ROOT=/var/www/html
11+
12+
if [ -d "$APACHE_DOCUMENT_ROOT/public" ]; then
13+
echo "# Detected /public directory."
14+
echo "# Using /var/www/html/public"
15+
export APACHE_DOCUMENT_ROOT="$APACHE_DOCUMENT_ROOT/public"
16+
else
17+
echo "# Using default Apache DocumentRoot: /var/www/html"
18+
fi
19+
20+
else
21+
echo "# A value was provided as an environment variable"
22+
fi
23+
24+
echo "# \$APACHE_DOCUMENT_ROOT: $APACHE_DOCUMENT_ROOT"
25+
echo "#"
26+
echo "#######################################"
27+
echo

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ docker_process_init_files() {
1919
. "$f"
2020
fi
2121
;;
22+
*.conf)
23+
echo "$0: copying $f into /etc/apache2/conf-enabled/"
24+
cp "$f" /etc/apache2/conf-enabled/
25+
a2enconf "$(basename "$f")"
26+
;;
2227
*.ini)
2328
echo "$0: copying $f into /usr/local/etc/php/conf.d/"
2429
cp "$f" /usr/local/etc/php/conf.d/
@@ -31,8 +36,16 @@ echo "Running PHP Configuration fetcher"
3136
/usr/local/bin/moodle-docker-php-ini
3237
echo
3338

34-
echo "Running entrypoint files from /docker-entrypoint.d/*"
35-
docker_process_init_files /docker-entrypoint.d/*
39+
mkdir -p /system-docker-entrypoint.d /docker-entrypoint.d /final-docker-entrypoint.d
40+
echo "Building Entrypoint files from /system-docker-entrypoint.d/* and /docker-entrypoint.d/*"
41+
42+
echo "Copying /system-docker-entrypoint.d/* to /final-docker-entrypoint.d/"
43+
cp -rf /system-docker-entrypoint.d/* /final-docker-entrypoint.d/ || true
44+
echo "Copying /docker-entrypoint.d/* to /final-docker-entrypoint.d/"
45+
cp -rf /docker-entrypoint.d/* /final-docker-entrypoint.d/ || true
46+
47+
echo "Running entrypoint files from /final-docker-entrypoint.d/*"
48+
docker_process_init_files /final-docker-entrypoint.d/*
3649
echo
3750

3851
echo "Starting docker-php-entrypoint with $@"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
header('HTTP/1.1 200 - OK');
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<?php
2+
3+
header('HTTP/1.1 200 - OK');
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)