start.sh 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #!/bin/bash
  2. set -eu
  3. readonly WP="/app/code/wp --allow-root"
  4. # Detect the wordpress prefix from existing database
  5. table_prefix=$(mysql --user=${MYSQL_USERNAME} --password=${MYSQL_PASSWORD} --host=${MYSQL_HOST} ${MYSQL_DATABASE} -e 'SHOW TABLES' --batch 2>/dev/null | sed -n 's/\(.*_\)usermeta/\1/p')
  6. [[ -n "${table_prefix}" ]] || table_prefix="wp_"
  7. echo "Using table prefix ${table_prefix}"
  8. # Settings to be updated on every run. Regenerating salts means users have to relogin
  9. sed -e "s/##MYSQL_DATABASE/${MYSQL_DATABASE}/" \
  10. -e "s/##MYSQL_USERNAME/${MYSQL_USERNAME}/" \
  11. -e "s/##MYSQL_PASSWORD/${MYSQL_PASSWORD}/" \
  12. -e "s/##MYSQL_HOST/${MYSQL_HOST}:${MYSQL_PORT}/" \
  13. -e "s,##APP_ORIGIN,${APP_ORIGIN}," \
  14. -e "s/##AUTH_KEY/$(pwgen -1cns 64)/" \
  15. -e "s/##SECURE_AUTH_KEY/$(pwgen -1cns 64)/" \
  16. -e "s/##LOGGED_IN_KEY/$(pwgen -1cns 64)/" \
  17. -e "s/##NONCE_KEY/$(pwgen -1cns 64)/" \
  18. -e "s/##AUTH_SALT/$(pwgen -1cns 64)/" \
  19. -e "s/##SECURE_AUTH_SALT/$(pwgen -1cns 64)/" \
  20. -e "s/##LOGGED_IN_SALT/$(pwgen -1cns 64)/" \
  21. -e "s/##NONCE_SALT/$(pwgen -1cns 64)/" \
  22. -e "s/##TABLE_PREFIX/${table_prefix}/" \
  23. /app/code/wp-config.php.template > /run/wordpress/wp-config.php # sed -i seems to destroy symlink
  24. # Used for wp rewrite
  25. touch /app/data/htaccess
  26. if [[ ! -f "/app/data/.dbsetup" ]]; then
  27. echo "Copying wp-content files on first run"
  28. mkdir -p /app/data/wp-content/mu-plugins
  29. cp -r /app/code/wp-content-vanilla/* /app/data/wp-content/
  30. if [[ -n "${LDAP_SERVER:-}" ]]; then
  31. admin_password=$(pwgen -1y 16)
  32. admin_email=${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  33. else
  34. admin_password="changeme"
  35. admin_email=${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  36. fi
  37. echo "Admin password is ${admin_password} and email is ${admin_email}"
  38. # --skip-email is part of 0.23.0 https://github.com/wp-cli/wp-cli/pull/2345 and https://github.com/wp-cli/wp-cli/issues/1164
  39. $WP --url="${APP_ORIGIN}" --skip-email core install \
  40. --url="${APP_ORIGIN}" \
  41. --title="My blog" \
  42. --admin_user=admin \
  43. --admin_password="${admin_password}" \
  44. --admin_email="${admin_email}"
  45. echo "WP is now installed"
  46. # Set default post structure to what most people want
  47. # Curiously, installing some plugins prevents .htaccess getting written
  48. $WP rewrite structure --hard '/%postname%/'
  49. touch "/app/data/.dbsetup"
  50. else
  51. # Update wordpress
  52. echo "Updating wordpress database"
  53. $WP core update-db
  54. fi
  55. # install and backup the plugins. mu plugins are a "flat" structure
  56. # sadly mu-plugins can still be re-configured, just not uninstallable
  57. # We have to do this on every run to get plugin updates
  58. if [[ ! -f "/run/wordpress/plugins_unpacked" ]]; then
  59. echo "Unpacking plugins"
  60. # clear the directory, otherwise unzip/mv have to be forced
  61. rm -rf /app/data/wp-content/mu-plugins/*
  62. rm -f /app/data/wp-content/mu-plugins/disable-updates.php # remove the old plugin we used
  63. unzip -d /app/data/wp-content/mu-plugins/ /app/code/disable-wordpress-core-update.zip
  64. mv /app/data/wp-content/mu-plugins/disable-wordpress-core-update/* /app/data/wp-content/mu-plugins/
  65. rm -rf /app/data/wp-content/mu-plugins/disable-wordpress-core-update/
  66. unzip -d /app/data/wp-content/mu-plugins/ /app/code/wp-mail-smtp.zip
  67. mv /app/data/wp-content/mu-plugins/wp-mail-smtp/* /app/data/wp-content/mu-plugins/
  68. rm -rf /app/data/wp-content/mu-plugins/wp-mail-smtp/
  69. # only install ldap plugin with sso
  70. if [[ -n "${LDAP_SERVER:-}" ]]; then
  71. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  72. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  73. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  74. fi
  75. touch /run/wordpress/plugins_unpacked
  76. else
  77. echo "Plugins already unpacked from previous run" # restarts
  78. fi
  79. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  80. echo "Configuring smtp mail"
  81. $WP option update mailer smtp
  82. $WP option update mail_from "${MAIL_FROM}"
  83. $WP option update mail_from_name WordPress
  84. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  85. $WP option update smtp_port ${MAIL_SMTP_PORT}
  86. $WP option update smtp_auth true
  87. $WP option update smtp_user ${MAIL_SMTP_USERNAME}
  88. $WP option update smtp_pass "${MAIL_SMTP_PASSWORD}"
  89. if [[ -n "${LDAP_SERVER:-}" ]]; then
  90. # configure LDAP
  91. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  92. # GroupEnable means that cloudron groups are carried over to wp groups
  93. # GroupOverUser means that if there is an existing wp group for the user, it won't be overwritten
  94. # The above implies that users can override the roles in wordpress and it
  95. # doesn't get overwritten on re-login
  96. echo "Configuring LDAP"
  97. ldapConfig=$(cat <<EOF
  98. {
  99. "Enabled" : true,
  100. "CachePW" : false,
  101. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  102. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  103. "NameAttr" : "givenName",
  104. "SecName" : "sn",
  105. "UidAttr" : "username",
  106. "MailAttr" : "mail",
  107. "WebAttr" : "",
  108. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  109. "GroupSeparator": ";",
  110. "Debug" : false,
  111. "GroupAttr" : "memberof",
  112. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  113. "DefaultRole" : "editor",
  114. "GroupEnable" : true,
  115. "GroupOverUser" : false,
  116. "Version" : 1
  117. }
  118. EOF
  119. )
  120. $WP --format=json option update authLDAPOptions "${ldapConfig}"
  121. fi
  122. chown -R www-data:www-data /app/data /run/wordpress
  123. echo "Starting apache"
  124. APACHE_CONFDIR="" source /etc/apache2/envvars
  125. rm -f "${APACHE_PID_FILE}"
  126. exec /usr/sbin/apache2 -DFOREGROUND