start.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/bin/bash
  2. set -eux
  3. readonly WP="/app/code/wp --allow-root"
  4. readonly admin_password=$(pwgen -1y 16)
  5. readonly admin_email=${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  6. echo "Admin password is ${admin_password} and email is ${admin_email}"
  7. if [[ -z "$(ls -A /app/data)" ]]; then
  8. echo "Copying wp-content files on first run"
  9. mkdir -p /app/data/wp-content/mu-plugins
  10. cp -r /app/code/wp-content-vanilla/* /app/data/wp-content/
  11. # this also generates the salt in wp-config.php which must be backed up
  12. $WP core config --dbname="${MYSQL_DATABASE}" --dbuser="${MYSQL_USERNAME}" --dbpass="${MYSQL_PASSWORD}" --dbhost="${MYSQL_HOST}" --extra-php <<EOF
  13. // prevent user from changing the Settings->General, WordPress and Blog address values.
  14. define('WP_HOME', 'https://$(hostname -f)');
  15. define('WP_SITEURL', 'https://$(hostname -f)');
  16. /*
  17. http://cmanios.wordpress.com/2014/04/12/nginx-https-reverse-proxy-to-wordpress-with-apache-http-and-different-port/
  18. http://wordpress.org/support/topic/compatibility-with-wordpress-behind-a-reverse-proxy
  19. https://wordpress.org/support/topic/wp_home-and-wp_siteurl
  20. */
  21. // If WordPress is behind reverse proxy which proxies https to http
  22. if (!empty(\$_SERVER['HTTP_X_FORWARDED_FOR'])) {
  23. \$_SERVER['HTTP_HOST'] = \$_SERVER['HTTP_X_FORWARDED_HOST'];
  24. if (\$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
  25. \$_SERVER['HTTPS']='on';
  26. }
  27. EOF
  28. $WP --url="https://$(hostname -f)" core install \
  29. --url="https://$(hostname -f)" \
  30. --title="My blog" \
  31. --admin_user=admin \
  32. --admin_password="${admin_password}" \
  33. --admin_email="${admin_email}"
  34. $WP plugin install --activate --force /app/code/disable-wordpress-updates.zip
  35. mv /app/data/wp-content/plugins/disable-wordpress-updates /app/data/wp-content/mu-plugins/
  36. $WP plugin install --activate --force /app/code/wp-mail-smtp.zip
  37. mv /app/data/wp-content/plugins/wp-mail-smtp /app/data/wp-content/mu-plugins/
  38. else
  39. rm -rf /app/code/wp-content # upgrades & updates - starting out with existing data
  40. ln -sf /app/data/wp-content /app/code/wp-content
  41. fi
  42. # Settings to be updated on every run
  43. sed -e "s/define('DB_NAME',.*/define('DB_NAME', '${MYSQL_DATABASE}');/" \
  44. -e "s/define('DB_USER',.*/define('DB_USER', '${MYSQL_USERNAME}');/" \
  45. -e "s/define('DB_PASSWORD',.*/define('DB_PASSWORD', '${MYSQL_PASSWORD}');/" \
  46. -e "s/define('DB_HOST',.*/define('DB_HOST', '${MYSQL_HOST}');/" \
  47. -e "s|define('WP_HOME',.*|define('WP_HOME', 'https://$(hostname -f)');|" \
  48. -e "s|define('WP_SITEURL',.*|define('WP_SITEURL', 'https://$(hostname -f)');|" \
  49. -i /run/wordpress/wp-config.php # sed -i seems to destroy symlink
  50. # reset the admin password
  51. $WP user update $($WP user get admin --field=ID) --user_pass="${admin_password}"
  52. $WP user update $($WP user get admin --field=ID) --user_email="${admin_email}"
  53. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  54. $WP option update mailer smtp
  55. $WP option update mail_from ${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  56. $WP option update mail_from_name ${MAIL_SMTP_USERNAME}
  57. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  58. $WP option update smtp_port ${MAIL_SMTP_PORT}
  59. $WP option update smtp_auth false
  60. chown -R www-data:www-data /app/data
  61. echo "Starting apache"
  62. APACHE_CONFDIR="" source /etc/apache2/envvars
  63. rm -f "${APACHE_PID_FILE}"
  64. exec /usr/sbin/apache2 -DFOREGROUND