]> git.armaanb.net Git - dockerfiles.git/blob - wordpress/apache-7.4/docker-entrypoint.sh
Add Wordpress reccomended PHP modules
[dockerfiles.git] / wordpress / apache-7.4 / docker-entrypoint.sh
1 #!/bin/bash
2
3 set -e
4
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 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}
27 fi
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
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
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'
50                         # BEGIN WordPress
51                         <IfModule mod_rewrite.c>
52                         RewriteEngine On
53                         RewriteBase /
54                         RewriteRule ^index\.php$ - [L]
55                         RewriteCond %{REQUEST_FILENAME} !-f
56                         RewriteCond %{REQUEST_FILENAME} !-d
57                         RewriteRule . /index.php [L]
58                         </IfModule>
59                         # END WordPress
60                 EOF
61                 chown www-data:www-data .htaccess
62         fi
63 fi
64
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';
72 }
73 EOPHP
74         chown www-data:www-data wp-config.php
75 fi
76
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 }
84
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
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
113         fi
114 done
115
116 TERM=dumb php -- "$WORDPRESS_DB_HOST" "$WORDPRESS_DB_USER" "$WORDPRESS_DB_PASSWORD" "$WORDPRESS_DB_NAME" <<'EOPHP'
117 <?php
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);
121 $maxTries = 10;
122 do {
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");
126                 --$maxTries;
127                 if ($maxTries <= 0) {
128                         exit(1);
129                 }
130                 sleep(3);
131         }
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");
135         $mysql->close();
136         exit(1);
137 }
138 $mysql->close();
139 EOPHP
140
141 exec "$@"