start.sh 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. # Generate php.ini
  25. memory_limit=$(cat /sys/fs/cgroup/memory/memory.limit_in_bytes) # this is the RAM. we have equal amount of swap
  26. php_memory_limit_mb=$((memory_limit*2/1024/1024)) # we have 5 apache workers
  27. echo "Setting PHP memory limit to ${php_memory_limit_mb}M"
  28. cp /etc/php/7.0/apache2/php.ini.orig /run/php.ini
  29. crudini --set /run/php.ini PHP memory_limit "${php_memory_limit_mb}M"
  30. # Used for wp rewrite
  31. touch /app/data/htaccess
  32. if [[ ! -f "/app/data/.dbsetup" ]]; then
  33. echo "Copying wp-content files on first run"
  34. mkdir -p /app/data/wp-content/mu-plugins
  35. cp -r /app/code/wp-content-vanilla/* /app/data/wp-content/
  36. if [[ -n "${LDAP_SERVER:-}" ]]; then
  37. admin_password=$(pwgen -1y 16)
  38. admin_email=${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  39. else
  40. admin_password="changeme"
  41. admin_email=${MAIL_SMTP_USERNAME}@${MAIL_DOMAIN}
  42. fi
  43. echo "Admin password is ${admin_password} and email is ${admin_email}"
  44. # --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
  45. $WP --url="${APP_ORIGIN}" --skip-email core install \
  46. --url="${APP_ORIGIN}" \
  47. --title="My blog" \
  48. --admin_user=admin \
  49. --admin_password="${admin_password}" \
  50. --admin_email="${admin_email}"
  51. echo "WP is now installed"
  52. # Set default post structure to what most people want
  53. # Curiously, installing some plugins prevents .htaccess getting written
  54. $WP rewrite structure --hard '/%postname%/'
  55. touch "/app/data/.dbsetup"
  56. else
  57. # Update wordpress
  58. echo "Updating wordpress database"
  59. $WP core update-db
  60. fi
  61. # install and backup the plugins. mu plugins are a "flat" structure
  62. # sadly mu-plugins can still be re-configured, just not uninstallable
  63. # We have to do this on every run to get plugin updates
  64. if [[ ! -f "/run/wordpress/plugins_unpacked" ]]; then
  65. echo "Unpacking plugins"
  66. # clear the directory, otherwise unzip/mv have to be forced
  67. rm -rf /app/data/wp-content/mu-plugins/*
  68. rm -f /app/data/wp-content/mu-plugins/disable-updates.php # remove the old plugin we used
  69. unzip -d /app/data/wp-content/mu-plugins/ /app/code/disable-wordpress-core-update.zip
  70. mv /app/data/wp-content/mu-plugins/disable-wordpress-core-update/* /app/data/wp-content/mu-plugins/
  71. rm -rf /app/data/wp-content/mu-plugins/disable-wordpress-core-update/
  72. unzip -d /app/data/wp-content/mu-plugins/ /app/code/wp-mail-smtp.zip
  73. mv /app/data/wp-content/mu-plugins/wp-mail-smtp/* /app/data/wp-content/mu-plugins/
  74. rm -rf /app/data/wp-content/mu-plugins/wp-mail-smtp/
  75. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  76. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  77. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  78. touch /run/wordpress/plugins_unpacked
  79. else
  80. echo "Plugins already unpacked from previous run" # restarts
  81. fi
  82. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  83. echo "Configuring smtp mail"
  84. $WP option update mailer smtp
  85. $WP option update mail_from "${MAIL_FROM}"
  86. $WP option update mail_from_name WordPress
  87. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  88. $WP option update smtp_port ${MAIL_SMTP_PORT}
  89. $WP option update smtp_auth true
  90. $WP option update smtp_user ${MAIL_SMTP_USERNAME}
  91. $WP option update smtp_pass "${MAIL_SMTP_PASSWORD}"
  92. if [[ -n "${LDAP_SERVER:-}" ]]; then
  93. # configure LDAP
  94. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  95. # GroupEnable means that cloudron groups are carried over to wp groups
  96. # GroupOverUser means that if there is an existing wp group for the user, it won't be overwritten
  97. # The above implies that users can override the roles in wordpress and it
  98. # doesn't get overwritten on re-login
  99. echo "Configuring LDAP"
  100. ldapConfig=$(cat <<EOF
  101. {
  102. "Enabled" : true,
  103. "CachePW" : false,
  104. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  105. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  106. "NameAttr" : "givenName",
  107. "SecName" : "sn",
  108. "UidAttr" : "username",
  109. "MailAttr" : "mail",
  110. "WebAttr" : "",
  111. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  112. "GroupSeparator": ";",
  113. "Debug" : false,
  114. "GroupAttr" : "memberof",
  115. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  116. "DefaultRole" : "editor",
  117. "GroupEnable" : true,
  118. "GroupOverUser" : false,
  119. "Version" : 1
  120. }
  121. EOF
  122. )
  123. $WP --format=json option update authLDAPOptions "${ldapConfig}"
  124. fi
  125. chown -R www-data:www-data /app/data /run/wordpress
  126. echo "Starting apache"
  127. APACHE_CONFDIR="" source /etc/apache2/envvars
  128. rm -f "${APACHE_PID_FILE}"
  129. exec /usr/sbin/apache2 -DFOREGROUND