start.sh 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/bash
  2. set -eux
  3. readonly WP="/app/code/wp --allow-root"
  4. readonly admin_password=$(pwgen -1)
  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. mv /app/code/wp-content /app/data/wp-content/
  10. rm -rf /app/code/wp-content
  11. ln -sf /app/data/wp-content /app/code/wp-content
  12. # this also generates the salt in wp-config.php which must be backed up
  13. $WP core config --dbname="${MYSQL_DATABASE}" --dbuser="${MYSQL_USERNAME}" --dbpass="${MYSQL_PASSWORD}" --dbhost="${MYSQL_HOST}" --extra-php <<EOF
  14. // prevent user from changing the Settings->General, WordPress and Blog address values.
  15. define('WP_HOME', 'https://$(hostname -f)');
  16. define('WP_SITEURL', 'https://$(hostname -f)');
  17. /*
  18. http://cmanios.wordpress.com/2014/04/12/nginx-https-reverse-proxy-to-wordpress-with-apache-http-and-different-port/
  19. http://wordpress.org/support/topic/compatibility-with-wordpress-behind-a-reverse-proxy
  20. https://wordpress.org/support/topic/wp_home-and-wp_siteurl
  21. */
  22. // If WordPress is behind reverse proxy which proxies https to http
  23. if (!empty(\$_SERVER['HTTP_X_FORWARDED_FOR'])) {
  24. \$_SERVER['HTTP_HOST'] = \$_SERVER['HTTP_X_FORWARDED_HOST'];
  25. if (\$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')
  26. \$_SERVER['HTTPS']='on';
  27. }
  28. EOF
  29. $WP --url="https://$(hostname -f)" core install \
  30. --url="https://$(hostname -f)" \
  31. --title="My blog" \
  32. --admin_user=admin \
  33. --admin_password="${admin_password}" \
  34. --admin_email="${admin_email}"
  35. echo "Installing OAuth plugin"
  36. $WP plugin install --activate --force /app/code/wp-oauth.zip
  37. $WP plugin install --activate --force /app/code/disable-wordpress-updates.zip
  38. $WP plugin install --activate --force /app/code/wp-mail-smtp.zip
  39. $WP option update users_can_register 1 # without this, nothing works
  40. $WP option update wpoa_cloudron_api_enabled 1
  41. $WP option update wpoa_new_user_role administrator # TODO: let the plugin determine this from the oauth profile
  42. # $WP option update wpoa_hide_wordpress_login_form 1 # disabling this will make it impossible for users (commenters) to login
  43. $WP option update wpoa_suppress_welcome_email 1
  44. else
  45. rm -rf /app/code/wp-content # upgrades & updates - starting out with existing data
  46. ln -sf /app/data/wp-content /app/code/wp-content
  47. fi
  48. # Settings to be updated on every run
  49. sed -e "s/define('DB_NAME',.*/define('DB_NAME', '${MYSQL_DATABASE}');/" \
  50. -e "s/define('DB_USER',.*/define('DB_USER', '${MYSQL_USERNAME}');/" \
  51. -e "s/define('DB_PASSWORD',.*/define('DB_PASSWORD', '${MYSQL_PASSWORD}');/" \
  52. -e "s/define('DB_HOST',.*/define('DB_HOST', '${MYSQL_HOST}');/" \
  53. -e "s|define('WP_HOME',.*|define('WP_HOME', 'https://$(hostname -f)');|" \
  54. -e "s|define('WP_SITEURL',.*|define('WP_SITEURL', 'https://$(hostname -f)');|" \
  55. -i /app/data/wp-config.php # sed -i seems to destroy symlink
  56. # reset the admin password
  57. $WP user update $($WP user get admin --field=ID) --user_pass="${admin_password}"
  58. $WP user update $($WP user get admin --field=ID) --user_email="${admin_email}"
  59. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  60. $WP option update mailer smtp
  61. $WP option update mail_from ${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  62. $WP option update mail_from_name ${MAIL_SMTP_USERNAME}
  63. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  64. $WP option update smtp_port ${MAIL_SMTP_PORT}
  65. $WP option update smtp_auth false
  66. $WP option update wpoa_cloudron_api_id "${OAUTH_CLIENT_ID}"
  67. $WP option update wpoa_cloudron_api_secret "${OAUTH_CLIENT_SECRET}"
  68. chown -R www-data:www-data /app/code /app/data
  69. /usr/bin/supervisord --configuration /etc/supervisor/supervisord.conf --nodaemon -i WordPress