start.sh 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. # only install ldap plugin with sso
  76. if [[ -n "${LDAP_SERVER:-}" ]]; then
  77. unzip -d /app/data/wp-content/mu-plugins/ /app/code/authLdap.zip
  78. mv /app/data/wp-content/mu-plugins/authLdap-*/* /app/data/wp-content/mu-plugins/
  79. rm -rf /app/data/wp-content/mu-plugins/authLdap-*/
  80. fi
  81. touch /run/wordpress/plugins_unpacked
  82. else
  83. echo "Plugins already unpacked from previous run" # restarts
  84. fi
  85. # configure WP mail smtp plugin (smtp_user, smtp_pass can be set when supported)
  86. echo "Configuring smtp mail"
  87. $WP option update mailer smtp
  88. $WP option update mail_from "${MAIL_FROM}"
  89. $WP option update mail_from_name WordPress
  90. $WP option update smtp_host ${MAIL_SMTP_SERVER}
  91. $WP option update smtp_port ${MAIL_SMTP_PORT}
  92. $WP option update smtp_auth true
  93. $WP option update smtp_user ${MAIL_SMTP_USERNAME}
  94. $WP option update smtp_pass "${MAIL_SMTP_PASSWORD}"
  95. if [[ -n "${LDAP_SERVER:-}" ]]; then
  96. # configure LDAP
  97. # https://github.com/heiglandreas/authLdap/blob/master/authLdap.php#L644
  98. # GroupEnable means that cloudron groups are carried over to wp groups
  99. # GroupOverUser means that if there is an existing wp group for the user, it won't be overwritten
  100. # The above implies that users can override the roles in wordpress and it
  101. # doesn't get overwritten on re-login
  102. echo "Configuring LDAP"
  103. ldapConfig=$(cat <<EOF
  104. {
  105. "Enabled" : true,
  106. "CachePW" : false,
  107. "URI" : "ldap://${LDAP_SERVER}:${LDAP_PORT}/${LDAP_USERS_BASE_DN}",
  108. "Filter" : "(|(mail=%1\$s)(username=%1\$s))",
  109. "NameAttr" : "givenName",
  110. "SecName" : "sn",
  111. "UidAttr" : "username",
  112. "MailAttr" : "mail",
  113. "WebAttr" : "",
  114. "Groups" : { "administrator" : "cn=admins,${LDAP_GROUPS_BASE_DN}" },
  115. "GroupSeparator": ";",
  116. "Debug" : false,
  117. "GroupAttr" : "memberof",
  118. "GroupFilter" : "(|(mail=%1\$s)(username=%1\$s))",
  119. "DefaultRole" : "editor",
  120. "GroupEnable" : true,
  121. "GroupOverUser" : false,
  122. "Version" : 1
  123. }
  124. EOF
  125. )
  126. $WP --format=json option update authLDAPOptions "${ldapConfig}"
  127. fi
  128. chown -R www-data:www-data /app/data /run/wordpress
  129. echo "Starting apache"
  130. APACHE_CONFDIR="" source /etc/apache2/envvars
  131. rm -f "${APACHE_PID_FILE}"
  132. exec /usr/sbin/apache2 -DFOREGROUND