-
Notifications
You must be signed in to change notification settings - Fork 0
/
web_server.sh
519 lines (411 loc) · 11.7 KB
/
web_server.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
514
515
516
517
518
519
#!/bin/bash
pause(){
read -p "$*"
}
install_package(){
local _PACKAGE_NAME=$1
# Check if firewall Installed
if !(dpkg -s $_PACKAGE_NAME | grep -q 'Status: install ok installed'); then
echo "---> Installing $_PACKAGE_NAME"
apt -qq install $_PACKAGE_NAME
fi
}
configure_firewall(){
local _WEB_SERVER=$1
local _HTTP_AUTH=$2
echo "---> Configuring Firewall entries for $_WEB_SERVER with HTTP_AUTH=$_HTTP_AUTH"
# Check if firewall Installed
install_package "ufw"
#ufw reset
echo "---> Adding Firewall rules"
# Check if OpenSSH authorized
if !(ufw status | grep -q 'OpenSSH'); then
ufw allow "OpenSSH"
fi
if [[ "$_WEB_SERVER" == "Apache" ]]; then
if $_HTTP_AUTH;
then
ufw allow "WWW Full";
ufw delete allow "WWW Secure";
else
ufw allow "WWW Secure";
ufw delete allow "WWW Full";
fi
fi
if [[ "$_WEB_SERVER" == "Nginx" ]]; then
if $_HTTP_AUTH;
then
ufw allow "Nginx Full";
ufw delete allow "Nginx HTTPS";
else
ufw allow "Nginx HTTPS";
ufw delete allow "Nginx Full";
fi
fi
# Check if firewall active
if !(ufw status | grep -q 'Status: active'); then
echo "---> Activating Firewall"
ufw enable
fi
ufw status
}
install_apache(){
local _APACHE_CONFIG="/etc/apache2/apache2.conf"
echo "---> Start installing Apache"
install_package "apache2"
#apt -qq install apache2
if systemctl status apache2 | grep -q 'Active: active (running)'; then
echo "---> Server Running"
fi
}
install_nginx(){
local _NGINX_CONFIG="/etc/nginx/nginx.conf"
echo "---> Start installing NGINX"
install_package "nginx"
#apt -qq install nginx
if systemctl status nginx | grep -q 'Active: active (running)'; then
echo "---> Server Running"
fi
echo "---> Editing config file"
local search="# server_names_hash_bucket_size 64;"
local replace="server_names_hash_bucket_size 64;"
sed -i "s/${search}/${replace}/g" $_NGINX_CONFIG
if nginx -t | grep -q 'syntax is ok'; then
echo "---> Config file OK"
systemctl restart nginx
fi
}
add_nginx_server_block(){
local _DOMAIN=$1
local _PORT=$2
local _ENV=$3
local SITES_AVAILABLE=/etc/nginx/sites-available/
local SITES_ENABLED=/etc/nginx/sites-enabled/
local SERVER_BLOCK=$SITES_AVAILABLE$_DOMAIN
local domain_string="<<DOMAIN>>"
local port_string="<<PORT>>"
local path_string="<<ROOT>>"
FILE_PATH=/var/www/$_DOMAIN
echo "---> Creating folder"
mkdir -p $FILE_PATH
echo "---> Setting folder security"
chown -R $CURRENT_USER:$CURRENT_USER $FILE_PATH
chmod -R 755 $FILE_PATH
echo "---> Adding server block"
if [[ "$ENV" == "NodeJS" ]];
then
cp "./config/Server_Blocks/Nginx_Proxy" $SERVER_BLOCK
sed -i "s/${domain_string}/${_DOMAIN}/g" $SERVER_BLOCK
sed -i "s/${port_string}/${_PORT}/g" $SERVER_BLOCK
sed -i "s/${path_string}/${FILE_PATH}/g" $SERVER_BLOCK
else
cp "./config/Server_Blocks/Nginx" $SERVER_BLOCK
sed -i "s/${domain_string}/${_DOMAIN}/g" $SERVER_BLOCK
sed -i "s/${path_string}/${FILE_PATH}/g" $SERVER_BLOCK
fi
echo "---> Enabling Server Block"
ln -sf $SERVER_BLOCK $SITES_ENABLED
if nginx -t | grep -q 'syntax is ok'; then
echo "---> Config file OK"
echo "---> Restarting Nginx"
systemctl restart nginx
fi
}
add_apache_server_block(){
local _DOMAIN=$1
local _PORT=$2
local _ENV=$3
local SITES_AVAILABLE=/etc/apache2/sites-available/
local SITES_ENABLED=/etc/apache2/sites-enabled/
local domain_string="<<DOMAIN>>"
local port_string="<<PORT>>"
local path_string="<<ROOT>>"
FILE_PATH=/var/www/$_DOMAIN
echo "---> Creating folder"
mkdir -p $FILE_PATH
echo "---> Setting folder security"
chown -R $CURRENT_USER:$CURRENT_USER $FILE_PATH
chmod -R 755 $FILE_PATH
echo "---> Adding server block"
if [[ "$ENV" == "NodeJS" ]];
then
a2enmod proxy
a2enmod proxy_html
a2enmod proxy_http
cp "./config/Server_Blocks/Apache_Proxy" $SERVER_BLOCK
sed -i "s/${domain_string}/${_DOMAIN}/g" $SERVER_BLOCK
sed -i "s/${port_string}/${_PORT}/g" $SERVER_BLOCK
sed -i "s/${path_string}/${FILE_PATH}/g" $SERVER_BLOCK
else
cp "./config/Server_Blocks/Apache" $SERVER_BLOCK
sed -i "s/${domain_string}/${_DOMAIN}/g" $SERVER_BLOCK
sed -i "s/${path_string}/${FILE_PATH}/g" $SERVER_BLOCK
fi
echo "---> Enabling Serer Block"
a2ensite $_DOMAIN.conf
a2dissite 000-default.conf
if apache2ctl configtest | grep -q 'Syntax OK'; then
echo "---> Config file OK"
systemctl restart apache2
fi
}
install_NodeJS(){
local _Node_Version="12.x"
echo "---> Installing NodeJS"
#apt -qq install curl software-properties-common
install_package "curl"
install_package "software-properties-common"
curl -sL https://deb.nodesource.com/setup_$_Node_Version | sudo bash -
#apt -qq install nodejs
install_package "nodejs"
echo "---> Installed Node Version :" | node -v
echo "---> Installed NPM version :" | npm -v
}
launch_NodeJS(){
local _PROJECT_PATH=$1
echo "---> Navigating to $_PROJECT_PATH"
cd $_PROJECT_PATH
echo "---> Installing Dependecies"
#npm install -g nodemon
npm install -g forever
npm install
echo "---> Starting NodeJS app"
forever start app.js
}
install_Wordpress(){
local _FILE_PATH=$1
local _TEMP_PATH="/tmp"
#echo "---> Installing MariaDB"
install_package "mariadb-server"
install_package "mariadb-client"
#apt -qq install mariadb-client mariadb-server
local _DB_NAME="default"
local _USERNAME="wordpress"
local _PASSWORD="password"
echo "---> Setting up configuration :"
read -p "Enter a database name :" _DB_NAME
read -p "Enter a username :" _USERNAME
read -s -p "Enter a password :" _PASSWORD
echo
echo "---> Configuring Database"
mysql -u root -p -e "CREATE DATABASE IF NOT EXISTS $_DB_NAME; CREATE USER IF NOT EXISTS '$_USERNAME'@'localhost' IDENTIFIED BY '$_PASSWORD'; GRANT ALL PRIVILEGES ON $_DB_NAME.* TO '$_USERNAME'@'localhost'; FLUSH PRIVILEGES;"
#echo "---> Installing PHP"
install_package "php7.0"
install_package "php7.0-mysql"
install_package "libapache2-mod-php7.0"
#apt -qq install php7.0 php7.0-mysql
#apt -qq install libapache2-mod-php7.0
#echo "---> Installing PHP MY ADMIN"
#apt -qq install phpmyadmin
install_package "phpmyadmin"
echo "---> Downloading latest Wordpress Version"
wget -P $_TEMP_PATH/ https://wordpress.org/latest.tar.gz
echo "---> Uncompressing wordpress archive"
tar xzf $_TEMP_PATH/latest.tar.gz -C $_TEMP_PATH/
rm $_TEMP_PATH/latest.tar.gz
echo "---> Deleting target folder content"
rm -rf $_FILE_PATH/{,.[!.],..?}*
echo "---> Copying wordpress sources to target folder"
cp -r $_TEMP_PATH/wordpress/. $_FILE_PATH/
rm -Rf $_TEMP_PATH/wordpress/
echo "---> Setting up wp-config.php"
mv $_FILE_PATH/wp-config-sample.php $_FILE_PATH/wp-config.php
#GET SALTS
WPSalts=$(wget https://api.wordpress.org/secret-key/1.1/salt/ -q -O -)
TablePrefx=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 9 | head -n 1)_
cat <<EOF > $_FILE_PATH/wp-config.php
<?php
/***Managed by JEEBIE.ME - Benjamin Fourgeaud***/
define('DB_NAME', '$_DB_NAME');
define('DB_USER', '$_USERNAME');
define('DB_PASSWORD', '$_PASSWORD');
define('DB_HOST', 'localhost');
define('DB_CHARSET', 'utf8');
define('DB_COLLATE', '');
/*WP Tweaks*/
#define( 'WP_SITEURL', '' );
#define( 'WP_HOME', '' );
#define( 'ALTERNATE_WP_CRON', true );
#define('DISABLE_WP_CRON', 'true');
#define('WP_CRON_LOCK_TIMEOUT', 900);
#define('AUTOSAVE_INTERVAL', 300);
#define( 'WP_MEMORY_LIMIT', '256M' );
#define( 'FS_CHMOD_DIR', ( 0755 & ~ umask() ) );
#define( 'FS_CHMOD_FILE', ( 0644 & ~ umask() ) );
#define( 'WP_ALLOW_REPAIR', true );
#define( 'FORCE_SSL_ADMIN', true );
#define( 'AUTOMATIC_UPDATER_DISABLED', true );
#define( 'WP_AUTO_UPDATE_CORE', false );
$WPSalts
\$table_prefix = '$TablePrefx';
define('WP_DEBUG', false);
if ( !defined('ABSPATH') )
define('ABSPATH', dirname(__FILE__) . '/');
require_once(ABSPATH . 'wp-settings.php');
EOF
echo "---> Setting folder security"
chown -R $CURRENT_USER:$CURRENT_USER $_FILE_PATH
chmod -R 755 $_FILE_PATH
}
install_Static(){
local _FILE_PATH=$1
cp "./config/index.html" "$_FILE_PATH/index.html"
}
clone_github(){
local _FILE_PATH=$1
local _GITHUB_LINK=""
echo "---> Setting-up GitHub"
read -p "Enter GitHub clone link :" _GITHUB_LINK
echo "---> Deleting folder content"
rm -rf $_FILE_PATH/{,.[!.],..?}*
echo "---> Cloning to $_FILE_PATH"
cd $_FILE_PATH
git clone $_GITHUB_LINK .
}
setup_ssl(){
local _WEB_SERVER=$1
local _DOMAIN=$2
local MIRROR1="deb http://deb.debian.org/debian stretch-backports main contrib non-free"
local MIRROR2="deb-src http://deb.debian.org/debian stretch-backports main contrib non-free"
local MIRROR_FILE="/etc/apt/sources.list"
echo "---> Setting up SSL"
echo "---> Add up-to-date mirrors"
if !(grep -Fxq "$MIRROR1" "$MIRROR_FILE"); then echo $MIRROR1 >> $MIRROR_FILE; fi
if !(grep -Fxq "$MIRROR2" "$MIRROR_FILE"); then echo $MIRROR2 >> $MIRROR_FILE; fi
echo "---> update mirrors"
apt -qq update
if [[ "$_WEB_SERVER" == "Apache" ]];
then
echo "---> Install Certbot"
apt -qq install python-certbot-apache -t stretch-backports
echo "---> Obtaining Certificate"
certbot --apache -d $_DOMAIN -d www.$_DOMAIN
echo "---> Restarting Apache"
systemctl restart apache2
fi
if [[ "$_WEB_SERVER" == "Nginx" ]];
then
echo "---> Install Certbot"
apt -qq install python-certbot-nginx -t stretch-backports
echo "---> Obtaining Certificate"
certbot --nginx -d $_DOMAIN -d www.$_DOMAIN
echo "---> Restarting Nginx"
systemctl restart nginx
fi
echo "---> Test auto-renawal"
certbot renew --dry-run
}
update(){
#Update and Upgrade packages
echo "---> Updating and Upgrading Packages"
apt -qq update && apt -qq upgrade
}
### MAIN PROGRAMM ###
# Default values
INSTALL=false
ADD=false
SSL=false
GITHUB=false
HTTP_AUTH=true
WEB_SERVER=""
ENV=""
FILE_PATH="/var/www/default"
CURRENT_USER=$(who | awk 'NR==1{print $1}')
PORT=8080
# Loop through arguments and process them
while [ -n "$1" ]; do # while loop starts
case "$1" in
-i|--install)
INSTALL=true
WEB_SERVER=$2
echo "Installing new web server ..."
if [[ "$WEB_SERVER" == "Apache" ]]; then install_apache; fi
if [[ "$WEB_SERVER" == "Nginx" ]]; then install_nginx; fi
break;;
-up|--update-mirrors)
update
break;;
-uf|--update-firewall) ## TODO : HANDLE
WEB_SERVER=$2
HTTP_AUTH=$3
configure_firewall $WEB_SERVER $HTTP_AUTH
break;;
-as|--add-site)
ADD=true
ENV=$2
shift;;
-ssl|--secure)
SSL=$2
shift;;
-d|--domain)
DOMAIN=$2
shift;;
-g|--github)
GITHUB=$2
shift;;
-p|--port)
PORT=$2
shift;;
-mg|--mongoose)
MONGOOSE=$2
shift;;
--module)
MODULES+=($2)
shift;;
--template)
TEMPLATES+=($2)
shift;;
-s|--server)
WEB_SERVER=$2
shift;;
-e|--env)
ENV=$2
shift;;
*)
echo "Option $1 not recognized" ;;
esac
shift
done
if $ADD;
then
echo "Adding new web instance ..."
echo "---> WebServer : $WEB_SERVER"
echo "---> Domain : $DOMAIN"
echo "---> Port : $PORT"
echo "---> SSL : $SSL"
echo "---> GITHUB : $GITHUB"
echo "---> ENVIRONNEMENT : $ENV"
if [[ "$WEB_SERVER" == "Apache" ]]; then add_apache_server_block $DOMAIN $PORT $ENV; fi
if [[ "$WEB_SERVER" == "Nginx" ]]; then add_nginx_server_block $DOMAIN $PORT $ENV; fi
if $GITHUB;
then
clone_github $FILE_PATH
fi
if [[ "$ENV" == "NodeJS" ]];
then
install_NodeJS;
if $GITHUB;
then
clone_github $FILE_PATH
launch_NodeJS $FILE_PATH;
fi
fi
if [[ "$ENV" == "Wordpress" ]];
then
install_Wordpress $FILE_PATH;
fi
if [[ "$ENV" == "Static" ]];
then
if $GITHUB;
then
clone_github $FILE_PATH
else
install_static $FILE_PATH;
fi
fi
if $SSL;
then
setup_ssl $WEB_SERVER $DOMAIN
fi
fi
echo