Skip to content

Commit 891b710

Browse files
committed
Move "wp-config-docker.php" to non-beta (5.7 GA)
1 parent 24c4469 commit 891b710

32 files changed

+1372
-2317
lines changed

Dockerfile.template

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ RUN set -ex; \
251251

252252
VOLUME /var/www/html
253253

254-
{{ if env.version == "beta" then ( -}}
254+
{{ if env.version != "cli" then ( -}}
255255
COPY --chown=www-data:www-data wp-config-docker.php /usr/src/wordpress/
256256
{{ ) else "" end -}}
257257
COPY docker-entrypoint.sh /usr/local/bin/

apply-templates.sh

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,8 @@ for version; do
5252

5353
if [ "$version" = 'cli' ]; then
5454
cp -a cli-entrypoint.sh "$dir/docker-entrypoint.sh"
55-
elif [ "$version" = 'beta' ]; then
56-
cp -a docker-entrypoint-ng.sh "$dir/docker-entrypoint.sh"
57-
cp -a wp-config-docker.php "$dir/"
5855
else
59-
cp -a docker-entrypoint.sh "$dir/"
56+
cp -a docker-entrypoint.sh wp-config-docker.php "$dir/"
6057
fi
6158
done
6259
done

docker-entrypoint-ng.sh

Lines changed: 0 additions & 91 deletions
This file was deleted.

docker-entrypoint.sh

Lines changed: 28 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
#!/bin/bash
2-
set -euo pipefail
1+
#!/usr/bin/env bash
2+
set -Eeuo pipefail
33

4-
# usage: file_env VAR [DEFAULT]
5-
# ie: file_env 'XYZ_DB_PASSWORD' 'example'
6-
# (will allow for "$XYZ_DB_PASSWORD_FILE" to fill in the value of
7-
# "$XYZ_DB_PASSWORD" from a file, especially for Docker's secrets feature)
8-
file_env() {
9-
local var="$1"
10-
local fileVar="${var}_FILE"
11-
local def="${2:-}"
12-
if [ "${!var:-}" ] && [ "${!fileVar:-}" ]; then
13-
echo >&2 "error: both $var and $fileVar are set (but are exclusive)"
14-
exit 1
15-
fi
16-
local val="$def"
17-
if [ "${!var:-}" ]; then
18-
val="${!var}"
19-
elif [ "${!fileVar:-}" ]; then
20-
val="$(< "${!fileVar}")"
21-
fi
22-
export "$var"="$val"
23-
unset "$fileVar"
24-
}
25-
26-
if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
27-
if [ "$(id -u)" = '0' ]; then
4+
if [[ "$1" == apache2* ]] || [ "$1" = 'php-fpm' ]; then
5+
uid="$(id -u)"
6+
gid="$(id -g)"
7+
if [ "$uid" = '0' ]; then
288
case "$1" in
299
apache2*)
3010
user="${APACHE_RUN_USER:-www-data}"
@@ -41,13 +21,13 @@ if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
4121
;;
4222
esac
4323
else
44-
user="$(id -u)"
45-
group="$(id -g)"
24+
user="$uid"
25+
group="$gid"
4626
fi
4727

4828
if [ ! -e index.php ] && [ ! -e wp-includes/version.php ]; then
4929
# if the directory exists and WordPress doesn't appear to be installed AND the permissions of it are root:root, let's chown it (likely a Docker-created directory)
50-
if [ "$(id -u)" = '0' ] && [ "$(stat -c '%u:%g' .)" = '0:0' ]; then
30+
if [ "$uid" = '0' ] && [ "$(stat -c '%u:%g' .)" = '0:0' ]; then
5131
chown "$user:$group" .
5232
fi
5333

@@ -65,7 +45,7 @@ if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
6545
--extract
6646
--file -
6747
)
68-
if [ "$user" != '0' ]; then
48+
if [ "$uid" != '0' ]; then
6949
# avoid "tar: .: Cannot utime: Operation not permitted" and "tar: .: Cannot change mode to rwxr-xr-x: Operation not permitted"
7050
targetTarArgs+=( --no-overwrite-dir )
7151
fi
@@ -84,202 +64,28 @@ if [[ "$1" == apache2* ]] || [ "$1" == php-fpm ]; then
8464
echo >&2 "Complete! WordPress has been successfully copied to $PWD"
8565
fi
8666

87-
# allow any of these "Authentication Unique Keys and Salts." to be specified via
88-
# environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY")
89-
uniqueEnvs=(
90-
AUTH_KEY
91-
SECURE_AUTH_KEY
92-
LOGGED_IN_KEY
93-
NONCE_KEY
94-
AUTH_SALT
95-
SECURE_AUTH_SALT
96-
LOGGED_IN_SALT
97-
NONCE_SALT
98-
)
99-
envs=(
100-
WORDPRESS_DB_HOST
101-
WORDPRESS_DB_USER
102-
WORDPRESS_DB_PASSWORD
103-
WORDPRESS_DB_NAME
104-
WORDPRESS_DB_CHARSET
105-
WORDPRESS_DB_COLLATE
106-
"${uniqueEnvs[@]/#/WORDPRESS_}"
107-
WORDPRESS_TABLE_PREFIX
108-
WORDPRESS_DEBUG
109-
WORDPRESS_CONFIG_EXTRA
110-
)
111-
haveConfig=
112-
for e in "${envs[@]}"; do
113-
file_env "$e"
114-
if [ -z "$haveConfig" ] && [ -n "${!e}" ]; then
115-
haveConfig=1
116-
fi
117-
done
118-
119-
# linking backwards-compatibility
120-
if [ -n "${!MYSQL_ENV_MYSQL_*}" ]; then
121-
haveConfig=1
122-
# host defaults to "mysql" below if unspecified
123-
: "${WORDPRESS_DB_USER:=${MYSQL_ENV_MYSQL_USER:-root}}"
124-
if [ "$WORDPRESS_DB_USER" = 'root' ]; then
125-
: "${WORDPRESS_DB_PASSWORD:=${MYSQL_ENV_MYSQL_ROOT_PASSWORD:-}}"
126-
else
127-
: "${WORDPRESS_DB_PASSWORD:=${MYSQL_ENV_MYSQL_PASSWORD:-}}"
128-
fi
129-
: "${WORDPRESS_DB_NAME:=${MYSQL_ENV_MYSQL_DATABASE:-}}"
130-
fi
131-
132-
# only touch "wp-config.php" if we have environment-supplied configuration values
133-
if [ "$haveConfig" ]; then
134-
: "${WORDPRESS_DB_HOST:=mysql}"
135-
: "${WORDPRESS_DB_USER:=root}"
136-
: "${WORDPRESS_DB_PASSWORD:=}"
137-
: "${WORDPRESS_DB_NAME:=wordpress}"
138-
: "${WORDPRESS_DB_CHARSET:=utf8}"
139-
: "${WORDPRESS_DB_COLLATE:=}"
140-
141-
# version 4.4.1 decided to switch to windows line endings, that breaks our seds and awks
142-
# https://github.com/docker-library/wordpress/issues/116
143-
# https://github.com/WordPress/WordPress/commit/1acedc542fba2482bab88ec70d4bea4b997a92e4
144-
sed -ri -e 's/\r$//' wp-config*
145-
146-
if [ ! -e wp-config.php ]; then
147-
awk '
148-
/^\/\*.*stop editing.*\*\/$/ && c == 0 {
149-
c = 1
150-
system("cat")
151-
if (ENVIRON["WORDPRESS_CONFIG_EXTRA"]) {
152-
print "// WORDPRESS_CONFIG_EXTRA"
153-
print ENVIRON["WORDPRESS_CONFIG_EXTRA"] "\n"
67+
wpEnvs=( "${!WORDPRESS_@}" )
68+
if [ ! -s wp-config.php ] && [ "${#wpEnvs[@]}" -gt 0 ]; then
69+
for wpConfigDocker in \
70+
wp-config-docker.php \
71+
/usr/src/wordpress/wp-config-docker.php \
72+
; do
73+
if [ -s "$wpConfigDocker" ]; then
74+
echo >&2 "No 'wp-config.php' found in $PWD, but 'WORDPRESS_...' variables supplied; copying '$wpConfigDocker' (${wpEnvs[*]})"
75+
# using "awk" to replace all instances of "put your unique phrase here" with a properly unique string (for AUTH_KEY and friends to have safe defaults if they aren't specified with environment variables)
76+
awk '
77+
/put your unique phrase here/ {
78+
cmd = "head -c1m /dev/urandom | sha1sum | cut -d\\ -f1"
79+
cmd | getline str
80+
close(cmd)
81+
gsub("put your unique phrase here", str)
15482
}
155-
}
156-
{ print }
157-
' wp-config-sample.php > wp-config.php <<'EOPHP'
158-
// If we're behind a proxy server and using HTTPS, we need to alert WordPress of that fact
159-
// see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
160-
if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
161-
$_SERVER['HTTPS'] = 'on';
162-
}
163-
164-
EOPHP
165-
chown "$user:$group" wp-config.php
166-
elif [ -e wp-config.php ] && [ -n "$WORDPRESS_CONFIG_EXTRA" ] && [[ "$(< wp-config.php)" != *"$WORDPRESS_CONFIG_EXTRA"* ]]; then
167-
# (if the config file already contains the requested PHP code, don't print a warning)
168-
echo >&2
169-
echo >&2 'WARNING: environment variable "WORDPRESS_CONFIG_EXTRA" is set, but "wp-config.php" already exists'
170-
echo >&2 ' The contents of this variable will _not_ be inserted into the existing "wp-config.php" file.'
171-
echo >&2 ' (see https://github.com/docker-library/wordpress/issues/333 for more details)'
172-
echo >&2
173-
fi
174-
175-
# see http://stackoverflow.com/a/2705678/433558
176-
sed_escape_lhs() {
177-
echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g'
178-
}
179-
sed_escape_rhs() {
180-
echo "$@" | sed -e 's/[\/&]/\\&/g'
181-
}
182-
php_escape() {
183-
local escaped="$(php -r 'var_export(('"$2"') $argv[1]);' -- "$1")"
184-
if [ "$2" = 'string' ] && [ "${escaped:0:1}" = "'" ]; then
185-
escaped="${escaped//$'\n'/"' + \"\\n\" + '"}"
186-
fi
187-
echo "$escaped"
188-
}
189-
set_config() {
190-
key="$1"
191-
value="$2"
192-
var_type="${3:-string}"
193-
start="(['\"])$(sed_escape_lhs "$key")\2\s*,"
194-
end="\);"
195-
if [ "${key:0:1}" = '$' ]; then
196-
start="^(\s*)$(sed_escape_lhs "$key")\s*="
197-
end=";"
198-
fi
199-
sed -ri -e "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php
200-
}
201-
202-
set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
203-
set_config 'DB_USER' "$WORDPRESS_DB_USER"
204-
set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD"
205-
set_config 'DB_NAME' "$WORDPRESS_DB_NAME"
206-
set_config 'DB_CHARSET' "$WORDPRESS_DB_CHARSET"
207-
set_config 'DB_COLLATE' "$WORDPRESS_DB_COLLATE"
208-
209-
for unique in "${uniqueEnvs[@]}"; do
210-
uniqVar="WORDPRESS_$unique"
211-
if [ -n "${!uniqVar}" ]; then
212-
set_config "$unique" "${!uniqVar}"
213-
else
214-
# if not specified, let's generate a random value
215-
currentVal="$(sed -rn -e "s/define\(\s*(([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\s*\);/\4/p" wp-config.php)"
216-
if [ "$currentVal" = 'put your unique phrase here' ]; then
217-
set_config "$unique" "$(head -c1m /dev/urandom | sha1sum | cut -d' ' -f1)"
218-
fi
83+
{ print }
84+
' "$wpConfigDocker" > wp-config.php
85+
break
21986
fi
22087
done
221-
222-
if [ "$WORDPRESS_TABLE_PREFIX" ]; then
223-
set_config '$table_prefix' "$WORDPRESS_TABLE_PREFIX"
224-
fi
225-
226-
if [ "$WORDPRESS_DEBUG" ]; then
227-
set_config 'WP_DEBUG' 1 boolean
228-
fi
229-
230-
if ! TERM=dumb php -- <<'EOPHP'
231-
<?php
232-
// database might not exist, so let's try creating it (just to be safe)
233-
234-
$stderr = fopen('php://stderr', 'w');
235-
236-
// https://codex.wordpress.org/Editing_wp-config.php#MySQL_Alternate_Port
237-
// "hostname:port"
238-
// https://codex.wordpress.org/Editing_wp-config.php#MySQL_Sockets_or_Pipes
239-
// "hostname:unix-socket-path"
240-
list($host, $socket) = explode(':', getenv('WORDPRESS_DB_HOST'), 2);
241-
$port = 0;
242-
if (is_numeric($socket)) {
243-
$port = (int) $socket;
244-
$socket = null;
245-
}
246-
$user = getenv('WORDPRESS_DB_USER');
247-
$pass = getenv('WORDPRESS_DB_PASSWORD');
248-
$dbName = getenv('WORDPRESS_DB_NAME');
249-
250-
$maxTries = 10;
251-
do {
252-
$mysql = new mysqli($host, $user, $pass, '', $port, $socket);
253-
if ($mysql->connect_error) {
254-
fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n");
255-
--$maxTries;
256-
if ($maxTries <= 0) {
257-
exit(1);
258-
}
259-
sleep(3);
260-
}
261-
} while ($mysql->connect_error);
262-
263-
if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($dbName) . '`')) {
264-
fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n");
265-
$mysql->close();
266-
exit(1);
267-
}
268-
269-
$mysql->close();
270-
EOPHP
271-
then
272-
echo >&2
273-
echo >&2 "WARNING: unable to establish a database connection to '$WORDPRESS_DB_HOST'"
274-
echo >&2 ' continuing anyways (which might have unexpected results)'
275-
echo >&2
276-
fi
27788
fi
278-
279-
# now that we're definitely done writing configuration, let's clear out the relevant envrionment variables (so that stray "phpinfo()" calls don't leak secrets from our code)
280-
for e in "${envs[@]}"; do
281-
unset "$e"
282-
done
28389
fi
28490

28591
exec "$@"

0 commit comments

Comments
 (0)