-
Notifications
You must be signed in to change notification settings - Fork 0
/
install-wp.sh
executable file
·513 lines (441 loc) · 17.3 KB
/
install-wp.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
#!/bin/bash
# install-wp
# Bash script that downloads and installs WordPress at the specified location, with the specified domain and database.
# https://github.com/farid-hadi/install-wp
version="0.3.0-beta"
# Print version function
function printVersion() {
printf "install-wp version: $version\n"
}
# Print help text function
function printHelp() {
printVersion
printf "\n"
printf "Quickly install a new WordPress site in your development environment.\n"
printf "This script will download WordPress from WordPress.org, extract the files to your desired document root, create the database and database user with values extracted from the MySQL Option files, create a wp-config.php file with the correct values and configure a server block or virtual host for the desired domain.\n"
printf "\n"
printf "Prior to running this script you need to create the below two 'MySQL Option files' with '[client] sections' in your home directory. "
printf "You can copy the files install-wp/conf/mysql-opts-admin-template.cnf and install-wp/conf/mysql-opts-site-template.cnf to create your option files.\n"
printf "\n"
printf "IMPORTANT: Make sure you restrict access to your option files with e.g. chmod 600 so that unauthorized users can't see your database passwords!\n"
printf "\n"
printf "MySQL Option Files:\n"
printf "~/install-wp/conf/mysql-opts-admin.cnf\n"
printf " Needs to contain username and password for an existing database user that can create new databases, users and grants.\n"
printf "~/install-wp/conf/mysql-opts-site.cnf\n"
printf " Needs to contain username, password and database name for the new database and database user that you wish to be created for this site.\n"
printf "\n"
printf "Usage: install-wp [options]\n"
printf "\n"
printf "If you don't pass any options the script will prompt you for the required data.\n"
printf "\n"
printf "Options:\n"
printf " -h, --help Display this help and exit.\n"
printf " -v, --version Display version and exit.\n"
printf " -d, --domain Set domain name of the site you're creating.\n"
printf " -docroot, --document-root Set document root of the site you're creating. I.e. where to install the WordPress core files.\n"
printf " --nginx Set your chosen web server to Nginx.\n"
printf " --apache, --apache2, --httpd Set your chosen web server to Apache.\n"
printf "\n"
printf "Please also see documentation at https://github.com/farid-hadi/install-wp\n"
}
# Read arguments passed with command
while [ $# -gt 0 ]; do
case "$1" in
-v|--version)
printVersion
exit 0
;;
-h|--help)
printHelp
exit 0
;;
-d|--domain)
domain="$2"
;;
-docroot|--document-root)
document_root="$2"
;;
--nginx)
web_server="nginx"
shift
continue
;;
--apache|--apache2|--httpd)
web_server="apache2"
shift
continue
;;
*)
printf "${red}Error: Invalid arguments.${cf}\n"
printf "Use --help for help with usage.\n"
exit 1
esac
shift
shift
done
# Text formatting
underline='\033[4m'
green='\033[0;32m'
red='\033[1;31m'
cf='\033[0m' # Clear formatting
# Function: Output message and exit with exit code
# Params: $1 = exit code, $2 = message
function abort() {
if [ $1 -ne 0 ]; then
printf "${red}$2${cf}\n"
else
printf "$2\n"
fi
exit $1
}
# Require root privileges
if [ $EUID -ne 0 ]; then
abort 1 "Error: This script must be run as root."
fi
# Prompt for any missing arguments
if [ -z "$domain" ]; then
read -p "Domain name (optional): " domain
fi
if [ -z "$document_root" ]; then
read -e -p "Document root (where to place WordPress files): " document_root
fi
if [ -z "$web_server" ]; then
read -p "Web server (nginx|apache2|httpd): " web_server
fi
# Set $web_server to "apache2" if user entered apache or httpd
if [ "$web_server" == "apache" ] || [ "$web_server" == "httpd" ]; then
web_server="apache2"
fi
# Abort if any required arguments are missing
if [ -z "$document_root" ]; then
abort 1 "Aborted. No document root supplied as argument."
fi
if [ -z "$web_server" ]; then
abort 1 "Aborted. No web server supplied as argument."
fi
if [ "$web_server" != "nginx" ] && [ "$web_server" != "apache2" ]; then
abort 1 "Aborted. Invalid value supplied for web server."
fi
# Check that the commands we need are installed and store their absolute paths in variables
mkdir_cmd=$(which mkdir)
if [ -z "$mkdir_cmd" ]; then
abort 1 "Aborted. mkdir does not seem to be installed."
fi
curl_cmd=$(which curl)
if [ -z "$curl_cmd" ]; then
abort 1 "Aborted. CURL does not seem to be installed."
fi
tar_cmd=$(which tar)
if [ -z "$tar_cmd" ]; then
abort 1 "Aborted. tar does not seem to be installed."
fi
awk_cmd=$(which awk)
if [ -z "$awk_cmd" ]; then
abort 1 "Aborted. AWK does not seem to be installed."
fi
find_cmd=$(which find)
if [ -z "$find_cmd" ]; then
abort 1 "Aborted. find does not seem to be installed."
fi
mysql_cmd=$(which mysql)
if [ -z "$mysql_cmd" ]; then
abort 1 "Aborted. MySQL/MariaDB does not seem to be installed."
fi
# Ask for verfication before beginning install
printf "Install WordPress site with below details?\n"
printf "Domain name: ${domain}\n"
printf "Document root: ${document_root}\n"
printf "Web server: ${web_server}\n"
printf "Continue? [yes/No]: "
read continue
if [ -z "$continue" ] || [ "$continue" != "yes" ]; then
abort 1 "Aborted. No site created."
fi
unset continue
# If chosen web server is Nginx, get command and user
if [ "$web_server" == "nginx" ]; then
nginx_cmd=$(which nginx)
if [ -z "$nginx_cmd" ]; then
abort 1 "Aborted. nginx does not seem to be installed."
else
# Get nginx user
declare $(ps -eo "%u,%c,%a" | grep nginx | $awk_cmd '
BEGIN { FS="," }
{gsub(/^[ \t]+|[ \t]+$/, "", $1)}
{gsub(/^[ \t]+|[ \t]+$/, "", $3)}
$1 != "root" && $3~/worker process/ { print "web_server_user="$1; exit }
')
if [ -z "$web_server_user" ]; then
abort 1 "Aborted. Could not get nginx user."
fi
fi
fi
# If chosen web server is Apache, get command and user
if [ "$web_server" == "apache2" ]; then
apache2_or_httpd="apache2"
apache_cmd=$(which apache2 2>/dev/null)
if [ -z "$apache_cmd" ]; then
apache2_or_httpd="httpd"
apache_cmd=$(which httpd 2>/dev/null)
fi
if [ -z "$apache_cmd" ]; then
abort 1 "Aborted. apache2 / httpd does not seem to be installed."
else
# Get apache user
declare $(ps -eo "%u,%c,%a" | grep $apache2_or_httpd | $awk_cmd -v apache2_or_httpd="$apache2_or_httpd" '
BEGIN { FS="," }
{gsub(/^[ \t]+|[ \t]+$/, "", $1)}
{gsub(/^[ \t]+|[ \t]+$/, "", $2)}
$1 != "root" && $2 == apache2_or_httpd { print "web_server_user="$1; exit }
')
if [ -z "$web_server_user" ]; then
abort 1 "Aborted. Could not get apache2 user."
fi
fi
fi
# Create the document root
printf "Creating document root...\n"
if [ -d $document_root ]; then
abort 1 "Aborted. The doucment root already exists."
else
$mkdir_cmd -p $document_root
fi
if [ $? -ne 0 ]; then
abort 1 "Aborted. Could not create document root."
fi
# Get users home directory
user_home=$(getent passwd $SUDO_USER | cut -d: -f6)
# Hidden tmp directory in install-wp/ in the user's home directory
user_home_tmp="$user_home/install-wp/.tmp"
if [ ! -d $user_home_tmp ]; then
$mkdir_cmd $user_home_tmp
fi
if [ $? -ne 0 ]; then
abort 1 "Aborted. Could not create temporary directory in home directory."
fi
# Check if we have a recent download of WordPress
printf "Checking for cached WordPress archive...\n"
if [ -f "$user_home_tmp/latest.tar.gz" ]; then
$find_cmd "$user_home_tmp" -name "latest.tar.gz" -type f -user root -mmin +360 -delete
fi
# Download WordPress if we don't have a recent download
if [ ! -f "$user_home_tmp/latest.tar.gz" ]; then
printf "Downloading WordPress...\n"
$curl_cmd -L -o "$user_home_tmp/latest.tar.gz" https://wordpress.org/latest.tar.gz
if [ $? -ne 0 ]; then
if [ -f "$user_home_tmp/latest.tar.gz" ]; then
rm "$user_home_tmp/latest.tar.gz"
fi
abort 1 "Aborted. Could not download WordPress."
fi
fi
# Extract WordPress tar file and place WordPress core in document root
printf "Extracting files...\n"
$tar_cmd xzf "$user_home_tmp/latest.tar.gz" -C "$document_root" --strip-components=1
# Read MySQL option files and get required information
printf "Reading MySQL Option files...\n"
mysql_admin_opts_file="$user_home/install-wp/conf/mysql-opts-admin.cnf"
mysql_site_opts_file="$user_home/install-wp/conf/mysql-opts-site.cnf"
if [ ! -f $mysql_admin_opts_file ]; then
abort 1 "Aborted. Could not find file install-wp/conf/mysql-opts-admin.cnf in home directory."
else
# Check file permissions to ensure user doesn't have insecure options file
file_permissions=$(stat -c %a $mysql_admin_opts_file)
if [ $file_permissions -ne 600 ] && [ $file_permissions -ne 400 ]; then
abort 1 "Aborted. Insecure file permissons on file install-wp/conf/mysql-opts-admin.cnf in home directory."
fi
fi
if [ ! -f $mysql_site_opts_file ]; then
abort 1 "Aborted. Could not find file install-wp/conf/mysql-opts-site.cnf in home directory."
else
# Check file permissions to ensure user doesn't have insecure options file
file_permissions=$(stat -c %a $mysql_site_opts_file)
if [ $file_permissions -ne 600 ] && [ $file_permissions -ne 400 ]; then
abort 1 "Aborted. Insecure file permissons on file install-wp/conf/mysql-opts-site.cnf in home directory."
fi
fi
# Extract data from MySQL option file
declare $($awk_cmd '
BEGIN { FS="=|#" }
$1 == "user" { gsub(/^[ \t]+|[ \t]+$/, "", $2); print "database_user="$2; next }
$1 == "password" { gsub(/^[ \t]+|[ \t]+$/, "", $2); print "database_password="$2; next }
$1 == "database" { gsub(/^[ \t]+|[ \t]+$/, "", $2); print "database_name="$2; next }
' "$mysql_site_opts_file" )
# Check that we have all the required database information
if [ -z "$database_user" ]; then
abort 1 "Aborted. Could not extract database user from MySQL options file."
fi
if [ -z "$database_password" ]; then
abort 1 "Aborted. Could not extract database password MySQL options file."
fi
if [ -z "$database_name" ]; then
abort 1 "Aborted. Could not extract database name from MySQL options file."
fi
# Create database and database user
printf "Creating database...\n"
$mysql_cmd --defaults-extra-file="$mysql_admin_opts_file" -e "USE $database_name;" &>/dev/null;
if [ $? -eq 0 ]; then
abort 1 "Aborted. Database already exists."
fi
$mysql_cmd --defaults-extra-file="$mysql_admin_opts_file" -e "CREATE DATABASE $database_name;" 1>/dev/null;
if [ $? -ne 0 ]; then
abort 1 "Aborted. Could not create database."
fi
$mysql_cmd --defaults-extra-file="$mysql_admin_opts_file" -e "CREATE USER IF NOT EXISTS '$database_user'@'localhost' IDENTIFIED BY '$database_password';" 1>/dev/null;
if [ $? -ne 0 ]; then
# Since we couldn't create the user, let's delete the database we created
$mysql_cmd --defaults-extra-file="$mysql_admin_opts_file" -e "DROP DATABASE $database_name;" 1>/dev/null;
abort 1 "Aborted. Could not create database user."
fi
$mysql_cmd --defaults-extra-file="$mysql_admin_opts_file" -e "GRANT ALL ON $database_name.* TO '$database_user'@'localhost';" 1>/dev/null;
if [ $? -ne 0 ]; then
# Since we couldn't set the grants, let's delete the database we created
$mysql_cmd --defaults-extra-file="$mysql_admin_opts_file" -e "DROP DATABASE $database_name;" 1>/dev/null;
abort 1 "Aborted. Could not set grants for database user."
fi
# Get salts from https://api.wordpress.org/secret-key/1.1/salt/ and save them in a tmp tile
printf "Downloading salts...\n"
wp_salts_tmp_file="wp-salts-"$(date +%s%N)".txt"
$curl_cmd -sS -o "$user_home_tmp/$wp_salts_tmp_file" https://api.wordpress.org/secret-key/1.1/salt/ && chmod 600 "$user_home_tmp/$wp_salts_tmp_file"
if [ $? -ne 0 ]; then
if [ -f "$user_home_tmp/$wp_salts_tmp_file" ]; then
rm "$user_home_tmp/$wp_salts_tmp_file"
fi
abort 1 "Aborted. Could not download salts."
fi
# Create wp-config.php with database information from MySQL option file for site
printf "Creating wp-config.php...\n"
$awk_cmd '
BEGIN{ OFS = "\047"; fname = ""; idx = 0 }
fname != FILENAME { fname = FILENAME; idx++ }
idx == 1 && $1 == "user" { gsub(/^[ \t]+|[ \t]+$/, "", $2); user = $2; next }
idx == 1 && $1 == "password" { gsub(/^[ \t]+|[ \t]+$/, "", $2); password = $2; next }
idx == 1 && $1 == "database" { gsub(/^[ \t]+|[ \t]+$/, "", $2); database = $2; next }
idx == 2 && /^define\(/ { salts[$2] = $4; next }
idx == 3 && /^define\(/ && $2~/DB_NAME/ { $4 = database }
idx == 3 && /^define\(/ && $2~/DB_USER/ { $4 = user }
idx == 3 && /^define\(/ && $2~/DB_PASSWORD/ { $4 = password }
idx == 3 && /^define\(/ && ($2 in salts) { $4 = salts[$2] }
idx == 3' FS="=|#" "$mysql_site_opts_file" FS="\047" "$user_home_tmp/$wp_salts_tmp_file" FS="\047" "$document_root/wp-config-sample.php" > "$document_root/wp-config.php"
if [ $? -ne 0 ]; then
rm "$user_home_tmp/$wp_salts_tmp_file"
abort 1 "Aborted. Could not create wp-config.php file."
fi
# Set file permission for document root
printf "Setting file permissions...\n"
chown -R $web_server_user:$web_server_user "$document_root"
chmod -R 770 "$document_root"
# Create Nginx server block for domain if web server is Nginx and domain is not none/localhost
if [ "$web_server" == "nginx" ] && [ -n "$domain" ] && [ "$domain" != "localhost" ] && [ "$domain" != "none" ]; then
printf "Creating Nginx server block...\n"
vhost_config_file="$domain"
if [ -d "/etc/nginx/sites-available" ] && [ -d "/etc/nginx/sites-enabled" ]; then
vhost_config_dir="/etc/nginx/sites-available"
elif [ -d "/etc/nginx/vhosts.d" ]; then
vhost_config_dir="/etc/nginx/vhosts.d"
vhost_config_file="$domain.conf"
elif [ -d "/etc/nginx/conf.d" ]; then
vhost_config_dir="/etc/nginx/conf.d"
vhost_config_file="$domain.conf"
fi
if [ -z "$vhost_config_dir" ]; then
abort 1 "Aborted. Could not find location for server block configuration."
fi
php_fpm_sock=$($find_cmd /var/run/php/ -name "php*-fpm.sock" -type s -print -quit 2>/dev/null)
if [ -z "$php_fpm_sock" ]; then
php_fpm_sock=$($find_cmd "/var/run/php-fpm/" -name "*.sock" -type s -print -quit 2>/dev/null)
fi
if [ -z "$php_fpm_sock" ]; then
abort 1 "Aborted. Could not find PHP FPM socket."
fi
read -r -d '' server_block_template <<"EOF"
# Created by install-wp
server {
server_name DOMAIN;
listen 80;
listen [::]:80;
root DOCROOT;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
try_files $fastcgi_script_name =404;
set $path_info $fastcgi_path_info;
include fastcgi_params;
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_pass unix:PHPFPMSOCK;
}
}
EOF
printf "$server_block_template" | $awk_cmd -v docroot="$document_root" -v domain="$domain" -v php_fpm_sock="$php_fpm_sock" '
/^\tserver_name/ && $2 == "DOMAIN;" && /;$/ { $2 = domain";"; print "\t"$0; next }
/^\troot/ && $2 == "DOCROOT;" && /;$/ { $2 = docroot";"; print "\t"$0; next }
/^\t\tfastcgi_pass/ && $2 == "unix:PHPFPMSOCK;" && /;$/ { $2 = "unix:"php_fpm_sock";"; print "\t\t"$0; next } 1' > "$vhost_config_dir/$vhost_config_file"
if [ $? -ne 0 ]; then
abort 1 "Aborted. Could not create Nginx server block."
else
if [ "$vhost_config_dir" == "/etc/nginx/sites-available" ]; then
ln -s "$vhost_config_dir/$vhost_config_file" /etc/nginx/sites-enabled
fi
printf "Reloading Nginx...\n"
$nginx_cmd -s reload
fi
fi
# Create Apache Virtual Host for domain if web server is Apache and domain is not none/localhost
if [ "$web_server" == "apache2" ] && [ -n "$domain" ] && [ "$domain" != "localhost" ] && [ "$domain" != "none" ]; then
printf "Creating Apache virtual host...\n"
vhost_config_file="$domain.conf"
if [ -d "/etc/apache2/sites-available" ] && [ -d "/etc/apache2/sites-enabled" ]; then
vhost_config_dir="/etc/apache2/sites-available"
elif [ -d "/etc/httpd/sites-available" ] && [ -d "/etc/httpd/sites-enabled" ]; then
vhost_config_dir="/etc/httpd/sites-available"
elif [ -d "/etc/httpd/conf.d" ]; then
vhost_config_dir="/etc/httpd/conf.d"
fi
if [ -z "$vhost_config_dir" ]; then
abort 1 "Aborted. Could not find location for virtual host configuration."
fi
read -r -d '' virtual_host_template <<"EOF"
# Created by install-wp
<VirtualHost *:80>
ServerName DOMAIN
DocumentRoot DOCROOT
DirectoryIndex index.php index.html
<Directory DOCROOT>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
EOF
printf "$virtual_host_template" | $awk_cmd -v docroot="$document_root" -v domain="$domain" '
/^\tServerName/ && $2 == "DOMAIN" { $2 = domain; print "\t"$0; next }
/^\tDocumentRoot/ && $2 == "DOCROOT" { $2 = docroot; print "\t"$0; next }
/^\t<Directory/ && $2 == "DOCROOT>" { $2 = docroot">"; print "\t"$0; next } 1' > "$vhost_config_dir/$vhost_config_file"
if [ $? -ne 0 ]; then
abort 1 "Aborted. Could not create Apache virtual host."
else
if [ "$vhost_config_dir" == "/etc/apache2/sites-available" ]; then
ln -s "$vhost_config_dir/$vhost_config_file" /etc/apache2/sites-enabled
elif [ "$vhost_config_dir" == "/etc/httpd/sites-available" ]; then
ln -s "$vhost_config_dir/$vhost_config_file" /etc/httpd/sites-enabled
fi
printf "Restarting Apache...\n"
apachectl -k graceful &>/dev/null
if [ $? -ne 0 ]; then
systemctl restart httpd &>/dev/null
fi
fi
fi
# Clean up - delete tmp files
printf "Cleaning up...\n"
if [ -f "$user_home_tmp/$wp_salts_tmp_file" ]; then
rm "$user_home_tmp/$wp_salts_tmp_file"
fi
printf "${green}All done!${cf}\n"