-
-
Notifications
You must be signed in to change notification settings - Fork 84
/
sites
610 lines (525 loc) · 25.4 KB
/
sites
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
#!/bin/bash
# This is a library for Site Manager Plugin
# Functions for site creation and delete
source /opt/webinoly/lib/general
wp_cache_plugins() {
if [[ ! -d /var/www/$domain/htdocs/wp-content/plugins/nginx-helper ]]; then
# Download Nginx Helper Plugin
sudo wget --timeout=15 -t 1 -qrO /var/www/$domain/htdocs/nginx-helper-plugin.zip https://downloads.wordpress.org/plugin/nginx-helper.latest-stable.zip
sudo unzip -qq /var/www/$domain/htdocs/nginx-helper-plugin.zip -d /var/www/$domain/htdocs/wp-content/plugins/
sudo rm /var/www/$domain/htdocs/nginx-helper-plugin.zip
echo ""
echo "${gre} Nginx Helper Plugin has been installed!"
echo " Please, activate this plugin for a better experience with FastCgi Cache."
echo "${blu} ** IMPORTANT - Plugin Settings **"
echo "- The “Caching Method” should be set to “Nginx FastCgi Cache”."
echo "- Support for purge/url method has been disabled because it is considered a security risk."
echo "- The Purge Method should be set to “Delete local server cache file” "
echo "${end}"
else
echo "${red}Nginx Helper Plugin is already installed!${end}"
fi
if [[ ! -d /var/www/$domain/htdocs/wp-content/plugins/redis-cache ]]; then
# Download Redis Object Cache Plugin
sudo wget --timeout=15 -t 1 -qrO /var/www/$domain/htdocs/redis-cache-plugin.zip https://downloads.wordpress.org/plugin/redis-cache.latest-stable.zip
sudo unzip -qq /var/www/$domain/htdocs/redis-cache-plugin.zip -d /var/www/$domain/htdocs/wp-content/plugins/
sudo rm /var/www/$domain/htdocs/redis-cache-plugin.zip
echo ""
echo "${gre} Redis Object Cache Plugin has been installed!"
echo " Please, activate this plugin for a better experience with WordPress Object Cache."
echo "${blu} ** IMPORTANT - Plugin Settings **"
echo "- Go to “Menu > Settings > Redis” and be sure that Object Cache is enabled. "
echo "${end}"
else
echo "${red}Redis Object Cache Plugin is already installed!${end}"
fi
sudo chown -R www-data:www-data /var/www/$domain/htdocs/wp-content/plugins
}
domain_check() {
local din="$1"
# Only numerals 0-9, basic Latin letters, both lowercase and uppercase, hyphen.
[[ $din =~ ^[\.0-9A-Za-z\-]+$ ]] || domfail="true"
# Check Lenght
[[ ${#din} -gt 67 ]] && domfail="true"
# Can not start or end with a hyphen
[[ $(echo "${din}" | cut -c-1) == "-" || $(echo "${din}" | rev | cut -c-1) == "-" ]] && domfail="true"
# Can not contain two points together and can not start or end with a point
[[ $din == *..* || $(echo "${din}" | cut -c-1) == "." || $(echo "${din}" | rev | cut -c-1) == "." ]] && domfail="true"
[[ $domfail == "true" ]] && echo "${red}[WARNING] Domain names can only contain letters, numbers or a hyphen; can not start or end with a hyphen and can be up to 67 characters long.${end}"
}
dbword_check() {
local win="$1"
local RANDOM_NAME=`pwgen -s -1`
# Reserved words - https://mariadb.com/kb/en/library/reserved-words/
# https://mariadb.com/kb/en/library/identifier-names/
# Trim start/end spaces and quotes
win=$(echo ${win//\'})
win=$(echo ${win//\"} | xargs)
# Check Lenght
[[ ( ${#win} -gt 80 && $2 == "user" ) || ( ${#win} -gt 64 && $2 != "user" ) ]] && win="Webinoly_$RANDOM_NAME"
# Reserved words
shopt -s nocasematch
if [[ $win =~ ^(ACCESSIBLE|ADD|ALL|ALTER|ANALYZE|AND|AS|ASC|ASENSITIVE|BEFORE|BETWEEN|BIGINT|BINARY|BLOB|BOTH|BY|CALL|CASCADE|CASE|CHANGE|CHAR|CHARACTER|CHECK|COLLATE|COLUMN|CONDITION|CONSTRAINT|CONTINUE|CONVERT|CREATE|CROSS|CURRENT_DATE|CURRENT_TIME|CURRENT_TIMESTAMP|CURRENT_USER|CURSOR|DATABASE|DATABASES|DAY_HOUR|DAY_MICROSECOND|DAY_MINUTE|DAY_SECOND|DEC|DECIMAL|DECLARE|DEFAULT|DELAYED|DELETE|DESC|DESCRIBE|DETERMINISTIC|DISTINCT|DISTINCTROW|DIV|DOUBLE|DROP|DUAL|EACH|ELSE|ELSEIF|ENCLOSED|ESCAPED|EXISTS|EXIT|EXPLAIN|FALSE|FETCH|FLOAT|FLOAT4|FLOAT8|FOR|FORCE|FOREIGN|FROM|FULLTEXT|GENERAL|GRANT|GROUP|HAVING|HIGH_PRIORITY|HOUR_MICROSECOND|HOUR_MINUTE|HOUR_SECOND|IF|IGNORE|IGNORE_SERVER_IDS|IN|INDEX|INFILE|INNER|INOUT|INSENSITIVE|INSERT|INT|INT1|INT2|INT3|INT4|INT8|INTEGER|INTERVAL|INTO|IS|ITERATE|JOIN|KEY|KEYS|KILL|LEADING|LEAVE|LEFT|LIKE|LIMIT|LINEAR|LINES|LOAD|LOCALTIME|LOCALTIMESTAMP|LOCK|LONG|LONGBLOB|LONGTEXT|LOOP|LOW_PRIORITY|MASTER_HEARTBEAT_PERIOD|MASTER_SSL_VERIFY_SERVER_CERT|MATCH|MAXVALUE|MEDIUMBLOB|MEDIUMINT|MEDIUMTEXT|MIDDLEINT|MINUTE_MICROSECOND|MINUTE_SECOND|MOD|MODIFIES|NATURAL|NOT|NO_WRITE_TO_BINLOG|NULL|NUMERIC|ON|OPTIMIZE|OPTION|OPTIONALLY|OR|ORDER|OUT|OUTER|OUTFILE|PARTITION|PRECISION|PRIMARY|PROCEDURE|PURGE|RANGE|READ|READS|READ_WRITE|REAL|RECURSIVE|REFERENCES|REGEXP|RELEASE|RENAME|REPEAT|REPLACE|REQUIRE|RESIGNAL|RESTRICT|RETURN|REVOKE|RIGHT|RLIKE|ROWS|SCHEMA|SCHEMAS|SECOND_MICROSECOND|SELECT|SENSITIVE|SEPARATOR|SET|SHOW|SIGNAL|SLOW|SMALLINT|SPATIAL|SPECIFIC|SQL|SQLEXCEPTION|SQLSTATE|SQLWARNING|SQL_BIG_RESULT|SQL_CALC_FOUND_ROWS|SQL_SMALL_RESULT|SSL|STARTING|STRAIGHT_JOIN|TABLE|TERMINATED|THEN|TINYBLOB|TINYINT|TINYTEXT|TO|TRAILING|TRIGGER|TRUE|UNDO|UNION|UNIQUE|UNLOCK|UNSIGNED|UPDATE|USAGE|USE|USING|UTC_DATE|UTC_TIME|UTC_TIMESTAMP|VALUES|VARBINARY|VARCHAR|VARCHARACTER|VARYING|WHEN|WHERE|WHILE|WINDOW|WITH|WRITE|XOR|YEAR_MONTH|ZEROFILL)$ ]]; then
win="Webinoly_$RANDOM_NAME"
fi
shopt -u nocasematch
# Only numerals 0-9, basic Latin letters, both lowercase and uppercase, dollar sign, underscore.
[[ $win =~ ^[0-9A-Za-z\$_]+$ ]] || win="Webinoly_$RANDOM_NAME"
# Can not contain only numbers
[[ $win =~ ^[0-9]+$ ]] && win="Webinoly_$RANDOM_NAME"
# Floating point number confusing
[[ ${win:0:1} =~ ^[0-9]+$ && ${win:1:1} == "e" ]] && win="Webinoly_$RANDOM_NAME"
echo $win
}
wpinstall() {
[[ $domain == "-mysql" ]] && local dom="Webinoly_$(pwgen -s -1)" || local dom=$(echo $domain | sed "s/[^0-9A-Za-z]/_/g")
local ROOT_PASS=$( echo $(conf_read mysql-root) | openssl enc -d -a -salt )
local AUTOGENPASS_WPDB=`pwgen -s -1`
local setupmysql="y"
local setupwp="y"
local dbhost="localhost"
local dbname=$dom
local dbuser=$dom
local dbpass=$AUTOGENPASS_WPDB
local dbpref="wp_"
# WP with Custom Data
if [[ $wp == [35] ]]; then
[[ $setmsqcustom == "false" ]] && setupmysql="n"
[[ $setpwpcustom == "false" ]] && setupwp="n"
dbhost="$dbhostcustom"
dbname="$dbnamecustom"
dbuser="$dbusercustom"
dbpass="$dbpasscustom"
dbpref="$dbprefcustom"
dburoot="$exusercustom"
dbproot="$expasscustom"
fi
# Print MySQL default data
if [[ $wp == 4 ]]; then
echo "${blu}"
echo "Host: $dbhost"
echo "Database Name: $dbname"
echo "Database User: $dbuser"
echo "Password: $dbpass"
echo "${end}"
fi
echo "${blu}"
if [[ $wp == 1 ]]; then
echo "Do you need to setup new MySQL database? [Y/n]"
while read -r -n 1 -s setupmysql; do
setupmysql=${setupmysql:-y}
[[ $setupmysql = [YyNn] ]] && break
done
echo "Create WP-Config file automatically? [Y/n]"
while read -r -n 1 -s setupwp; do
setupwp=${setupwp:-y}
[[ $setupwp = [YyNn] ]] && break
done
fi
# Just create DB, not install WP
[[ $wp == [045] ]] && setupwp="n"
# Download WP (latest version)
if [[ $wp == [123] ]]; then
sudo wget --timeout=15 -t 1 -qrO /var/www/$domain/htdocs/wp.tar.gz https://wordpress.org/latest.tar.gz
sudo tar -xf /var/www/$domain/htdocs/wp.tar.gz -C /var/www/$domain/htdocs/
sudo mv /var/www/$domain/htdocs/wordpress/* /var/www/$domain/htdocs/
sudo rm -rf /var/www/$domain/htdocs/wordpress
sudo rm /var/www/$domain/htdocs/wp.tar.gz
fi
# Generate and auto-fill wp-config.php and also create database
if [[ $setupmysql == [yY] || $setupwp == [yY] ]] ; then
local done="0"
while [[ $done != "1" ]]
do
done="1"
# Ask DB data or suggest default values
echo ""
[[ $wp == [2345] ]] || read -p "Database Host [localhost]:" dbhost
dbhost=${dbhost:-localhost}
if [[ $dbhost == "localhost" && $setupmysql == [yY] && $(conf_read mysql) != "true" ]]; then
echo "${red} [ERROR] MySQL is not installed or localhost was not found!${blu}"
done="0"
[[ $wp == 1 ]] && continue 1 || exit 1
fi
# Check if localhost or external DB
if [[ $dbhost != "localhost" && $setupmysql == [yY] ]]; then
if [[ $(conf_read mysql-client) != "true" ]]; then
echo "${gre}MySQL Client is not installed and we need it to stablish a connection with your external server."
echo "Wait while we install MySQL Client... installing!!!${end}"
mysql_client_install > /dev/null 2>&1 &
echo "${gre}MySQL Client has been successfully installed!${end}"
fi
echo "${blu}"
if [[ $wp == 1 || ($wp == [35] && ( -z $dburoot || -z $dbproot )) ]]; then
read -p "External DB root username [root]: " dburoot
read -p "External DB root password: " dbproot
echo ""
local dburoot=${dburoot:-root}
local dbproot=${dbproot:-nodefined}
fi
local dburl=$(echo "$dbhost" | cut -f 1 -d ':')
local dbport=$(echo "$dbhost" | cut -f 2 -d ':')
fi
# ************* Ask for DB Name and validate data *************** #
dom=$(dbword_check $dom)
[[ $wp == [2345] ]] || read -p "Database Name [$dom]:" dbname
dbname=${dbname:-$dom}
# Check for duplicate database names, if already exists ask for another dbname to create the new db
if [[ "$setupmysql" == [yY] ]]; then
local newdbname="$dbname"
while [[ $dbname == $newdbname && $dbreuse != [yY] ]]; do
# Chech connection to DB first
if [[ $dbhost == "localhost" ]]; then
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "quit"
if [[ $? != "0" ]]; then
done="0"
# Remove the Unattended feature and ask for values.
[[ $wp == [2345] ]] && wp=1
echo "${red}============================================"
echo " [Error]: Database conection failed."
echo "============================================${blu}"
echo ""
continue 2;
fi
else
sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "quit"
if [[ $? != "0" ]]; then
done="0"
[[ $wp == [2345] ]] && wp=1
echo "${red}============================================"
echo " [Error]: Database conection failed."
echo "============================================${blu}"
echo ""
continue 2;
fi
fi
if [[ $dbhost == "localhost" ]]; then
# https://stackoverflow.com/questions/7364709/bash-script-check-if-mysql-database-exists-perform-action-based-on-result
local newdbname=$(sudo mysqlshow --user=root -p$ROOT_PASS | grep -ow $dbname)
else
local newdbname=$(sudo mysqlshow -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" | grep -ow $dbname)
fi
if [[ $newdbname == $dbname || $dbname != $(dbword_check $dbname) ]]; then
echo ""
if [[ $newdbname == $dbname ]]; then
echo "${red}Database $dbname already exists!${blu}"
echo "Do you want to use this DB in your new site? [y/N]"
while read -r -n 1 -s dbreuse; do
local dbreuse=${dbreuse:-n}
[[ $dbreuse == [YyNn] ]] && break
done
fi
if [[ $dbname != $(dbword_check $dbname) ]]; then
if [[ $wp == [24] ]]; then
newdbname="Webinoly_$(pwgen -s -1)"
dbname="$newdbname"
continue 1;
fi
echo "${red}The DB Name can not be a reserved word or should only contain allowed characters!${blu}"
dbreuse="n"
fi
if [[ $dbreuse != [yY] ]]; then
echo ""
read -p "Please enter a new DB_NAME for your Database: " newdbname
[[ -z "$newdbname" ]] && newdbname="$dbname"
dbname="$newdbname"
elif [[ $dbreuse == [yY] ]]; then
# If you want to use the DB that already exist, abort DB creation.
setupmysql="n"
fi
fi
done
fi
# ************* Ask for DB User and validate data *************** #
[[ $wp == [2345] ]] || read -p "Database User [$dom]:" dbuser
dbuser=${dbuser:-$dom}
# Check for duplicate database users, if already exists ask for another dbuser to create the new user
if [[ "$setupmysql" == [yY] ]]; then
local newdbuser="$dbuser"
while [[ $dbuser == $newdbuser && $dbureuse != [yY] ]]; do
# Chech connection to DB first
if [[ $dbhost == "localhost" ]]; then
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "quit"
if [[ $? != "0" ]]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database conection failed."
echo "============================================${blu}"
echo ""
continue 2;
fi
else
sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "quit"
if [[ $? != "0" ]]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database conection failed."
echo "============================================${blu}"
echo ""
continue 2;
fi
fi
if [[ $dbhost == "localhost" ]]; then
# https://stackoverflow.com/questions/7364709/bash-script-check-if-mysql-database-exists-perform-action-based-on-result
local newdbuser=$(sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "SELECT User FROM mysql.user;" | grep -ow $dbuser)
else
local newdbuser=$(sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" -e "SELECT User FROM mysql.user;" | grep -ow $dbuser)
fi
if [[ $newdbuser == $dbuser || $dbuser != $(dbword_check $dbuser user) ]]; then
echo ""
if [[ $newdbuser == $dbuser ]]; then
echo "${red}User $dbuser already exists!${blu}"
echo "Do you want to use this DB User for your new site? [y/N]"
while read -r -n 1 -s dbureuse; do
local dbureuse=${dbureuse:-n}
[[ $dbureuse == [YyNn] ]] && break
done
fi
if [[ $dbuser != $(dbword_check $dbuser user) ]]; then
if [[ $wp == [24] ]]; then
newdbuser="Webinoly_$(pwgen -s -1)"
dbuser="$newdbuser"
continue 1;
fi
echo "${red}The DB User can not be a reserved word or should only contain allowed characters!${blu}"
dbureuse="n"
fi
if [[ $dbureuse != [yY] ]]; then
echo ""
read -p "Please enter a new DB_User for your Database: " newdbuser
[[ -z "$newdbuser" ]] && newdbuser="$dbuser"
dbuser="$newdbuser"
elif [[ $dbureuse == [yY] ]]; then
# If you want to use the User that already exist, abort DB creation.
setupmysql="n"
fi
fi
done
fi
if [[ $wp != [2345] ]]; then
read -p "Database Password [$AUTOGENPASS_WPDB]:" dbpass
# Not ask for wp_prefix when wp=0 (mysql only site)
[[ $wp == [045] ]] || read -p "Database Prefix [wp_]:" dbpref
echo "${end}"
# If empty, assign defalut values
dbpass=${dbpass:-$AUTOGENPASS_WPDB}
dbpref=${dbpref:-wp_}
fi
# DB Creation
if [[ "$setupmysql" == [yY] ]] ; then
if [[ $dbhost == "localhost" ]]; then
local dbsetup="CREATE DATABASE $dbname;GRANT ALL PRIVILEGES ON $dbname.* TO $dbuser@$dbhost IDENTIFIED BY '$dbpass';FLUSH PRIVILEGES;"
sudo mysql --connect-timeout=10 --user=root -p$ROOT_PASS -e "$dbsetup"
if [ $? != "0" ]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database creation failed."
echo "============================================${end}"
echo ""
fi
else
sudo mysql --connect-timeout=10 -h "$dburl" -P "$dbport" -u"$dburoot" -p"$dbproot" <<_EOF_
CREATE DATABASE ${dbname};
grant usage on ${dbname}.* to ${dbuser}@\`%\` identified by '${dbpass}';
grant all privileges on ${dbname}.* to ${dbuser}@\`%\`;
FLUSH PRIVILEGES;
_EOF_
if [ $? != "0" ]; then
done="0"
echo "${red}============================================"
echo " [Error]: Database creation failed."
echo "============================================${end}"
echo ""
fi
fi
fi
if [[ $done != "1" ]]; then
echo "${red} Some error ocurred during Database Configuration."
echo "${blu} Retry [Y/n]?"
while read -r -n 1 -s done; do
done=${done:-y}
[[ $done = [YyNn] ]] && break
done
[[ $done == [nN] ]] && done="1"
fi
done
fi
echo "${end}"
#WP-Config.php auto-setup
if [[ $setupwp == [yY] ]] ; then
# Generate random salt keys
local SALT_AUTHK=`pwgen -s -1 64`
local SALT_SECUR=`pwgen -s -1 64`
local SALT_LOGGE=`pwgen -s -1 64`
local SALT_NONCE=`pwgen -s -1 64`
local SALT_AUTHS=`pwgen -s -1 64`
local SALT_SECUS=`pwgen -s -1 64`
local SALT_LOGGS=`pwgen -s -1 64`
local SALT_NONCS=`pwgen -s -1 64`
cp /var/www/$domain/htdocs/wp-config-sample.php /var/www/$domain/wp-config.php
# Fill new wp-config with data
sudo sed -i "/DB_NAME/c \define('DB_NAME', '$dbname');" /var/www/$domain/wp-config.php
sudo sed -i "/DB_USER/c \define('DB_USER', '$dbuser');" /var/www/$domain/wp-config.php
sudo sed -i "/DB_PASSWORD/c \define('DB_PASSWORD', '$dbpass');" /var/www/$domain/wp-config.php
sudo sed -i "/DB_HOST/c \define('DB_HOST', '$dbhost');" /var/www/$domain/wp-config.php
sudo sed -i "/table_prefix/c \$table_prefix = '$dbpref';" /var/www/$domain/wp-config.php
sudo sed -i "/'AUTH_KEY'/c \define('AUTH_KEY', '$SALT_AUTHK');" /var/www/$domain/wp-config.php
sudo sed -i "/'SECURE_AUTH_KEY'/c \define('SECURE_AUTH_KEY', '$SALT_SECUR');" /var/www/$domain/wp-config.php
sudo sed -i "/'LOGGED_IN_KEY'/c \define('LOGGED_IN_KEY', '$SALT_LOGGE');" /var/www/$domain/wp-config.php
sudo sed -i "/'NONCE_KEY'/c \define('NONCE_KEY', '$SALT_NONCE');" /var/www/$domain/wp-config.php
sudo sed -i "/'AUTH_SALT'/c \define('AUTH_SALT', '$SALT_AUTHS');" /var/www/$domain/wp-config.php
sudo sed -i "/'SECURE_AUTH_SALT'/c \define('SECURE_AUTH_SALT','$SALT_SECUS');" /var/www/$domain/wp-config.php
sudo sed -i "/'LOGGED_IN_SALT'/c \define('LOGGED_IN_SALT', '$SALT_LOGGS');" /var/www/$domain/wp-config.php
sudo sed -i "/'NONCE_SALT'/c \define('NONCE_SALT', '$SALT_NONCS');" /var/www/$domain/wp-config.php
# Redis Conf
sudo sed -i "/stop editing/i \define('WP_REDIS_SELECTIVE_FLUSH', true);" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \define('WP_CACHE_KEY_SALT', 'redis_object_cache_for_wp_$domain');" /var/www/$domain/wp-config.php
# Multisite wp-config
if [[ "$type" == "-wpsubdir" || "$type" == "-wpsubdom" ]]; then
sudo sed -i "/stop editing/i \define('WP_ALLOW_MULTISITE', true);" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('MULTISITE', true);" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('DOMAIN_CURRENT_SITE', '$domain');" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('PATH_CURRENT_SITE', '/');" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('SITE_ID_CURRENT_SITE', 1);" /var/www/$domain/wp-config.php
sudo sed -i "/stop editing/i \#define('BLOG_ID_CURRENT_SITE', 1);" /var/www/$domain/wp-config.php
fi
if [ "$type" == "-wpsubdir" ]; then
sudo sed -i "/stop editing/i \#define('SUBDOMAIN_INSTALL', false);" /var/www/$domain/wp-config.php
elif [ "$type" == "-wpsubdom" ]; then
sudo sed -i "/stop editing/i \#define('SUBDOMAIN_INSTALL', true);" /var/www/$domain/wp-config.php
sudo sed -i "/server_name /c \ server_name $domain *.$domain;" /etc/nginx/sites-available/$domain
fi
fi
}
deletesite() {
if [[ -n $value ]]; then
dbdata=${value:1:-1}
local dbdatahost=$(echo "${dbdata}" | cut -d',' -f 1 -s)
[[ $dbdatahost == "external" ]] && dbdatadel=$(echo "${dbdata}" | cut -d',' -f 2 -s)
[[ $dbdatadel =~ ^(true|false)$ || $value == "force" ]] && local skip="true"
fi
# Determine if site is WP, so you should delete the DB too.
if [[ -a /var/www/$domain/wp-config.php || -a /var/www/$domain/htdocs/wp-config.php ]]; then
if [[ $skip != "true" ]]; then
echo "${blu}Delete Database [Y/n]?${end}"
while read -r -n 1 -s dbdel; do
local dbdel=${dbdel:-y}
[[ $dbdel = [YyNn] ]] && break
done
else
[[ $dbdatadel != "false" ]] && dbdel="Y"
fi
fi
[[ $dbdel == [yY] ]] && db_delete $domain
# Delete site files
sudo rm /etc/nginx/sites-available/$domain
# Check to avoid error when site is disabled.
[[ -L /etc/nginx/sites-enabled/$domain ]] && sudo rm /etc/nginx/sites-enabled/$domain
sudo rm -rf /var/www/$domain
}
createsite() {
# Check domain and then prevent reserved characters for file names.
domain_check $domain
domain=$(echo $domain | sed "s/[^\.0-9A-Za-z\-]/_/g" | sed "s/^[\.]/_/g" | sed "s/\.\./_/g")
if [[ $(conf_read php) != "true" && ("$wp" == [123] || "$type" == "-php") ]]; then
echo "${red}"
echo " [ERROR] PHP must be installed before you can create a WP site!"
echo "${end}"
exit 1
fi
# Check for duplicate sites
if [[ -a /etc/nginx/sites-available/$domain ]]; then
echo "${red}Site${blu} $domain ${red}could not be created because already exists!${end}"
exit 1
fi
sudo cp /opt/webinoly/templates/template-site-php /etc/nginx/sites-available/$domain
# Remove www support for subdomains - only main domain will support both www and non-www.
[[ $subdomflag == 1 ]] && sudo sed -i "s/ www.domain.com;/;/g" /etc/nginx/sites-available/$domain
# Nginx conf file for the new site (-php conf is default)
sudo sed -i "s/domain.com/$domain/g" /etc/nginx/sites-available/$domain
sudo chmod 644 /etc/nginx/sites-available/$domain
sudo chown root:root /etc/nginx/sites-available/$domain
sudo ln -s /etc/nginx/sites-available/$domain /etc/nginx/sites-enabled/$domain
# Create data folder for new site
if [[ $type != "-proxy" && $type != "-parked" ]]; then
if [[ ! -d /var/www/$domain/htdocs || ! -d /var/www/$domain ]]; then
sudo mkdir -p /var/www/$domain/htdocs
[[ "$wp" == [123] ]] && wpinstall
else
echo "${blu}"
echo " We found a folder with${end} $domain ${blu}site data, do you want to use it [Y/n]? "
while read -r -n 1 -s wwwexist; do
wwwexist=${wwwexist:-y}
[[ $wwwexist == [YyNn] ]] && break
done
echo ""
if [[ $wwwexist == [nN] ]]; then
sudo rm -rf /var/www/$domain/htdocs
sudo mkdir -p /var/www/$domain/htdocs
[[ "$wp" == [123] ]] && wpinstall
fi
fi
fi
# Activate FastCgi cache
if [[ "$cache" == "-cache" && "$wp" == [123] ]]; then
sudo sed -i '/php.conf/c \ include common/wpfc.conf;' /etc/nginx/sites-available/$domain
wp_cache_plugins
fi
sudo chown -R www-data:www-data /var/www
[[ $(conf_read login-www-data) == "true" ]] && sudo chown root:root /var/www
echo "${gre}Site${blu} $domain ${gre}has been successfully created!${end}"
}
force_redirect() {
if ! [[ $value =~ ^(www|root|off)$ ]]; then
echo "${red}Please, enter a valid argument!${end}"
exit 1
fi
if [[ $subdomflag == 1 ]]; then
echo "${red}Please, use the main/root domain, Force-Redirection should not be used in Subdomains!${end}"
exit 1
fi
local rediron=$( grep -F "WebinolyWWWredirectStart" /etc/nginx/sites-available/$domain )
local sername="server_name $domain www.$domain;"
cp /opt/webinoly/templates/template-site-wwwredirect /tmp/template-site-wwwredirect
case $value in
"www")
[[ -n $rediron ]] && sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
sudo sed -i "/#server_name;/c \ server_name $domain;" /tmp/template-site-wwwredirect
sudo sed -i "/#return 301;/c \ return 301 \$scheme://www.${domain}\$request_uri;" /tmp/template-site-wwwredirect
sudo sed -i "/server_name /c \ server_name www.$domain;" /etc/nginx/sites-available/$domain
echo "${gre}Force-Redirection to WWW has been successfully enabled!${end}"
;;
"root")
[[ -n $rediron ]] && sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
sudo sed -i "/#server_name;/c \ server_name www.$domain;" /tmp/template-site-wwwredirect
sudo sed -i "/#return 301;/c \ return 301 \$scheme://${domain}\$request_uri;" /tmp/template-site-wwwredirect
sudo sed -i "/server_name /c \ server_name $domain;" /etc/nginx/sites-available/$domain
echo "${gre}Force-Redirection to non-WWW has been successfully enabled!${end}"
;;
"off")
if [[ -n $rediron ]]; then
sudo sed -i '/WebinolyWWWredirectStart/,/WebinolyWWWredirectEnd/{/.*/d}' /etc/nginx/sites-available/$domain
sudo sed -i "/server_name /c \ $sername" /etc/nginx/sites-available/$domain
echo "${gre}Force-Redirection has been successfully disabled!${end}"
else
echo "${red}Force-Redirect is already disabled for site${blu} $domain ${end}"
fi
;;
esac
# If SSL is enabled insert after that - First redirect should be to HTTPS due to HSTS.
isssl=$( grep -F "ssl on;" /etc/nginx/sites-available/$domain )
if [[ -z $isssl && $value =~ ^(www|root)$ ]]; then
sudo sed -i '1r /tmp/template-site-wwwredirect' /etc/nginx/sites-available/$domain
elif [[ -n $isssl && $value =~ ^(www|root)$ ]]; then
sudo sed -i '/listen 80/c \ listen 443 ssl http2;' /tmp/template-site-wwwredirect
sudo sed -i '/listen \[::\]:80/c \ listen [::]:443 ssl http2;' /tmp/template-site-wwwredirect
if [[ $cache != "-ignore-ssl" ]]; then
sudo sed -i '/headers-http.conf/a \ include common/headers-https.conf;' /tmp/template-site-wwwredirect
sudo sed -i '/server_name /r /opt/webinoly/templates/template-site-ssl' /tmp/template-site-wwwredirect
sudo sed -i "/WebinolySSLstart/,/WebinolySSLend/{s/domain.com/$domain/}" /tmp/template-site-wwwredirect
fi
sudo sed -i "/WebinolySSLredirectStart/,/WebinolySSLredirectEnd/s/\(server_name\).*$/$sername/" /etc/nginx/sites-available/$domain
sudo sed -i '/WebinolySSLredirectEnd/r /tmp/template-site-wwwredirect' /etc/nginx/sites-available/$domain
fi
sudo rm /tmp/template-site-wwwredirect
}