start.sh 6.9 KB

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