-
Notifications
You must be signed in to change notification settings - Fork 12
/
wpinstall.sh
139 lines (117 loc) · 5.13 KB
/
wpinstall.sh
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/bin/bash
#Copyright: (c) 2017 by Bhavin Gandhi
#License: GNU GPL v3, see LICENSE for more details
log_file=wpinstall.log
# this function will ensure that the given package is installed
function check_install {
pkg_name=$1
dpkg-query --show $pkg_name &>> $log_file
if [ $? -ne 0 ]; then
echo "Installing $pkg_name"
apt-get install $pkg_name -y &>> $log_file
if [ $? -ne 0 ]; then
echo "Installation of $pkg_name failed!" 1>&2
exit 1
else
echo "done."
fi
else
echo "$pkg_name is already installed, skipping..."
fi
}
echo -e "Installing nginx, MySQL, php on the system and deploying latest WordPress. It will ask for domain name.\n"
# check if running on Ubuntu or Debian
distro=$(lsb_release -i | cut -f 2)
if [ $distro != "Ubuntu" ] && [ $distro != "Debian" ]; then
echo "This script is designed to work only on Ubuntu or Debian." 1>&2
exit 1
fi
# check if running with root privileges
if [ $(id -u) -ne 0 ]; then
echo "This script needs root privileges to work correctly." 1>&2
exit 1
fi
# update apt lists
echo "Updating apt package lists, this may take some time."
apt-get update &>> $log_file
check_install nginx
check_install debconf-utils
# set configurations for mysql-server
echo "Creating temporary configuration for mysql"
db_password="secpass#1"
debconf-set-selections <<< "mysql-server mysql-server/root_password password $db_password" &>> $log_file
debconf-set-selections <<< "mysql-server mysql-server/root_password_again password $db_password" &>> $log_file
check_install mysql-server
# delete the installation configurations of mysql-server
echo "Deleting temporary configuration of mysql"
debconf-communicate mysql-server <<< 'PURGE' &>> $log_file
check_install php7.0-fpm
check_install php-mysql
# don't execute closest php file, if not found
echo "Configuring php"
sed -i "s/;*cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g" /etc/php/7.0/fpm/php.ini &>> $log_file
# increase post and upload size
sed -i "s/post_max_size = [0-9]*M/post_max_size = 200M/g" /etc/php/7.0/fpm/php.ini &>> $log_file
sed -i "s/upload_max_filesize = [0-9]*M/upload_max_filesize = 100M/g" /etc/php/7.0/fpm/php.ini &>> $log_file
systemctl restart php7.0-fpm.service &>> $log_file
if [ $1 == "--domain" ] && [ ! -z $2 ]; then
echo "Found domain name $2 from command line arguments, skipping user dialouge." >> $log_file
domain_name=$2
else
echo "Enter domain name: "
read domain_name
while [ -z $domain_name ]; do
echo "domain name cannot be blank! Please enter a valid name."
read domain_name
done
fi
echo "Creating /etc/hosts entry for new site"
chown $(whoami) /etc/hosts &>> $log_file
sed -i "\$a127.0.0.1\t$domain_name" /etc/hosts &>> $log_file
chown root /etc/hosts &>> $log_file
# create nginx configuration for $domain_name
# check if nginx.conf exist
if [ ! -f nginx.conf ]; then
echo "nginx.conf file is missing in current directory. Aborting..." 1>&2
exit 1
fi
echo "Creating nginx configuration for new site"
cp nginx.conf /etc/nginx/sites-available/$domain_name &>> $log_file
sed -i "s/domain_name/$domain_name/g" /etc/nginx/sites-available/$domain_name &>> $log_file
ln -s /etc/nginx/sites-available/$domain_name /etc/nginx/sites-enabled/ &>> $log_file
systemctl reload nginx &>> $log_file
echo "Downloading latest wordpress.zip and extracting in site root"
check_install unzip
curl -L http://wordpress.org/latest.zip -o wordpress.zip &>> $log_file
unzip wordpress.zip -d /tmp/ &>> $log_file
rm -f wordpress.zip &>> $log_file
mkdir /var/www/$domain_name &>> $log_file
mv /tmp/wordpress/* /var/www/$domain_name/ &>> $log_file
rm -rf /tmp/wordpress &>> $log_file
echo "Creating database for new site"
db_name=${domain_name//./_}_db
mysql -u root -p$db_password -e "USE $db_name;" &>> $log_file
if [ $? -ne 0 ]; then
mysql -u root -p$db_password -e "CREATE DATABASE $db_name;" &>> $log_file
else
echo "Database $db_name already exist."
fi
# create wp-config.php
echo "Creating wp-config.php for WordPress site"
cp /var/www/$domain_name/wp-config-sample.php /var/www/$domain_name/wp-config.php &>> $log_file
sed -i "s/database_name_here/$db_name/g" /var/www/$domain_name/wp-config.php &>> $log_file
sed -i "s/username_here/root/g" /var/www/$domain_name/wp-config.php &>> $log_file
sed -i "s/password_here/$db_password/g" /var/www/$domain_name/wp-config.php &>> $log_file
salts_keys=$(curl https://api.wordpress.org/secret-key/1.1/salt)
salts_keys=$(echo $salts_keys | sed -e 's/\([[\/.*]\|\]\)/\\&/g')
sed -i "/_KEY/d" /var/www/$domain_name/wp-config.php &>> $log_file
sed -i "/_SALT/d" /var/www/$domain_name/wp-config.php &>> $log_file
sed -i "/define('DB_COLLATE', '');/a$salts_keys" /var/www/$domain_name/wp-config.php &>> $log_file
# change the owner so that php-fpm will have write access
chown -R www-data:www-data /var/www/$domain_name/ &>> $log_file
echo -e "\nSite can be browsed at http://$domain_name"
echo "root directory of site: /var/www/$domain_name"
echo "nginx configuration of site: /etc/nginx/sites-available/$domain_name"
echo "Database user: root"
echo "Database password: $db_password"
echo "Database name: $db_name"