Skip to content

Commit 01108b7

Browse files
authored
Merge pull request #202 from andrewnicols/documentRoot
Allow specification of DocumentRoot with intelligent fallback
2 parents 8ced11e + c81c601 commit 01108b7

File tree

10 files changed

+151
-22
lines changed

10 files changed

+151
-22
lines changed

.github/workflows/test_buildx_and_publish.yml

Lines changed: 40 additions & 1 deletion
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,53 @@ 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 set to a different 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.
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
78+
docker logs test1
79+
docker logs test2
80+
docker logs test3
81+
docker logs test4
4782
4883
- name: Cleanup docker images
4984
run: |
5085
docker rm -f test0
86+
docker rm -f test1
87+
docker rm -f test2
88+
docker rm -f test3
89+
docker rm -f test4
5190
5291
Publish:
5392
# 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
@@ -34,22 +34,62 @@ A Moodle PHP environment configured for Moodle development based on [Official PH
3434
| PHP 5.6 | Jessie | 5.6-jessie | [![Build Status](https://travis-ci.com/moodlehq/moodle-php-apache.svg?branch=5.6-jessie)](https://travis-ci.com/moodlehq/moodle-php-apache)|Jessie and PHP 5.6 EOL|
3535

3636
## Example usage
37+
3738
The following command will expose the current working directory on port 8080:
3839
```bash
3940
$ docker run --name web0 -p 8080:80 -v $PWD:/var/www/html moodlehq/moodle-php-apache:8.3
4041
```
4142

4243
## Features
44+
4345
* Preconfigured with all php extensions required for Moodle development and all database drivers
44-
* Serves wwwroot configured at /var/www/html/
46+
* Serves content from `/var/www/html` or `/var/www/html/public` (for Moodle 5.1 onwards) by default.
47+
* Document root can be overridden
4548
* 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.
4649
* Verified by [automated tests](https://travis-ci.com/moodlehq/moodle-php-apache).
4750
* Autobuilt from GHA, on push.
4851
* Support for entrypoint scripts and PHP Configuration
4952
* Many common extensions available
5053
* Note that PHP 8.4 images do not include oci extensions as these are no longer supported by Moodle 5.0 onwards.
5154

55+
## Configuration
56+
57+
### Apache Configuration
58+
59+
This image makes use of the Apache HTTPD server to serve all content. It requires minimal manual configuration.
60+
61+
The Apache `DocumentRoot` directive can be configured using the `APACHE_DOCUMENT_ROOT` environment variable, for example:
62+
63+
```bash
64+
docker run \
65+
--name web0 \
66+
-p 8080:80 \
67+
-v $PWD/moodle:/srv/moodle
68+
-e APACHE_DOCUMENT_ROOT=/srv/moodle \
69+
moodle-php-apache:latest
70+
```
71+
72+
Note: Specifying a `DocumentRoot` will override the default root, and will prevent the ability for the image to automatically configure any Moodle-specific configuration.
73+
74+
### PHP Configuration
75+
76+
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.
77+
78+
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.
79+
80+
```bash
81+
docker run \
82+
--name web0 \
83+
-p 8080:80 \
84+
-v $PWD/moodle:/var/www/html
85+
-e PHP_INI-upload_max_filesize=200M \
86+
-e PHP_INI-post_max_size=210M \
87+
moodle-php-apache:latest
88+
```
89+
90+
5291
## Directories
92+
5393
To facilitate testing and easy setup the following directories are created and owned by www-data by default:
5494

5595
* `/var/www/moodledata`
@@ -59,9 +99,19 @@ To facilitate testing and easy setup the following directories are created and o
5999

60100
## Initialisation scripts
61101

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

64-
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:
108+
The following scripts are included as standard:
109+
110+
* `10-wwwroot.sh` - a non-executable script used to guess the `APACHE_DOCUMENT_ROOT` if one is not provided.
111+
112+
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.
113+
114+
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:
65115

66116
```
67117
; Specify a max filesize of 200M for uploads.
@@ -82,22 +132,6 @@ docker run \
82132

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

85-
## PHP Configuration
86-
87-
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.
88-
89-
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.
90-
91-
```
92-
docker run \
93-
--name web0 \
94-
-p 8080:80 \
95-
-v $PWD/moodle:/var/www/html
96-
-e PHP_INI-upload_max_filesize=200M \
97-
-e PHP_INI-post_max_size=210M \
98-
moodle-php-apache:latest
99-
```
100-
101135
## Extensions
102136

103137
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)