SHOW:
|
|
- or go back to the newest paste.
1 | #!/bin/bash | |
2 | set -eu | |
3 | ||
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 | file_env 'WORDPRESS_DB_HOST' 'mysql' | |
28 | # if we're linked to MySQL and thus have credentials already, let's use them | |
29 | file_env 'WORDPRESS_DB_USER' "${MYSQL_ENV_MYSQL_USER:-root}" | |
30 | if [ "$WORDPRESS_DB_USER" = 'root' ]; then | |
31 | file_env 'WORDPRESS_DB_PASSWORD' "${MYSQL_ENV_MYSQL_ROOT_PASSWORD:-}" | |
32 | else | |
33 | file_env 'WORDPRESS_DB_PASSWORD' "${MYSQL_ENV_MYSQL_PASSWORD:-}" | |
34 | fi | |
35 | file_env 'WORDPRESS_DB_NAME' "${MYSQL_ENV_MYSQL_DATABASE:-wordpress}" | |
36 | if [ -z "$WORDPRESS_DB_PASSWORD" ]; then | |
37 | echo >&2 'error: missing required WORDPRESS_DB_PASSWORD environment variable' | |
38 | echo >&2 ' Did you forget to -e WORDPRESS_DB_PASSWORD=... ?' | |
39 | echo >&2 | |
40 | echo >&2 ' (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)' | |
41 | exit 1 | |
42 | fi | |
43 | ||
44 | if ! [ -e index.php -a -e wp-includes/version.php ]; then | |
45 | echo >&2 "WordPress not found in $(pwd) - copying now..." | |
46 | #if [ "$(ls -A)" ]; then | |
47 | # echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!" | |
48 | # ( set -x; ls -A; sleep 10 ) | |
49 | #fi | |
50 | tar cf - --one-file-system -C /usr/src/wordpress . | tar xf - | |
51 | echo >&2 "Complete! WordPress has been successfully copied to $(pwd)" | |
52 | if [ ! -e .htaccess ]; then | |
53 | # NOTE: The "Indexes" option is disabled in the php:apache base image | |
54 | cat > .htaccess <<-'EOF' | |
55 | # BEGIN WordPress | |
56 | <IfModule mod_rewrite.c> | |
57 | RewriteEngine On | |
58 | RewriteBase / | |
59 | RewriteRule ^index\.php$ - [L] | |
60 | RewriteCond %{REQUEST_FILENAME} !-f | |
61 | RewriteCond %{REQUEST_FILENAME} !-d | |
62 | RewriteRule . /index.php [L] | |
63 | </IfModule> | |
64 | # END WordPress | |
65 | EOF | |
66 | chown www-data:www-data .htaccess | |
67 | fi | |
68 | fi | |
69 | ||
70 | # TODO handle WordPress upgrades magically in the same way, but only if wp-includes/version.php's $wp_version is less than /usr/src/wordpress/wp-includes/version.php's $wp_version | |
71 | ||
72 | # version 4.4.1 decided to switch to windows line endings, that breaks our seds and awks | |
73 | # https://github.com/docker-library/wordpress/issues/116 | |
74 | # https://github.com/WordPress/WordPress/commit/1acedc542fba2482bab88ec70d4bea4b997a92e4 | |
75 | sed -ri -e 's/\r\n|\r/\n/g' wp-config* | |
76 | ||
77 | if [ ! -e wp-config.php ]; then | |
78 | awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP' | |
79 | // If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact | |
80 | // see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy | |
81 | if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') { | |
82 | $_SERVER['HTTPS'] = 'on'; | |
83 | } | |
84 | ||
85 | EOPHP | |
86 | chown www-data:www-data wp-config.php | |
87 | fi | |
88 | ||
89 | # see http://stackoverflow.com/a/2705678/433558 | |
90 | sed_escape_lhs() { | |
91 | echo "$@" | sed -e 's/[]\/$*.^|[]/\\&/g' | |
92 | } | |
93 | sed_escape_rhs() { | |
94 | echo "$@" | sed -e 's/[\/&]/\\&/g' | |
95 | } | |
96 | php_escape() { | |
97 | php -r 'var_export(('$2') $argv[1]);' -- "$1" | |
98 | } | |
99 | set_config() { | |
100 | key="$1" | |
101 | value="$2" | |
102 | var_type="${3:-string}" | |
103 | start="(['\"])$(sed_escape_lhs "$key")\2\s*," | |
104 | end="\);" | |
105 | if [ "${key:0:1}" = '$' ]; then | |
106 | start="^(\s*)$(sed_escape_lhs "$key")\s*=" | |
107 | end=";" | |
108 | fi | |
109 | sed -ri -e "s/($start\s*).*($end)$/\1$(sed_escape_rhs "$(php_escape "$value" "$var_type")")\3/" wp-config.php | |
110 | } | |
111 | ||
112 | set_config 'DB_HOST' "$WORDPRESS_DB_HOST" | |
113 | set_config 'DB_USER' "$WORDPRESS_DB_USER" | |
114 | set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD" | |
115 | set_config 'DB_NAME' "$WORDPRESS_DB_NAME" | |
116 | ||
117 | # allow any of these "Authentication Unique Keys and Salts." to be specified via | |
118 | # environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY") | |
119 | UNIQUES=( | |
120 | AUTH_KEY | |
121 | SECURE_AUTH_KEY | |
122 | LOGGED_IN_KEY | |
123 | NONCE_KEY | |
124 | AUTH_SALT | |
125 | SECURE_AUTH_SALT | |
126 | LOGGED_IN_SALT | |
127 | NONCE_SALT | |
128 | ) | |
129 | for unique in "${UNIQUES[@]}"; do | |
130 | uniqVar="WORDPRESS_$unique" | |
131 | file_env "$uniqVar" | |
132 | if [ "${!uniqVar}" ]; then | |
133 | set_config "$unique" "${!uniqVar}" | |
134 | else | |
135 | # if not specified, let's generate a random value | |
136 | current_set="$(sed -rn -e "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)" | |
137 | if [ "$current_set" = 'put your unique phrase here' ]; then | |
138 | set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)" | |
139 | fi | |
140 | fi | |
141 | done | |
142 | ||
143 | file_env 'WORDPRESS_TABLE_PREFIX' | |
144 | if [ "$WORDPRESS_TABLE_PREFIX" ]; then | |
145 | set_config '$table_prefix' "$WORDPRESS_TABLE_PREFIX" | |
146 | fi | |
147 | ||
148 | file_env 'WORDPRESS_DEBUG' | |
149 | if [ "$WORDPRESS_DEBUG" ]; then | |
150 | set_config 'WP_DEBUG' 1 boolean | |
151 | fi | |
152 | ||
153 | TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP' | |
154 | <?php | |
155 | // database might not exist, so let's try creating it (just to be safe) | |
156 | ||
157 | $stderr = fopen('php://stderr', 'w'); | |
158 | ||
159 | // https://codex.wordpress.org/Editing_wp-config.php#MySQL_Alternate_Port | |
160 | // "hostname:port" | |
161 | // https://codex.wordpress.org/Editing_wp-config.php#MySQL_Sockets_or_Pipes | |
162 | // "hostname:unix-socket-path" | |
163 | list($host, $socket) = explode(':', $argv[1], 2); | |
164 | $port = 0; | |
165 | if (is_numeric($socket)) { | |
166 | $port = (int) $socket; | |
167 | $socket = null; | |
168 | } | |
169 | ||
170 | $maxTries = 10; | |
171 | do { | |
172 | $mysql = new mysqli($host, $argv[2], $argv[3], '', $port, $socket); | |
173 | if ($mysql->connect_error) { | |
174 | fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n"); | |
175 | --$maxTries; | |
176 | if ($maxTries <= 0) { | |
177 | exit(1); | |
178 | } | |
179 | sleep(3); | |
180 | } | |
181 | } while ($mysql->connect_error); | |
182 | ||
183 | if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`')) { | |
184 | fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n"); | |
185 | $mysql->close(); | |
186 | exit(1); | |
187 | } | |
188 | ||
189 | $mysql->close(); | |
190 | EOPHP | |
191 | fi | |
192 | ||
193 | chown -R www-data:www-data /var/www/html | |
194 | envsubst '\$PHP_ERROR_REPORTING,\$PHP_DISPLAY_ERRORS,\$PHP_OPCACHE_ENABLED,\$PHP_MEM_LIMIT,\$PHP_POST_MAX_SIZE,\$TZ' < ${PHP_INI_DIR}/conf.d/tuning.template > ${PHP_INI_DIR}/conf.d/tuning.ini | |
195 | ||
196 | exec "$@" |