|
1 | 1 | #!/bin/bash
|
2 | 2 | set -e
|
3 | 3 |
|
4 |
| -if [ -n "$MYSQL_PORT_3306_TCP" ]; then |
5 |
| - if [ -z "$WORDPRESS_DB_HOST" ]; then |
6 |
| - WORDPRESS_DB_HOST='mysql' |
7 |
| - else |
8 |
| - echo >&2 'warning: both WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP found' |
9 |
| - echo >&2 " Connecting to WORDPRESS_DB_HOST ($WORDPRESS_DB_HOST)" |
10 |
| - echo >&2 ' instead of the linked mysql container' |
| 4 | +if [[ "$1" == apache2* ]]; 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 |
11 | 13 | fi
|
12 |
| -fi |
13 |
| - |
14 |
| -if [ -z "$WORDPRESS_DB_HOST" ]; then |
15 |
| - echo >&2 'error: missing WORDPRESS_DB_HOST and MYSQL_PORT_3306_TCP environment variables' |
16 |
| - echo >&2 ' Did you forget to --link some_mysql_container:mysql or set an external db' |
17 |
| - echo >&2 ' with -e WORDPRESS_DB_HOST=hostname:port?' |
18 |
| - exit 1 |
19 |
| -fi |
20 | 14 |
|
21 |
| -# if we're linked to MySQL, and we're using the root user, and our linked |
22 |
| -# container has a default "root" password set up and passed through... :) |
23 |
| -: ${WORDPRESS_DB_USER:=root} |
24 |
| -if [ "$WORDPRESS_DB_USER" = 'root' ]; then |
25 |
| - : ${WORDPRESS_DB_PASSWORD:=$MYSQL_ENV_MYSQL_ROOT_PASSWORD} |
26 |
| -fi |
27 |
| -: ${WORDPRESS_DB_NAME:=wordpress} |
28 |
| - |
29 |
| -if [ -z "$WORDPRESS_DB_PASSWORD" ]; then |
30 |
| - echo >&2 'error: missing required WORDPRESS_DB_PASSWORD environment variable' |
31 |
| - echo >&2 ' Did you forget to -e WORDPRESS_DB_PASSWORD=... ?' |
32 |
| - echo >&2 |
33 |
| - echo >&2 ' (Also of interest might be WORDPRESS_DB_USER and WORDPRESS_DB_NAME.)' |
34 |
| - exit 1 |
35 |
| -fi |
| 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 |
36 | 21 |
|
37 |
| -if ! [ -e index.php -a -e wp-includes/version.php ]; then |
38 |
| - echo >&2 "WordPress not found in $(pwd) - copying now..." |
39 |
| - if [ "$(ls -A)" ]; then |
40 |
| - echo >&2 "WARNING: $(pwd) is not empty - press Ctrl+C now if this is an error!" |
41 |
| - ( set -x; ls -A; sleep 10 ) |
| 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} |
42 | 27 | fi
|
43 |
| - tar cf - --one-file-system -C /usr/src/wordpress . | tar xf - |
44 |
| - echo >&2 "Complete! WordPress has been successfully copied to $(pwd)" |
45 |
| - if [ ! -e .htaccess ]; then |
46 |
| - # NOTE: The "Indexes" option is disabled in the php:apache base image |
47 |
| - cat > .htaccess <<-'EOF' |
48 |
| - # BEGIN WordPress |
49 |
| - <IfModule mod_rewrite.c> |
50 |
| - RewriteEngine On |
51 |
| - RewriteBase / |
52 |
| - RewriteRule ^index\.php$ - [L] |
53 |
| - RewriteCond %{REQUEST_FILENAME} !-f |
54 |
| - RewriteCond %{REQUEST_FILENAME} !-d |
55 |
| - RewriteRule . /index.php [L] |
56 |
| - </IfModule> |
57 |
| - # END WordPress |
58 |
| - EOF |
59 |
| - chown www-data:www-data .htaccess |
| 28 | + : ${WORDPRESS_DB_NAME:=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 |
60 | 36 | fi
|
61 |
| -fi |
62 | 37 |
|
63 |
| -# 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 |
| 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 - --one-file-system -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 |
64 | 63 |
|
65 |
| -if [ ! -e wp-config.php ]; then |
66 |
| - awk '/^\/\*.*stop editing.*\*\/$/ && c == 0 { c = 1; system("cat") } { print }' wp-config-sample.php > wp-config.php <<'EOPHP' |
| 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 | + 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' |
67 | 68 | // If we're behind a proxy server and using HTTPS, we need to alert Wordpress of that fact
|
68 | 69 | // see also http://codex.wordpress.org/Administration_Over_SSL#Using_a_Reverse_Proxy
|
69 | 70 | if (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
|
70 | 71 | $_SERVER['HTTPS'] = 'on';
|
71 | 72 | }
|
72 | 73 |
|
73 | 74 | EOPHP
|
74 |
| - chown www-data:www-data wp-config.php |
75 |
| -fi |
| 75 | + chown www-data:www-data wp-config.php |
| 76 | + fi |
76 | 77 |
|
77 |
| -set_config() { |
78 |
| - key="$1" |
79 |
| - value="$2" |
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 |
83 |
| -} |
| 78 | + set_config() { |
| 79 | + key="$1" |
| 80 | + value="$2" |
| 81 | + php_escaped_value="$(php -r 'var_export($argv[1]);' "$value")" |
| 82 | + sed_escaped_value="$(echo "$php_escaped_value" | sed 's/[\/&]/\\&/g')" |
| 83 | + sed -ri "s/((['\"])$key\2\s*,\s*)(['\"]).*\3/\1$sed_escaped_value/" wp-config.php |
| 84 | + } |
84 | 85 |
|
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" |
89 |
| - |
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") |
92 |
| -UNIQUES=( |
93 |
| - AUTH_KEY |
94 |
| - SECURE_AUTH_KEY |
95 |
| - LOGGED_IN_KEY |
96 |
| - NONCE_KEY |
97 |
| - AUTH_SALT |
98 |
| - SECURE_AUTH_SALT |
99 |
| - LOGGED_IN_SALT |
100 |
| - NONCE_SALT |
101 |
| -) |
102 |
| -for unique in "${UNIQUES[@]}"; do |
103 |
| - eval unique_value=\$WORDPRESS_$unique |
104 |
| - if [ "$unique_value" ]; then |
105 |
| - set_config "$unique" "$unique_value" |
106 |
| - else |
107 |
| - # if not specified, let's generate a random value |
108 |
| - current_set="$(sed -rn "s/define\((([\'\"])$unique\2\s*,\s*)(['\"])(.*)\3\);/\4/p" wp-config.php)" |
109 |
| - if [ "$current_set" = 'put your unique phrase here' ]; then |
110 |
| - set_config "$unique" "$(head -c1M /dev/urandom | sha1sum | cut -d' ' -f1)" |
| 86 | + set_config 'DB_HOST' "$WORDPRESS_DB_HOST" |
| 87 | + set_config 'DB_USER' "$WORDPRESS_DB_USER" |
| 88 | + set_config 'DB_PASSWORD' "$WORDPRESS_DB_PASSWORD" |
| 89 | + set_config 'DB_NAME' "$WORDPRESS_DB_NAME" |
| 90 | + |
| 91 | + # allow any of these "Authentication Unique Keys and Salts." to be specified via |
| 92 | + # environment variables with a "WORDPRESS_" prefix (ie, "WORDPRESS_AUTH_KEY") |
| 93 | + UNIQUES=( |
| 94 | + AUTH_KEY |
| 95 | + SECURE_AUTH_KEY |
| 96 | + LOGGED_IN_KEY |
| 97 | + NONCE_KEY |
| 98 | + AUTH_SALT |
| 99 | + SECURE_AUTH_SALT |
| 100 | + LOGGED_IN_SALT |
| 101 | + NONCE_SALT |
| 102 | + ) |
| 103 | + for unique in "${UNIQUES[@]}"; do |
| 104 | + eval unique_value=\$WORDPRESS_$unique |
| 105 | + if [ "$unique_value" ]; then |
| 106 | + set_config "$unique" "$unique_value" |
| 107 | + else |
| 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)" |
| 112 | + fi |
111 | 113 | fi
|
112 |
| - fi |
113 |
| -done |
| 114 | + done |
114 | 115 |
|
115 |
| -TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP' |
| 116 | + TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP' |
116 | 117 | <?php
|
117 | 118 | // database might not exist, so let's try creating it (just to be safe)
|
118 | 119 |
|
@@ -141,5 +142,6 @@ if (!$mysql->query('CREATE DATABASE IF NOT EXISTS `' . $mysql->real_escape_strin
|
141 | 142 |
|
142 | 143 | $mysql->close();
|
143 | 144 | EOPHP
|
| 145 | +fi |
144 | 146 |
|
145 | 147 | exec "$@"
|
0 commit comments