5 if [ -n "$MYSQL_PORT_3306_TCP" ]; then
6 if [ -z "$WORDPRESS_DB_HOST" ]; then
7 WORDPRESS_DB_HOST='mysql'
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'
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?'
22 # if we're linked to MySQL, and we're using the root user, and our linked
23 # container has a default "root" password set up and passed through... :)
24 : ${WORDPRESS_DB_USER:=root}
25 if [ "$WORDPRESS_DB_USER" = 'root' ]; then
26 : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD}
28 : ${WORDPRESS_DB_NAME:=wordpress}
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=... ?'
34 echo >&2 ' (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)'
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 )
45 tar cf - --one-file-system -C /usr/src/wordpress . | tar xf -
46 echo >&2 "Complete! WordPress has been successfully copied to $(pwd)"
47 if [ ! -e .htaccess ]; then
48 # NOTE: The "Indexes" option is disabled in the php:apache base image
49 cat > .htaccess <<-'EOF'
51 <IfModule mod_rewrite.c>
54 RewriteRule ^index\.php$ - [L]
55 RewriteCond %{REQUEST_FILENAME} !-f
56 RewriteCond %{REQUEST_FILENAME} !-d
57 RewriteRule . /index.php [L]
61 chown www-data:www-data .htaccess
65 # 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
66 if [ ! -e wp-config.php ]; then
67 awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP'
68 // If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact
69 // see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
70 if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
71 $_SERVER['HTTPS'] = 'on';
74 chown www-data:www-data wp-config.php
80 php_escaped_value="$(php -r 'var_export($argv[1]);' "$value")"
81 sed_escaped_value="$(echo "$php_escaped_value" | sed 's/[\/&]/\\&/g')"
82 sed -ri "s/((['\"])$key\2\s*,\s*)(['\"]).*\3/\1$sed_escaped_value/" wp-config.php
85 set_config 'DB_HOST' "$WORDPRESS_DB_HOST"
86 set_config 'DB_USER' "$WORDPRESS_DB_USER"
87 set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD"
88 set_config 'DB_NAME' "$WORDPRESS_DB_NAME"
90 # allow any of these "Authentication Unique Keys and Salts." to be specified via
91 # environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY")
103 for unique in "${UNIQUES[@]}"; do
104 eval unique_value=\$WORDPRESS_$unique
105 if [ "$unique_value" ]; then
106 set_config "$unique" "$unique_value"
108 # if not specified, let's generate a random value
109 current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)"
110 if [ "$current_set" = 'put your unique phrase here' ]; then
111 set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)"
116 TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP'
118 // database might not exist, so let's try creating it (just to be safe)
119 $stderr = fopen('php://stderr', 'w');
120 list($host, $port) = explode(':', $argv[1], 2);
123 $mysql = new mysqli($host, $argv[2], $argv[3], '', (int)$port);
124 if ($mysql->connect_error) {
125 fwrite($stderr, "\n" . 'MySQL Connection Error: (' . $mysql->connect_errno . ') ' . $mysql->connect_error . "\n");
127 if ($maxTries <= 0) {
132 } while ($mysql->connect_error);
133 if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_string($argv[4]) . '`')) {
134 fwrite($stderr, "\n" . 'MySQL "CREATE DATABASE" Error: ' . $mysql->error . "\n");