start.sh 4.76 KB
Newer Older
Johannes Zellner's avatar
Johannes Zellner committed
1 2 3 4
#!/bin/bash

set -eu

5
wp='sudo -u www-data -i -- /app/pkg/wp --path=/app/data/public/ --skip-themes --skip-plugins'
6

7 8
mkdir -p /app/data /run/apache2 /run/sessions

9
if [[ ! -f "/app/data/php.ini" ]]; then
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
10
    cp /etc/php/7.3/apache2/php.ini.orig /app/data/php.ini
11 12 13 14
    crudini --set /app/data/php.ini Session session.gc_probability 1
    crudini --set /app/data/php.ini Session session.gc_divisor 100
fi

15
if [[ ! -d "/app/data/public" ]]; then
16 17 18 19
    cp -rf /app/code/wordpress /app/data/public
    chown -R www-data:www-data /app/data

    echo "=> Create initial WordPress config"
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
20
    $wp config create --dbname="${CLOUDRON_MYSQL_DATABASE}" --dbuser="${CLOUDRON_MYSQL_USERNAME}" --dbpass="${CLOUDRON_MYSQL_PASSWORD}" --dbhost="${CLOUDRON_MYSQL_HOST}:${CLOUDRON_MYSQL_PORT}"
21 22 23

    echo "=> Install WordPress"
    # --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
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
24
    $wp core install --skip-email --url="${CLOUDRON_APP_ORIGIN}" --title="My website" --admin_user="admin" --admin_password="changeme" --admin_email="${CLOUDRON_MAIL_FROM}"
25

26
    echo "=> Install smtp mail plugin"
27 28
    $wp plugin install /app/pkg/wp-mail-smtp.zip
    $wp plugin activate wp-mail-smtp
29

Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
30 31 32 33 34 35 36 37 38 39 40
    echo "==> Install redis plugin"
    $wp plugin install /app/pkg/wp-redis-cache.zip
    $wp plugin activate redis-cache

    $wp config set WP_REDIS_HOST "${CLOUDRON_REDIS_HOST}"
    $wp config set WP_REDIS_PORT "${CLOUDRON_REDIS_PORT}"
    $wp config set WP_REDIS_PASSWORD "${CLOUDRON_REDIS_PASSWORD}"

    # this copies over the object-cache.php file into wp core
    sudo -u www-data -i -- /app/pkg/wp --path=/app/data/public/ --skip-themes redis enable

41
    chown -R www-data:www-data /app/data
42 43
fi

44 45 46
echo "=> Changing permissions"
chown -R www-data:www-data /app/data /run

47 48 49 50 51 52 53 54 55 56 57 58
# Update db settings first. otherwise, the domain/mail changes will overwrite the original db when cloning
echo "=> Updating db settings"
$wp config set DB_HOST "${CLOUDRON_MYSQL_HOST}:${CLOUDRON_MYSQL_PORT}"
$wp config set DB_NAME "${CLOUDRON_MYSQL_DATABASE}"
$wp config set DB_USER "${CLOUDRON_MYSQL_USERNAME}"
$wp config set DB_PASSWORD "${CLOUDRON_MYSQL_PASSWORD}"

echo "=> Updating redis settings"
$wp config set WP_REDIS_HOST "${CLOUDRON_REDIS_HOST}"
$wp config set WP_REDIS_PORT "${CLOUDRON_REDIS_PORT}"
$wp config set WP_REDIS_PASSWORD "${CLOUDRON_REDIS_PASSWORD}"

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
# configure WP mail smtp plugin
echo "==> Configuring smtp mail"
if mail_config=$($wp --format=json option get wp_mail_smtp); then
    mail_from_name=$(echo "${mail_config}" | jq -r .mail.from_name)
else
    mail_from_name="WordPress"
fi

mailConfig=$(cat <<EOF
{
    "mail": {
        "from_email"        : "${CLOUDRON_MAIL_FROM}",
        "from_name"         : "${mail_from_name}",
        "mailer"            : "smtp",
        "return_path"       : "from_email_force",
        "from_name_force"   : true,
        "from_email_force"  : true
    },
    "smtp": {
        "autotls"           : true,
        "host"              : "${CLOUDRON_MAIL_SMTP_SERVER}",
        "encryption"        : "none",
        "port"              : ${CLOUDRON_MAIL_SMTP_PORT},
        "auth"              : true,
        "user"              : "${CLOUDRON_MAIL_SMTP_USERNAME}",
        "pass"              : "${CLOUDRON_MAIL_SMTP_PASSWORD}"
    }
}
EOF
)

90
# note that is-installed only works if db settings are up to date
91
if $wp core is-installed; then
92
    echo "=> Updating domain related settings"
93 94
    $wp option update siteurl "${CLOUDRON_APP_ORIGIN}"
    $wp option update home "${CLOUDRON_APP_ORIGIN}"
95

96
    echo "=> Updating mailer settings"
97
    $wp --format=json option update wp_mail_smtp "${mailConfig}"
98 99 100
else
    echo "=> WordPress does not seem to be working or is not installed"
fi
101

Johannes Zellner's avatar
Johannes Zellner committed
102
echo "=> Creating credentials.txt"
103
sudo -u www-data -- sed -e "s,MYSQL_HOST,${CLOUDRON_MYSQL_HOST}," \
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
104 105 106 107 108 109 110 111 112 113 114 115
    -e "s,MYSQL_PORT,${CLOUDRON_MYSQL_PORT}," \
    -e "s,MYSQL_USERNAME,${CLOUDRON_MYSQL_USERNAME}," \
    -e "s,MYSQL_PASSWORD,${CLOUDRON_MYSQL_PASSWORD}," \
    -e "s,MYSQL_DATABASE,${CLOUDRON_MYSQL_DATABASE}," \
    -e "s,MYSQL_URL,${CLOUDRON_MYSQL_URL}," \
    -e "s,MAIL_SMTP_SERVER,${CLOUDRON_MAIL_SMTP_SERVER}," \
    -e "s,MAIL_SMTP_PORT,${CLOUDRON_MAIL_SMTP_PORT}," \
    -e "s,MAIL_SMTPS_PORT,${CLOUDRON_MAIL_SMTPS_PORT}," \
    -e "s,MAIL_SMTP_USERNAME,${CLOUDRON_MAIL_SMTP_USERNAME}," \
    -e "s,MAIL_SMTP_PASSWORD,${CLOUDRON_MAIL_SMTP_PASSWORD}," \
    -e "s,MAIL_FROM,${CLOUDRON_MAIL_FROM}," \
    -e "s,MAIL_DOMAIN,${CLOUDRON_MAIL_DOMAIN}," \
Girish Ramakrishnan's avatar
Girish Ramakrishnan committed
116 117 118
    -e "s,REDIS_SERVER,${CLOUDRON_REDIS_HOST}," \
    -e "s,REDIS_PORT,${CLOUDRON_REDIS_PORT}," \
    -e "s,REDIS_PASSWORD,${CLOUDRON_REDIS_PASSWORD}," \
Johannes Zellner's avatar
Johannes Zellner committed
119 120 121 122 123 124
    /app/pkg/credentials.template > /app/data/credentials.txt

echo "=> Starting apache"
APACHE_CONFDIR="" source /etc/apache2/envvars
rm -f "${APACHE_PID_FILE}"
exec /usr/sbin/apache2 -DFOREGROUND