Skip to content

Commit 7438998

Browse files
committed
Allow specification of DocumentRoot with intelligent fallback
1 parent b248925 commit 7438998

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
@@ -51,9 +51,19 @@ RUN mkdir /var/www/moodledata && chown www-data /var/www/moodledata && \
5151

5252
ADD root/usr /usr
5353
ADD root/etc /etc
54+
ADD root/system-docker-entrypoint.d/wwwroot.sh /system-docker-entrypoint.d/10-wwwroot.sh
5455

5556
# Fix the original permissions of /tmp, the PHP default upload tmp dir.
5657
RUN chmod 777 /tmp && chmod +t /tmp
5758

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

README.md

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,61 @@ 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

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

3171
* `/var/www/moodledata`
@@ -35,9 +75,19 @@ To facilitate testing and easy setup the following directories are created and o
3575

3676
## Initialisation scripts
3777

38-
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`).
78+
This image supports custom initialisation scripts using the the `docker-entrypoint.d` directory. These may be in the following formats:
79+
80+
* a non-executable `.sh` script, which will be _sourced_ and alter the current context;
81+
* an executable `.sh` script, which will be _executed_ in the current context;
82+
* a `.ini` file. which will be copied into the PHP Configuration directory (`/usr/local/etc/php/conf.d`.)
3983

40-
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:
84+
The following scripts are included as standard:
85+
86+
* `10-wwwroot.sh` - a non-executable script used to guess the `APACHE_DOCUMENT_ROOT` if one is not provided.
87+
88+
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.
89+
90+
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:
4191

4292
```
4393
; Specify a max filesize of 200M for uploads.
@@ -58,22 +108,6 @@ docker run \
58108

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

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

79113
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.

0 commit comments

Comments
 (0)