7 if [ -n "$MYSQL_PORT_3306_TCP" ]; then
9 if [ -z "$WORDPRESS_DB_HOST" ]; then
11 WORDPRESS_DB_HOST='mysql'
15 echo >&2 'warning: both WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP found'
17 echo >&2 " Connecting to WORDPRESS_DB_HOST ($WORDPRESS_DB_HOST)"
19 echo >&2 ' instead of the linked mysql container'
27 if [ -z "$WORDPRESS_DB_HOST" ]; then
29 echo >&2 'error: missing WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP environment variables'
31 echo >&2 ' Did you forget to --link some_mysql_container:mysql or set an external db'
33 echo >&2 ' with -e WORDPRESS_DB_HOST=hostname:port?'
41 # if we're linked to MySQL, and we're using the root user, and our linked
43 # container has a default "root" password set up and passed through... :)
45 : ${WORDPRESS_DB_USER:=root}
47 if [ "$WORDPRESS_DB_USER" = 'root' ]; then
49 : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
53 : ${WORDPRESS_DB_NAME:=wordpress}
57 if [ -z "$WORDPRESS_DB_PASSWORD" ]; then
59 echo >&2 'error: missing required WORDPRESS_DB_PASSWORD environment variable'
61 echo >&2 ' Did you forget to -e WORDPRESS_DB_PASSWORD=... ?'
65 echo >&2 ' (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)'
73 if ! [ -e index.php -a -e wp-includes/version.php ]; then
75 echo >&2 "WordPress not found in $(pwd) - copying now..."
77 if [ "$(ls -A)" ]; then
79 echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!"
81 ( set -x; ls -A; sleep 10 )
85 tar cf - --one-file-system -C /usr/src/wordpress . | tar xf -
87 echo >&2 "Complete! WordPress has been successfully copied to $(pwd)"
89 if [ ! -e .htaccess ]; then
91 # NOTE: The "Indexes" option is disabled in the php:apache base image
93 cat > .htaccess <<-'EOF'
97 <IfModule mod_rewrite.c>
103 RewriteRule ^index\.php$ - [L]
105 RewriteCond %{REQUEST_FILENAME} !-f
107 RewriteCond %{REQUEST_FILENAME} !-d
109 RewriteRule . /index.php [L]
117 chown www-data:www-data .htaccess
125 # 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
129 if [ ! -e wp-config.php ]; then
131 awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP'
133 // If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact
135 // see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
137 if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
139 $_SERVER['HTTPS'] = 'on';
145 chown www-data:www-data wp-config.php
157 php_escaped_value="$(php -r 'var_export($argv[1]);' "$value")"
159 sed_escaped_value="$(echo "$php_escaped_value" | sed 's/[\/&]/\\&/g')"
161 sed -ri "s/((['\"])$key\2\s*,\s*)(['\"]).*\3/\1$sed_escaped_value/" wp-config.php
167 set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
169 set_config 'DB_USER' "$WORDPRESS_DB_USER"
171 set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD"
173 set_config 'DB_NAME' "$WORDPRESS_DB_NAME"
177 # allow any of these "Authentication Unique Keys and Salts." to be specified via
179 # environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY")
201 for unique in "${UNIQUES[@]}"; do
203 eval unique_value=\$WORDPRESS_$unique
205 if [ "$unique_value" ]; then
207 set_config "$unique" "$unique_value"
211 # if not specified, let's generate a random value
213 current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
215 if [ "$current_set" = 'put your unique phrase here' ]; then
217 set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)"
227 TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP'
231 // database might not exist, so let's try creating it (just to be safe)
233 $stderr = fopen('php://stderr', 'w');
235 list($host, $port) = explode(':', $argv[1], 2);
241 $mysql = new mysqli($host, $argv[2], $argv[3], '', (int)$port);
243 if ($mysql->connect_error) {
245 fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n");
249 if ($maxTries <= 0) {
259 } while ($mysql->connect_error);
261 if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`')) {
263 fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n");