#!/bin/bash set -e PATH=/root/hosting-tools/wordpress:$PATH TEMPLATE_DIR=/root/hosting-tools/wordpress SITE_NAME=$1 NEW_DB_USER=wordpress@$SITE_NAME NEW_DB_NAME=wordpress@$SITE_NAME TEMPLATE_FILE=$TEMPLATE_DIR/wordpress-site.conf.ctpl APACHE_CONFIG_TARGET=/etc/apache2/sites-enabled/$SITE_NAME.conf warn() { echo "$0: Warning: $*" >&2 } check_dependencies() { [ -f "$TEMPLATE_FILE" ] for cmd in systemctl a2ensite ctpl mysql mysqladmin curl wordpress-create-wp-config btrfs do >/dev/null command -v "$cmd" done } fill_template() { ctpl -c SITE_NAME=\""$1"\" -c 'subdomains=["www"];' "$TEMPLATE_FILE" } create_apache_btrfs_subvolumes() { [ "$SITE_NAME" ] set -- for d in /srv/"$SITE_NAME" /srv/"$SITE_NAME"/public_html /srv/"$SITE_NAME"/logs do [ -d "$d" ] || set -- "$@" "$d" done btrfs subvolume create -- "$@" } create_apache_vhost() { if [ ! -e "$APACHE_CONFIG_TARGET" ] then create_apache_btrfs_subvolumes fill_template "$SITE_NAME" > /etc/apache2/sites-available/"$SITE_NAME".conf a2ensite $SITE_NAME systemctl reload apache2 else warn "create_apache_vhost: skipped" fi } # Install wordpress content to apache htdocs directory install_wordpress_documents() { for f in \ /srv/"$SITE_NAME"/public_html/wordpress/wp-config.php \ /srv/"$SITE_NAME"/public_html/wp-config.php do if [ -e "$f" ] then warn "install_wordpress_documents: skipped" return fi done ( cd /srv/"$SITE_NAME"/public_html curl https://wordpress.org/latest.tar.gz | tar -zx cd wordpress chown "$SITE_USER"."$SITE_GROUP" -R . wordpress-create-wp-config "$NEW_DB_NAME" "$NEW_DB_USER" > wp-config.php chown root.root wp-config.php ) } [ "$SITE_NAME" ] check_dependencies create_apache_vhost install_wordpress_documents wordpress-mysql --create "$SITE_NAME"