start.sh 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  70. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  71. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  72. touch /run/wordpress/plugins_unpacked
  73. else
  74. echo "Plugins already unpacked from previous run" # restarts
  75. fi
  76. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  77. echo "Configuring smtp mail"
  78. $WP option update mailer smtp
  79. $WP option update mail_from "${MAIL_FROM}"
  80. $WP option update mail_from_name WordPress
  81. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  82. $WP option update smtp_port ${MAIL_SMTP_PORT}
  83. $WP option update smtp_auth true
  84. $WP option update smtp_user ${MAIL_SMTP_USERNAME}
  85. $WP option update smtp_pass "${MAIL_SMTP_PASSWORD}"
  86. if [[ -n "${LDAP_SERVER:-}" ]]; then
  87. # configure LDAP
  88. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  89. echo "Configuring LDAP"
  90. ldapConfig=$(cat <<EOF
  91. {
  92. "Enabled" : true,
  93. "CachePW" : false,
  94. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  95. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  96. "NameAttr" : "givenName",
  97. "SecName" : "sn",
  98. "UidAttr" : "username",
  99. "MailAttr" : "mail",
  100. "WebAttr" : "",
  101. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  102. "GroupSeparator": ";",
  103. "Debug" : false,
  104. "GroupAttr" : "memberof",
  105. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  106. "DefaultRole" : "editor",
  107. "GroupEnable" : true,
  108. "GroupOverUser" : true,
  109. "Version" : 1
  110. }
  111. EOF
  112. )
  113. $WP --format=json option update authLDAPOptions "${ldapConfig}"
  114. fi
  115. chown -R www-data:www-data /app/data /run/wordpress
  116. echo "Starting apache"
  117. APACHE_CONFDIR="" source /etc/apache2/envvars
  118. rm -f "${APACHE_PID_FILE}"
  119. exec /usr/sbin/apache2 -DFOREGROUND