/
lampi
executable file
·473 lines (437 loc) · 13.4 KB
/
lampi
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
#!/bin/bash
p_name=lampi
p_author='Md Jahidul Hamid'
p_source='https://github.com/neurobin/lampi'
p_bugt="$p_source/issues"
p_version=0.2.0
ver_info="
Name : $p_name
Version : $p_version
Author : $p_author
Source : $p_source
Bug tracker : $p_bugt
"
help="
############################## lampi #####################################
################### LAMP stack installer for Ubuntu ######################
##########################################################################
# Usage:
# lampi [-n, -dr, -i, -ri, -u, -s, -so, -rm, -rmd, -nfo, -nsr, -nip, -f -nu, -v, -h]
#
# Run simply lampi -i to install the LAMP stack
# with default configs
#
# Options:
# -n | --name | server name (default localhost)
# -dr | --doc-root | arbitrary document root
# -i | --install | install LAMP
# -ri | --reinstall | reinstall LAMP
# -u | --uninstall | uninstall LAMP
# -s | --ssl | enable SSL
# -rm | --remove | remove a site
# -rmd | --remove-with-dir | remove a site and it's directory
# -f | --force | force
# -nu | --no-update | do not run apt-get update
# -so | --ssl-only | configure for https site only
# -npa | --no-php-myadmin | no php my admin
# -nsr | --no-server-restart | no server restart
# -nfo | --no-info | do not create info.php
# -nip | --no-ip | no ip, i.e use * instead of 127.0.0.1
# -v | --version | show version info
# -h | --help | show help
##########################################################################
"
dr=''
ssl=false
ssl_only=false
no_update=false
install=false
localhost=localhost
uninstall=false
reinstall=false
remove=
force=false
rmd=false
npa=false
nsr=false
nfo=false
nip=false
for op in "$@";do
case "$op" in
-v|--version)
echo "$ver_info"
shift
exit 0
;;
-h|--help)
echo "$help"
shift
exit 0
;;
-dr|--doc-root)
shift
if [[ "$1" != "" ]]; then
dr="${1/%\//}"
shift
else
echo "E: Arg missing for -dr option"
exit 1
fi
;;
-s|--ssl)
ssl=true
shift
;;
-so|--ssl-only)
ssl=true
ssl_only=true
shift
;;
-nu|--no-update)
no_update=true
shift
;;
-npa|--no-php-myadmin)
npa=true
shift
;;
-nsr|--no-server-restart)
nsr=true
shift
;;
-nip|--no-ip)
nip=true
shift
;;
-i|--install)
install=true
reinstall=false
uninstall=false
shift
;;
-ri|--reinstall)
install=true
reinstall=true
uninstall=false
shift
;;
-u|--uninstall)
install=false
reinstall=false
uninstall=true
shift
;;
-f|--force)
force=true
shift
;;
-nfo|--no-info)
nfo=true
shift
;;
-rm|--remove)
shift
if [[ "$1" != "" ]]; then
remove="$1"
shift
else
echo "E: Arg missing for -rm option"
exit 1
fi
;;
-rmd|--remove-with-dir)
shift
if [[ "$1" != "" ]]; then
remove="$1"
rmd=true
shift
else
echo "E: Arg missing for -rm option"
exit 1
fi
;;
-n|--name)
shift
if [[ "$1" != "" ]]; then
localhost="$1"
shift
else
echo "E: Arg missing for -n option"
exit 1
fi
;;
-*)
echo "E: Invalid option: $1"
shift
exit 1
;;
esac
done
[ "$dr" = "" ] && dr='/var/www/html'
opts="
***** Passed options *****
DocumentRoot : $dr
ServerName : $localhost
SSL : $ssl
SSL only : $ssl_only
Install : $install
Uninstall : $uninstall
Reinstall : $reinstall
NoUpdate : $no_update
NoInfo : $nfo
NoIP : $nip
"
#echo "$opts"
#check for root
if [ $EUID -ne 0 ]; then
printf "\n\tRoot privilege required\n"
printf "\tSorry! Exiting\n\n"
exit 1
fi
user=www-data
if [ ! -z "$SUDO_USER" ]; then
user="$SUDO_USER"
fi
mkdir -p "$dr"
chown $user:$user "$dr"
chmod 755 "$dr"
#extract dist version
rel=(`lsb_release -r | grep -o '[0-9]\+'`)
rv=${rel[0]}
tphp='<?php phpinfo(); ?>'
endm="
****** Successfully reloaded apache2 ******
"
aconf=/etc/apache2/apache2.conf
if [ -n "$remove" ]; then
a2dissite $remove
remove_file=/etc/apache2/sites-available/"$remove"
file=/etc/hosts
pat="^\([[:blank:]]*127\.0\.0\.1.*\)[[:blank:]]$remove\b\(.*\)$"
while grep -e "$pat" "$file" >/dev/null 2>&1;do
sed -i.bak -e "s/$pat/\1\2/" "$file"
done
if [ -f "$remove_file.conf" ]; then
rm_dir="$(sed -n -e 's/^[[:blank:]]*DocumentRoot[[:blank:]]*\([^#]*\).*/\1/p' "$remove_file.conf")"
if $rmd && ! test -z "$rm_dir" && [ "." != "$rm_dir" ] && [ '..' != "$rm_dir" ]; then
rm -r "$rm_dir"
fi
fi
rm "$remove_file.conf" "$remove_file.conf.bak"
if $ssl; then
a2dissite $remove-ssl
rm "$remove_file-ssl.conf" "$remove_file-ssl.conf.bak"
fi
else
if $install || $uninstall; then
#update
if ! $no_update && ! $uninstall;then apt-get update;fi
#apache2
list=(
'apache2'
)
#php
php='mcrypt'
if (( $rv <= 15 ));then
php="$php php5 libapache2-mod-php5 php5-mcrypt php5-cgi php5-cli php5-common php5-curl php5-gd"
elif (( $rv >= 16 ));then
php="$php php libapache2-mod-php php-mcrypt php-cgi php-cli php-common php-curl php-gd"
fi
list+=("$php")
# mysql
mysql='mysql-server mysql-client'
if (( $rv <= 15 ));then
mysql="$mysql php5-mysql"
elif (( $rv >= 16 ));then
mysql="$mysql php-mysql"
fi
list+=("$mysql")
#phpmyadmin
if ! $npa; then
phpmd='phpmyadmin apache2-utils'
else
phpmd=''
fi
if (( $rv >= 16 ));then
phpmd="$phpmd php-gettext php-mbstring"
fi
list+=("$phpmd")
for app in "${list[@]}";do
if ! $uninstall; then
echo "###### Installing: $app #######"
{ if ! $reinstall; then apt-get -y install $app;else apt-get --force-yes -y install --reinstall $app;fi; } ||
{
echo "W: Failed to install: $app"
[ "$app" = apache2 ] && exit 1
}
else
echo "###### Removing: $app #######"
apt-get -y purge $app
fi
done
if ! $npa; then
pdconf=/etc/phpmyadmin/apache.conf
pdincl="Include $pdconf"
if ! grep -s -e "$pdincl" "$aconf" >/dev/null 2>&1; then
sed -i.bak "$ a $pdincl" "$aconf" &&
printf "\n\t apache2 conf modified to include phpmyadmin\n" ||
printf "\n\tE: failed to modify apache2 conf for phpmyadmin\n"
else
printf "\n\tphpmyadmin is already added in apache2 conf (skipped)\n"
fi
fi
apt-get autoremove
if ! $uninstall; then
if (( $rv <= 15 )); then
php5enmod mcrypt
elif (( $rv >= 16 )); then
phpenmod mcrypt
phpenmod mbstring
fi
fi
fi
#if uninstall exit
if $uninstall;then echo "Removed LAMP installation (custom sites are not removed)."; exit 0;fi
#create info.php
if ! $nfo; then
[ ! -f "$dr"/info.php ] && echo "$tphp" > "$dr"/info.php && printf "\n\tinfo.php created\n" || printf "\n\tinfo.php exists (skipped)\n"
chown $user:$user "$dr"/info.php
#chmod 640 "$dr"/info.php
fi
dire_opts="\n\t\tAllowOverride All\n\t\tOptions Indexes FollowSymLinks MultiViews\n\t\tRequire all granted\n\t"
dire="\t<Directory $dr>$dire_opts</Directory>\n"
#custom sites
sites_av=/etc/apache2/sites-available
ds="$(find "$sites_av" -name '*default.conf')"
ds_ssl="$(find "$sites_av" -name '*default*ssl*.conf')"
dsn="$sites_av/$localhost.conf"
ds_ssln="$sites_av/$localhost-ssl.conf"
if [ "$ds" = "$dsn" ]; then
printf "$ds\m\t\tand\n$dsn\n\t\tcan not be same\nE: ServerName: $(basename "$ds") not allowed\n"
exit 1
elif [ "$ds_ssl" = "$ds_ssln" ]; then
printf "$ds_ssl\m\t\tand\n$ds_ssln\n\t\tcan not be same\nE: ServerName: default not allowed\n"
exit 1
fi
export localhost
export dr
export dire
copy_default_config(){
if ! $nip; then
ip='127.0.0.1'
else
ip='*'
fi
sed -e "s=^\([[:blank:]]*DocumentRoot\).*$=\1 $dr="\
-e "s=^\([[:blank:]]*ServerName\).*$=\1 $localhost="\
-e "s=^\([[:blank:]]*<[[:blank:]]*Directory[[:blank:]]*\)[^[:blank:]]\+\([[:blank:]]*>[[:blank:]]*\)$=\1$dr\2="\
-e "s=^\([[:blank:]]*<[[:blank:]]*VirtualHost[[:blank:]]*\)[^:]*\(:[^>]*>[[:blank:]]*\)$=\1 $ip\2="\
"$1" > "$2" &&
printf "\n\tcopied $1 --> $2 with required modifications\n" ||
printf "\n\tE: Failed to copy $1 --> $2\n"
}
if ! $ssl_only; then
copy_default_config "$ds" "$dsn"
fi
#no else
if $ssl; then
copy_default_config "$ds_ssl" "$ds_ssln"
fi
insert_server_property(){
if ! grep -s -e "^\([[:blank:]]*$1\).*$" "$2" >/dev/null 2>&1; then
sed -i.bak -e "s=^[[:blank:]]*<[[:blank:]]*VirtualHost[[:blank:]]*.*>[[:blank:]]*$=&\n\tServerName $localhost=" "$2" &&
printf "\n\tSuccessfully inserted $1 to $2\n" ||
printf "\n\tE: Failed to insert $1 to $2\n"
else
printf "\n\t$1 exists in $2 (skipped)\n"
fi
}
if ! $ssl_only; then
insert_server_property ServerName "$dsn"
fi
if $ssl; then
insert_server_property ServerName "$ds_ssln"
fi
insert_docroot_property(){
if ! grep -s -e "^[[:blank:]]*<[[:blank:]]*Directory[[:blank:]]*[^[:blank:]]\+[[:blank:]]*>[[:blank:]]*$" "$1" >/dev/null 2>&1; then
sed -i.bak -e "s=^\([[:blank:]]*\)DocumentRoot.*$=&\n$dire=" "$1" &&
printf "\n\tSuccessfully inserted DocumentRoot in $1\n" ||
printf "\n\tE: Failed to insert DocumentRoot in $1\n"
else
cat "${1-x}" > "${1-x}.bak" 2>&1 &&
printf "\n\tBack up: ${1-x} succeded\n" ||
printf "\n\tE: Back up: ${1-x} failed\n"
echo "$(awk '
BEGIN {p=1}
/^[[:blank:]]*<[[:blank:]]*Directory[[:blank:]]*[^[:blank:]]+[[:blank:]]*>[[:blank:]]*/ {print;print "'"$dire_opts"'";p=0}
/[^#]*<\/Directory/ {p=1}
p' "${1-x}")" > "${1-x}" &&
printf "\n\tSuccessfully edited ${1-x}\n" ||
printf "\n\tE: Failed to edit ${1-x}\n"
fi
}
if ! $ssl_only; then
insert_docroot_property "$dsn"
fi
if $ssl; then
insert_docroot_property "$ds_ssln"
fi
#manage /etc/hosts
pat='^[[:blank:]]*127\.0\.0\.1[[:blank:]]\+.*$'
if grep "$pat" /etc/hosts >/dev/null 2>&1; then
sed -i.bak -e "s/$pat/& $localhost/" /etc/hosts >/dev/null &&
printf "\n\tAdded $localhost to /etc/hosts\n" ||
printf "\n\tE: Failed to add $localhost to /etc/hosts\n"
else
sed -i.bak -e "1 a 127.0.0.1 $localhost" /etc/hosts >/dev/null &&
printf "\n\tAdded $localhost to /etc/hosts\n" ||
printf "\n\tE: Failed to add $localhost to /etc/hosts\n"
fi
#enable mods
a2enmod rewrite &&
printf "\n\tEnabled mod_rewrite\n" ||
printf "\n\tE: Failed to enable mod_rewrite\n"
if $ssl; then
a2enmod ssl &&
printf "\n\tEnabled mod_ssl\n" ||
printf "\n\tE: Failed to enable mod_ssl\n"
fi
#enable sites
if ! $ssl_only; then
a2ensite "$localhost" &&
printf "\n\tEnabled $localhost\n" ||
printf "\n\tE: Failed to enable $localhost\n"
fi
if $ssl; then
a2ensite "$localhost"-ssl &&
printf "\n\tEnabled $localhost-ssl\n" ||
printf "\n\tE: Failed to enable $localhost-ssl\n"
fi
if ! $nfo; then
info=info.php
else
info=
fi
endm="$endm
******* Visit Site *******
"
if ! $ssl_only; then
endm="${endm}http://$localhost/$info
"
fi
if $ssl; then
endm="${endm}https://$localhost/$info
"
fi
fi
#restart the apache2 service
if ! $nsr; then
{
service apache2 restart ||
systemctl restart apache2
} &&
echo "$endm" ||
echo "\n\tE: Failed to restart apache2\n"
else
echo "$endm after restarting the server."
fi