summaryrefslogtreecommitdiff
path: root/wordpress/wordpress-create-new-site
blob: b3b9b72ee6dfabea2925437bdc5ce19fd6b9b366 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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
#!/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"