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