start.sh 6.3 KB

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