Files

121 lines
4.4 KiB
Bash

#!/bin/sh
GRAY="\e[0;30m"
RED="\e[0;31m"
GREEN="\e[0;32m"
YELLOW="\e[0;33m"
BLUE="\e[0;34m"
PURPLE="\e[0;35m"
CYAN="\e[0;36m"
WHITE="\e[0;37m"
RESET="\e[0m"
# it gets the ENV variables from .env because specified in the docker-compose.yml file
# this is to make the wp_cron job works
# ! it might not be a very strong solution
#
# somehow it didn't work if the local adress is not accessible from inside the wordpress container :
# if : `curl <PROJECT_URL>` don't work, the cron job does not either (why ?)
# to make it work, i mapped the docker network gateway ip to the url (without understanding what i'm doing)
# the default network is named bridge, and it's gateway is 172.17.0.1 : https://docs.docker.com/network/network-tutorial-standalone/
# also might help, even though i didn't use, it helped me find the ip 172.17.0.1 : https://stackoverflow.com/questions/24319662/from-inside-of-a-docker-container-how-do-i-connect-to-the-localhost-of-the-mach?rq=2
echo "172.17.0.1 ${PROJECT_URL}" >> /etc/hosts
# install wordpress with cli : https://make.wordpress.org/cli/handbook/how-to-install/
# commands : https://developer.wordpress.org/cli/commands/
# bug with wp commands : https://github.com/wp-cli/config-command/issues/31
# -> use `php wp-cli.phar` instead of `wp` as command
echo -e ${YELLOW}download wordpress :${RESET}
if ! wp core version --path="${WP_VOLUME_DIR}" 2> /dev/null
then
echo -e ${YELLOW}wp core download...${RESET}
mkdir -p ${WP_VOLUME_DIR}
#wp core download --path="${WP_VOLUME_DIR}" --allow-root
php wp-cli.phar core download --path="${WP_VOLUME_DIR}" --allow-root
else
echo -e ${GREEN}already there !${RESET}
fi
# install wp if not already installed
if ! wp core is-installed --path="${WP_VOLUME_DIR}" 2> /dev/null
then
echo -e ${YELLOW}installing :${RESET}
#wp config create \
echo -e ${YELLOW}wp config create...${RESET}
php wp-cli.phar config create \
--dbhost="${DB_HOST}" \
--dbname="${DB_NAME}" \
--dbuser="${DB_USER}" \
--dbpass="${DB_PSWD}" \
--path="${WP_VOLUME_DIR}" --allow-root
#wp core install \
echo -e ${YELLOW}wp core install...${RESET}
php wp-cli.phar core install \
--url="${WP_URL}" \
--title="${WP_TITLE}" \
--admin_user="${WP_ADMIN}" \
--admin_email="${WP_ADMIN_EMAIL}" \
--admin_password="${WP_ADMIN_PSWD}" \
--skip-email \
--path="${WP_VOLUME_DIR}" --allow-root
echo -e ${YELLOW}chown and chmod...${RESET}
chown -R www-data:www-data /var/www/*
chmod 755 -R /var/www/*
echo -e ${GREEN}done !${RESET}
else
echo -e ${GREEN}wp is installed${RESET}
fi
# installing plugins :
echo -e ${YELLOW}installing plugins..${RESET}
plugins=$(ls ${WP_VOLUME_PLUGINS})
echo $plugins
for dir in $plugins; do
echo -e ${YELLOW}"creating symlink : ln -s ${WP_VOLUME_PLUGINS}/$dir ${WP_VOLUME_DIR}/wp-content/plugins/"${RESET}
ln -s ${WP_VOLUME_PLUGINS}/$dir ${WP_VOLUME_DIR}/wp-content/plugins/
done
# https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/
echo -e ${YELLOW}configuring the error logs${RESET}
# Enable WP_DEBUG mode
php wp-cli.phar config set WP_DEBUG true --raw --path=${WP_VOLUME_DIR}
# Enable Debug logging to a file (if set to true, this is the /wp-content/debug.log file)
php wp-cli.phar config set WP_DEBUG_LOG true --raw --path=${WP_VOLUME_DIR}
#php wp-cli.phar config set WP_DEBUG_LOG "${ERRORS_LOGS_VOLUME}/wp_error.log" --path=${WP_VOLUME_DIR}
# Disable display of errors and warnings
php wp-cli.phar config set WP_DEBUG_DISPLAY false --raw --path=${WP_VOLUME_DIR}
# if you don't want to rely on wpcron :
# - because it launches on each visit of the website
# - so it can slow it
# - instead creates a real cron job to call the wpcron file regularly
# launch the server cron job, and disable wpcron
# in the container, the crond deamon is started manually, so it is listed with :
# - ps aux
# - kill <PID> to remove it for example
# -> https://stackoverflow.com/questions/37015624/how-to-run-a-cron-job-inside-a-docker-container
php wp-cli.phar config set DISABLE_WP_CRON false --raw --path=${WP_VOLUME_DIR}
echo "* * * * * cd ${WP_VOLUME_DIR}; php ${WP_VOLUME_DIR}/wp-cron.php > /dev/null 2>&1" >> /etc/crontabs/root
crond -f -l 2 >> /var/log/wpcron.log 2>&1 &
# exec php-fpm7 -FR
# php-fpm7 --help :
# -F, --nodaemonize : force to stay in foreground, and ignore daemonize option from config file
# -R, --allow-to-run-as-root : Allow pool to run as root (disabled by default)
exec "${PHP_VERSION}" -FR