start.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #!/bin/bash
  2. set -eu
  3. readonly WP="/app/code/wp --allow-root --skip-plugins"
  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. admin_password="changeme"
  38. admin_email="admin@cloudron.local"
  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. # TODO: remove this after this release
  87. if [[ "$($WP option get admin_email)" == *.app@* ]]; then
  88. echo "Updating admin email since it was unchanged"
  89. $WP option update admin_email "admin@cloudron.local"
  90. fi
  91. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  92. echo "Configuring smtp mail"
  93. $WP option update mailer smtp
  94. $WP option update mail_from "${MAIL_FROM}"
  95. # Let user customize the mail from name
  96. if ! $WP option get mail_from_name; then
  97. $WP option update mail_from_name WordPress
  98. fi
  99. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  100. $WP option update smtp_port ${MAIL_SMTP_PORT}
  101. $WP option update smtp_auth true
  102. $WP option update smtp_user ${MAIL_SMTP_USERNAME}
  103. $WP option update smtp_pass "${MAIL_SMTP_PASSWORD}"
  104. if [[ -n "${LDAP_SERVER:-}" ]]; then
  105. # configure LDAP
  106. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  107. # GroupEnable means that cloudron groups are carried over to wp groups
  108. # GroupOverUser means that if there is an existing wp group for the user, it won't be overwritten
  109. # The above implies that users can override the roles in wordpress and it
  110. # doesn't get overwritten on re-login
  111. echo "Configuring LDAP"
  112. ldapConfig=$(cat <<EOF
  113. {
  114. "Enabled" : true,
  115. "CachePW" : false,
  116. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  117. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  118. "NameAttr" : "givenName",
  119. "SecName" : "sn",
  120. "UidAttr" : "username",
  121. "MailAttr" : "mail",
  122. "WebAttr" : "",
  123. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  124. "GroupSeparator": ";",
  125. "Debug" : false,
  126. "GroupAttr" : "memberof",
  127. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  128. "DefaultRole" : "editor",
  129. "GroupEnable" : true,
  130. "GroupOverUser" : false,
  131. "Version" : 1
  132. }
  133. EOF
  134. )
  135. $WP --format=json option update authLDAPOptions "${ldapConfig}"
  136. fi
  137. chown -R www-data:www-data /app/data /run/wordpress
  138. echo "Starting apache"
  139. APACHE_CONFDIR="" source /etc/apache2/envvars
  140. rm -f "${APACHE_PID_FILE}"
  141. exec /usr/sbin/apache2 -DFOREGROUND