wordpress/files/run.sh

107 lines
2.8 KiB
Bash
Raw Normal View History

#!/bin/bash
2022-06-24 14:58:58 -04:00
__url_check() {
curl -q -SIs "http://localhost:80" | grep -qE 'HTTP/[1,2]*' && return 0 || return 1
}
__file_check() {
ls var/run/php-fpm.sock /var/run/mysqld/mysqld.sock /var/run/nginx/nginx.pid &>/dev/null && return 0 || return 1
}
2022-06-24 15:58:13 -04:00
__phpfpm() {
if [ -f "/var/run/php-fpm.sock" ]; then
return 0
else
/usr/bin/php-fpm &
if [[ $? = 0 ]]; then
sleep 10
return 0
else
exit 1
fi
fi
}
2022-06-24 14:58:58 -04:00
__mysqld() {
2022-06-24 15:58:13 -04:00
if ! __mysql_test; then
mysqld_safe --datadir=/var/lib/mysql &
fi
if [[ $? = 0 ]]; then
sleep 5
return 0
else
exit 1
fi
2022-06-24 14:58:58 -04:00
}
__mysql_test() {
2022-06-24 15:58:13 -04:00
server_db="$(mysqladmin --silent --wait=2 ping &>/dev/null && echo 'running')"
2022-06-24 14:58:58 -04:00
[[ "$server_db" = "running" ]] && return 0 || return 1
}
if [ "$1" = "healthcheck" ]; then
2022-06-24 14:58:58 -04:00
__url_check && __file_check && exit 0 || exit 1
fi
if [ ! -d "/usr/html/wp-admin" ] && [ ! -f "/usr/html/wp-config.php" ]; then
echo "[i] Installing wordpress..."
cd /tmp || exit 1
wget https://wordpress.org/latest.tar.gz -O /tmp/latest.tar.gz &&
tar -xzf /tmp/latest.tar.gz &&
cp -Rf /tmp/wordpress/. /usr/html/ &&
rm -Rf /tmp/wordpress /tmp/latest.tar.gz &&
chown -Rf nginx:nginx /usr/html
else
echo "[i] Fixing permissions..."
chown -R nginx:nginx /usr/html
fi
2022-06-24 15:20:47 -04:00
if [ -d "/var/lib/wordpress/devel" ]; then
echo "[i] Initializing plugin development dir"
2022-06-24 15:58:13 -04:00
plugins="$(ls /var/lib/wordpress/devel 2>/dev/null || false)"
2022-06-24 15:34:00 -04:00
[ -d "/usr/html/wp-content/plugins" ] || mkdir -p "/usr/html/wp-content/plugins"
2022-06-24 15:58:13 -04:00
if [ -n "$plugins" ]; then
for d in $plugins; do
ln -sf "/var/lib/wordpress/devel/$d" "/usr/html/wp-content/plugins/$d"
done
fi
2022-06-24 15:20:47 -04:00
fi
2022-06-24 14:38:12 -04:00
if [ ! -d "/var/lib/mysql/mysql" ]; then
2022-06-24 15:20:47 -04:00
echo "[i] Initializing mysql database"
2022-06-24 16:45:06 -04:00
rm -Rf "/var/lib/mysql/" &>/dev/null
2022-06-24 14:38:12 -04:00
/usr/bin/mysql_install_db --user=mysql --datadir=/var/lib/mysql
2022-06-24 14:58:58 -04:00
__mysqld
__mysql_test
2022-06-24 14:38:12 -04:00
mysqladmin -u root password "$DB_PASS"
fi
2022-06-24 15:58:13 -04:00
echo "[i] Creating directories..."
mkdir -p /usr/logs/php8
mkdir -p /usr/logs/nginx
mkdir -p /tmp/nginx
2022-06-24 15:58:13 -04:00
echo "[i] Setting permissions..."
chown -Rf nginx /tmp/nginx
chown -Rf mysql:mysql /var/lib/mysql /run/mysqld
2022-06-24 15:58:13 -04:00
echo "[i] Starting mysql database server..."
2022-06-24 14:58:58 -04:00
__mysql_test || __mysqld
if [ ! -d "/var/lib/mysql/wordpress" ]; then
2022-06-24 15:58:13 -04:00
echo "[i] Creating word database..."
mysql -uroot -p$DB_PASS -e "CREATE DATABASE $DB_NAME"
mysql -uroot -p$DB_PASS -e "GRANT ALL PRIVILEGES ON $DB_NAME.* TO $DB_NAME@localhost IDENTIFIED BY '$DB_PASS'"
fi
[ -z "$DB_HOST" ] && echo "Database host: not set" || echo "Database host: $DB_HOST"
[ -z "$DB_NAME" ] && echo "Database name: not set" || echo "Database name: $DB_NAME"
[ -z "$DB_USER" ] && echo "Database user: not set" || echo "Database user: $DB_USER"
[ -z "$DB_PASS" ] && echo "Database pass: not set" || echo "Database pass: $DB_PASS"
2022-06-24 16:46:48 -04:00
echo "[i] Starting php-fpm..."
__phpfpm
2022-06-24 15:58:13 -04:00
echo "[i] Starting web server..."
nginx