start.sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #!/bin/bash
  2. set -eu
  3. readonly WP="/app/code/wp --allow-root --skip-plugins"
  4. mkdir -p /run/wordpress/sessions /app/data/wp-snapshots
  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. # note: we cannot delete the admin user in ldap mode because the default posts are assigned to that user
  38. admin_password=$([[ -n "${LDAP_SERVER:-}" ]] && pwgen -1y 16 || echo "changeme")
  39. admin_email="admin@cloudron.local"
  40. # --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
  41. $WP --url="${APP_ORIGIN}" --skip-email core install \
  42. --url="${APP_ORIGIN}" \
  43. --title="My blog" \
  44. --admin_user=admin \
  45. --admin_password="${admin_password}" \
  46. --admin_email="${admin_email}"
  47. echo "==> WP is now installed"
  48. # Set default post structure to what most people want
  49. # Curiously, installing some plugins prevents .htaccess getting written
  50. $WP rewrite structure --hard '/%postname%/'
  51. touch "/app/data/.dbsetup"
  52. else
  53. # Update wordpress
  54. echo "==> Updating wordpress database"
  55. $WP core update-db
  56. fi
  57. # install and backup the plugins. mu plugins are a "flat" structure
  58. # sadly mu-plugins can still be re-configured, just not uninstallable
  59. # We have to do this on every run to get plugin updates
  60. if [[ ! -f "/run/wordpress/plugins_unpacked" ]]; then
  61. echo "==> Unpacking plugins"
  62. # clear the directory, otherwise unzip/mv have to be forced
  63. rm -rf /app/data/wp-content/mu-plugins/*
  64. rm -f /app/data/wp-content/mu-plugins/disable-updates.php # remove the old plugin we used
  65. unzip -d /app/data/wp-content/mu-plugins/ /app/code/disable-wordpress-core-update.zip
  66. mv /app/data/wp-content/mu-plugins/disable-wordpress-core-update/* /app/data/wp-content/mu-plugins/
  67. rm -rf /app/data/wp-content/mu-plugins/disable-wordpress-core-update/
  68. unzip -d /app/data/wp-content/mu-plugins/ /app/code/wp-mail-smtp.zip
  69. mv /app/data/wp-content/mu-plugins/wp-mail-smtp/* /app/data/wp-content/mu-plugins/
  70. rm -rf /app/data/wp-content/mu-plugins/wp-mail-smtp/
  71. # only install ldap plugin with sso
  72. if [[ -n "${LDAP_SERVER:-}" ]]; then
  73. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  74. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  75. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  76. fi
  77. touch /run/wordpress/plugins_unpacked
  78. else
  79. echo "==> Plugins already unpacked from previous run" # restarts
  80. fi
  81. echo "==> Updating domain related settings"
  82. # Note that wp-config already sets WP_HOME and WP_SITEURL and the values in db below are ignored
  83. # This is only done for keeping the db dumps more useful
  84. $WP option update siteurl "${APP_ORIGIN}"
  85. $WP option update home "${APP_ORIGIN}"
  86. # If the user has not changed the email, update it to reflect it any domain change
  87. # TODO: remove this after this release
  88. if [[ "$($WP option get admin_email)" == *.app@* ]]; then
  89. echo "==> Updating admin email since it was unchanged"
  90. $WP option update admin_email "admin@cloudron.local"
  91. fi
  92. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  93. echo "==> Configuring smtp mail"
  94. $WP option update mailer smtp
  95. $WP option update mail_from "${MAIL_FROM}"
  96. # Let user customize the mail from name
  97. if ! $WP option get mail_from_name; then
  98. $WP option update mail_from_name WordPress
  99. fi
  100. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  101. $WP option update smtp_port ${MAIL_SMTP_PORT}
  102. $WP option update smtp_auth true
  103. $WP option update smtp_user ${MAIL_SMTP_USERNAME}
  104. $WP option update smtp_pass "${MAIL_SMTP_PASSWORD}"
  105. if [[ -n "${LDAP_SERVER:-}" ]]; then
  106. # configure LDAP
  107. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  108. # GroupEnable means that cloudron groups are carried over to wp groups
  109. # GroupOverUser means that if there is an existing wp group for the user, it won't be overwritten
  110. # The above implies that users can override the roles in wordpress and it
  111. # doesn't get overwritten on re-login
  112. echo "==> Configuring LDAP"
  113. ldapConfig=$(cat <<EOF
  114. {
  115. "Enabled" : true,
  116. "CachePW" : false,
  117. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  118. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  119. "NameAttr" : "givenName",
  120. "SecName" : "sn",
  121. "UidAttr" : "username",
  122. "MailAttr" : "mail",
  123. "WebAttr" : "",
  124. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  125. "GroupSeparator": ";",
  126. "Debug" : false,
  127. "GroupAttr" : "memberof",
  128. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  129. "DefaultRole" : "editor",
  130. "GroupEnable" : true,
  131. "GroupOverUser" : false,
  132. "Version" : 1
  133. }
  134. EOF
  135. )
  136. $WP --format=json option update authLDAPOptions "${ldapConfig}"
  137. fi
  138. chown -R www-data:www-data /app/data /run/wordpress
  139. echo "==> Starting apache"
  140. APACHE_CONFDIR="" source /etc/apache2/envvars
  141. rm -f "${APACHE_PID_FILE}"
  142. exec /usr/sbin/apache2 -DFOREGROUND