forked from Keith2/lowendscript-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup-wheezy.sh
executable file
·1579 lines (1437 loc) · 47.8 KB
/
setup-wheezy.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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
function check_install {
if [ -z "`which "$1" 2>/dev/null`" ]
then
DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -q -y $3 install $2
print_info "$2 installed for $1"
else
print_warn "$2 already installed"
fi
}
function check_remove {
if [ -n "`which "$1" 2>/dev/null`" ]
then
DEBIAN_FRONTEND=noninteractive apt-get -q -y remove --purge "$2"
print_info "$2 removed"
else
print_warn "$2 is not installed"
fi
}
function check_upgrade {
if [ -z "`which "$1" 2>/dev/null`" ]
then
print_warn "$2 not installed for $1"
else
DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -q -y $3 install $2
print_warn "$2 upgrade"
fi
}
function check_sanity {
# Do some sanity checking.
if [ $(/usr/bin/id -u) != "0" ]
then
die 'Must be run by root user'
fi
if [ -f /etc/lsb-release ]
then
die "Distribution is not supported"
fi
if [ ! -f /etc/debian_version ]
then
die "Ubuntu is not supported"
fi
}
function die {
echo "ERROR: $1" > /dev/null 1>&2
exit 1
}
function get_domain_name() {
# Getting rid of the lowest part.
domain=${1%.*}
lowest=`expr "$domain" : '.*\.\([a-z][a-z]*\)'`
case "$lowest" in
com|net|org|gov|edu|co)
domain=${domain%.*}
;;
esac
lowest=`expr "$domain" : '.*\.\([a-z][a-z]*\)'`
[ -z "$lowest" ] && echo "$domain" || echo "$lowest"
}
function get_password() {
# Check whether our local salt is present.
SALT=/var/lib/radom_salt
if [ ! -f "$SALT" ]
then
head -c 512 /dev/urandom > "$SALT"
chmod 400 "$SALT"
fi
password=`(cat "$SALT"; echo $1) | md5sum | base64`
echo ${password:0:13}
}
function install_dash {
check_install dash "dash"
rm -f /bin/sh
ln -s dash /bin/sh
}
function add_user {
if [ -z `grep $USER: /etc/passwd` ]; then
useradd -m $USER
cat >> /etc/sudoers.d/users <<END
$USER ALL=(ALL:ALL) ALL
END
if [ ! -d /home/$USER/Maildir ]; then
mkdir /home/$USER/Maildir
fi
if [ ! -d /home/$USER/Maildir/cur ]; then
mkdir /home/$USER/Maildir/cur
fi
if [ ! -d /home/$USER/Maildir/tmp ]; then
mkdir /home/$USER/Maildir/tmp
fi
if [ ! -d /home/$USER/Maildir/new ]; then
mkdir /home/$USER/Maildir/new
fi
chown -R $USER:$USER /home/$USER/Maildir
echo Set password for $USER
passwd $USER
fi
}
function install_dropbear {
check_upgrade ssh "ssh" "-t wheezy-backports"
check_remove dropbear "dropbear"
check_install /usr/sbin/xinetd "xinetd"
# Disable SSH
touch /etc/ssh/sshd_not_to_be_run
invoke-rc.d ssh stop
if [ -z $SSH_PORT ];then
SSH_PORT=22
print_info "SSH port set to 22"
else
if [ $SSH_PORT -le 65535 ]; then
print_info "SSH port set to $SSH_PORT"
else
SSH_PORT=22
print_warn "SSH port changed to 22"
fi
fi
# remove deprecated file
rm -f /etc/xinetd.d/dropbear
# Enable ssh start. We are going to use xinetd as it is just
# easier to configure and might be used for other things.
cat > /etc/xinetd.d/openssh <<END
service openssh
{
socket_type = stream
wait = no
port = $SSH_PORT
type = unlisted
flags = $FLAGS
user = root
protocol = tcp
server = /usr/sbin/sshd
server_args = -i
disable = no
}
END
invoke-rc.d xinetd restart
ssh-keygen -f /root/.ssh/id_rsa -N "" -t rsa -b 4096
ssh-keygen -f /root/.ssh/id_ed25519 -N "" -t ed25519
ssh-keygen -f /etc/ssh/ssh_host_ed25519_key -N "" -t ed25519
sed -i "/ssh_host_dsa_key/c#HostKey \/etc\/ssh\/ssh_host_dsa_key/" /etc/ssh/sshd_config
# sed -i "/ssh_host_rsa_key/c#HostKey \/etc\/ssh\/ssh_host_rsa_key/" /etc/ssh/sshd_config
sed -i "/ssh_host_ecdsa_key/c#HostKey \/etc\/ssh\/ssh_host_ecdsa_key/" /etc/ssh/sshd_config
if [ -z "`grep 'ssh_host_ed25519_key' /etc/ssh/sshd_config`" ];then
echo "HostKey /etc/ssh/ssh_host_ed25519_key" >>/etc/ssh/sshd_config
fi
}
function install_postfix {
check_install postfix "postfix procmail" "-t wheezy-backports"
cat > /etc/aliases <<END
postmaster: $EMAIL
MAILER-DAEMON: $EMAIL
abuse: $EMAIL
spam: $EMAIL
hostmaster: $EMAIL
root: $EMAIL
nobody: /dev/null
mail: $EMAIL
END
newaliases
postconf -e "mailbox_command = /usr/bin/procmail -a "$EXTENSION" DEFAULT=$HOME/Maildir/"
openssl gendh -out /etc/postfix/dh_512.pem -2 512
openssl gendh -out /etc/postfix/dh_1024.pem -2 1024
postconf -e "smtpd_tls_dh1024_param_file = /etc/postfix/dh_1024.pem"
postconf -e "smtpd_tls_dh512_param_file = /etc/postfix/dh_512.pem"
postconf -e "smtpd_tls_eecdh_grade = strong"
postconf -e "tls_preempt_cipherlist = yes"
postconf -e "smtpd_tls_loglevel = 1"
postconf -e "smtp_tls_loglevel = 1"
postconf -e "smtpd_tls_protocols = !SSLv2, !SSLv3"
postconf -e "smtp_tls_protocols = !SSLv2, !SSLv3"
postconf -e "smtpd_tls_received_header = yes"
postconf -e "smtpd_tls_security_level = may"
postconf -e "smtp_tls_security_level = may"
postconf -e "smtpd_tls_auth_only = yes"
postconf -e "smtp_tls_CAfile = /etc/ssl/certs/ca-certificates.crt"
service postfix reload
}
function install_percona {
apt-key adv --keyserver keys.gnupg.net --recv-keys 1C4CBDCDCD2EFD2A
cat > /etc/apt/sources.list.d/percona.list <<END
deb http://repo.percona.com/apt wheezy main
#deb-src http://repo.percona.com/apt wheezy main
END
apt-get update
# Install the Percona packages
check_install mysqld "percona-server-server-5.5"
check_install mysql "percona-server-client-5.5"
# Install a low-end copy of the my.cnf to disable InnoDB, and then delete
# all the related files.
invoke-rc.d mysql stop
rm -f /var/lib/mysql/ib*
cat > /etc/mysql/conf.d/lowendbox.cnf <<END
[mysqld]
key_buffer = 8M
query_cache_size = 0
ignore_builtin_innodb
default_storage_engine=MyISAM
END
invoke-rc.d mysql start
if [ ! -e ~/.my.cnf ]; then
# Generating a new password for the root user.
passwd=`get_password root@mysql`
mysqladmin password "$passwd"
cat > ~/.my.cnf <<END
[client]
user = root
password = $passwd
END
fi
chmod 600 ~/.my.cnf
}
function install_nginx {
wget -O - http://nginx.org/keys/nginx_signing.key | apt-key add -
cat > /etc/apt/sources.list.d/nginx.list <<END
deb http://nginx.org/packages/debian/ wheezy nginx
#deb-src http://nginx.org/packages/debian/ wheezy nginx
END
apt-get update
check_install nginx "nginx"
if [ ! -d /etc/nginx/ssl_keys ]; then
mkdir /etc/nginx/ssl_keys
fi
if [ ! -e /etc/nginx/ssl_keys/dhparam-2048.pem ]; then
openssl dhparam -out /etc/nginx/ssl_keys/dhparam-2048.pem 2048
fi
# Create a ssl default ssl certificate.
# This can be reused instead of creating a creating a self signed certificate.
if [ ! -e /etc/nginx/ssl_keys/default.pem ]; then
cat > /etc/nginx/ssl_keys/default.conf <<END
[req]
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
countryName = Country Name (2 letter code)
countryName_default = XX
countryName_min = 2
countryName_max = 2
commonName = Common Name (eg, YOUR name)
commonName_default = Default CA
commonName_max = 64
END
openssl genrsa -passout pass:password -des3 -out /etc/nginx/ssl_keys/default.key.secure 4096
openssl req -passin pass:password -new -x509 -key /etc/nginx/ssl_keys/default.key.secure -out /etc/nginx/ssl_keys/default.pem -days 3650 -config /etc/nginx/ssl_keys/default.conf -batch
openssl rsa -passin pass:password -in /etc/nginx/ssl_keys/default.key.secure -out /etc/nginx/ssl_keys/default.key
#openssl ecparam -out /etc/nginx/ssl_keys/default.ec.key -name secp521r1 -genkey
#openssl req -new -key /etc/nginx/ssl_keys/default.ec.key -x509 -nodes -days 3650 -out /etc/nginx/ssl_keys/default.ec.crt -config /etc/nginx/ssl_keys/default.ec.conf -batch
fi
cat > /etc/nginx/nginx.conf <<END
user www-data;
worker_processes $CPUCORES;
pid /run/nginx.pid;
events {
worker_connections 768;
# multi_accept on;
}
http {
##
# Basic Settings
##
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_names_hash_bucket_size 64;
ignore_invalid_headers on;
server_tokens off;
log_format main '\$remote_addr \$host \$server_port \$remote_user [\$time_local] "\$request" '
'\$status \$body_bytes_sent "\$http_referer" "\$http_user_agent" "\$http_x_forwarded_for"';
upstream php {
server unix:/var/run/php5-fpm.sock;
}
# server_name_in_redirect off;
include mime.types;
default_type application/octet-stream;
##
# Logging Settings
##
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log error;
##
# Gzip Settings
##
gzip on;
gzip_disable "msie6";
gzip_min_length 1400;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
ssl_certificate ssl_keys/default.pem;
ssl_certificate_key ssl_keys/default.key;
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains";
ssl_dhparam ssl_keys/dhparam-2048.pem;
ssl_session_timeout 5m;
ssl_session_cache shared:SSL:50m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-SHA384:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-SHA256:ECDHE-RSA-AES256-SHA:DHE-RSA-AES256-SHA:DHE-RSA-CAMELLIA256-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-SHA256:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:DHE-RSA-AES128-SHA:DHE-RSA-CAMELLIA128-SHA:HIGH:!aNULL;
ssl_prefer_server_ciphers on;
#fastcgi_cache_path /home/nginx-cache levels=1:2 keys_zone=CACHE:100m inactive=60m;
#fastcgi_cache_key "$scheme$request_method$host$request_uri";
client_max_body_size 8m;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
END
# Remove deprecated file
rm -f /etc/nginx/conf.d/lowendbox.conf
# Make sure sites-available & sites enabled exist
# Should only be needed when installing a cpu optimised nginx from my own repository.
if [ ! -d /etc/nginx/sites-available ]; then
mkdir /etc/nginx/sites-available
fi
if [ ! -d /etc/nginx/sites-enabled ]; then
mkdir /etc/nginx/sites-enabled
fi
cat > /etc/nginx/sites-available/default <<END
server {
END
if [ "$INTERFACE" = "all" ]; then
cat >> /etc/nginx/sites-available/default <<END
listen 80 default_server; ## listen for ipv4
listen 443 default_server ssl; ## listen for ipv4
listen [::]:80 default_server ipv6only=on; ## listen for ipv6
listen [::]:443 default_server ipv6only=on ssl; ## listen for ipv6
END
else
if [ "$INTERFACE" = "ipv6" ]; then
cat >> /etc/nginx/sites-available/default <<END
listen [::]:80 default_server; ## listen for ipv6
listen [::]:443 default_server ipv6only=on ssl; ## listen for ipv6
END
else
cat >> /etc/nginx/sites-available/default <<END
listen 80 default_server; ## listen for ipv4
listen 443 default_server ssl; ## listen for ipv4
END
fi
fi
cat >> /etc/nginx/sites-available/default <<END
server_name _;
access_log /var/log/nginx/default.log main;
ssl_ciphers "ALL:!aNULL:!RC4";
return 444;
}
END
cat > /etc/nginx/standard.conf <<END
location = /favicon.ico {
return 204;
log_not_found off;
access_log off;
}
location = /robots.txt {
log_not_found off;
access_log off;
}
# Make sure files with the following extensions do not get loaded by nginx because nginx would display the source code, and these files can contain PASSWORDS!
location ~* \.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)$|^(\..*|Entries.*|Repository|Root|Tag|Template)$|\.php_
{
return 444;
}
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
return 444;
access_log off;
log_not_found off;
}
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
END
cat > /etc/nginx/nophp.conf <<END
location ~* \.php\$ {
return 444;
}
END
cat > /etc/nginx/nocgi.conf <<END
location ~* \\.(pl|cgi|py|sh|lua)\$ {
return 444;
}
END
cat > /etc/nginx/disallow.conf <<END
location ~* (roundcube|webdav|smtp|http\\:|soap|w00tw00t) {
return 444;
}
if (\$http_user_agent ~* "(Morfeus|larbin|ZmEu|Toata|Huawei|talktalk)" ) {
return 444;
}
END
# delete deprecated file
rm -f /etc/nginx/disallow-agent.conf
invoke-rc.d nginx restart
chown www-data:adm /var/log/nginx/*
sed -i "s/rotate 52/rotate 1/" /etc/logrotate.d/nginx
}
function install_nginx-upstream {
wget -O - http://nginx.org/keys/nginx_signing.key | apt-key add -
cat > /etc/apt/sources.list.d/nginx.list <<END
deb http://nginx.org/packages/debian/ wheezy nginx
#deb-src http://nginx.org/packages/debian/ wheezy nginx
END
apt-get update
apt-get -y remove nginx nginx-full nginx-common
apt-get install nginx
sed -i "s/rotate 52/rotate 1/" /etc/logrotate.d/nginx
}
function install_php {
check_install php5-fpm "php5-fpm php5-cli php5-mysqlnd php5-cgi php5-gd php5-curl php5-xcache"
cat > /etc/nginx/fastcgi_php <<END
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
if (-f \$request_filename) {
fastcgi_pass php;
}
}
END
sed -i "/pm =/cpm = ondemand" /etc/php5/fpm/pool.d/www.conf
if [ "$MEMORY" = "low" ]; then
sed -i "/pm.max_children =/cpm.max_children = 1" /etc/php5/fpm/pool.d/www.conf
elif [ "$MEMORY" = "64" ]; then
sed -i "/pm.max_children =/cpm.max_children = 2" /etc/php5/fpm/pool.d/www.conf
elif [ "$MEMORY" = "96" ]; then
sed -i "/pm.max_children =/cpm.max_children = 3" /etc/php5/fpm/pool.d/www.conf
elif [ "$MEMORY" = "128" ]; then
sed -i "/pm.max_children =/cpm.max_children = 4" /etc/php5/fpm/pool.d/www.conf
elif [ "$MEMORY" = "192" ]; then
sed -i "/pm.max_children =/cpm.max_children = 6" /etc/php5/fpm/pool.d/www.conf
elif [ "$MEMORY" = "256" ]; then
sed -i "/pm.max_children =/cpm.max_children = 8" /etc/php5/fpm/pool.d/www.conf
elif [ "$MEMORY" = "384" ]; then
sed -i "/pm.max_children =/cpm.max_children = 12" /etc/php5/fpm/pool.d/www.conf
elif [ "$MEMORY" = "512" ]; then
sed -i "/pm.max_children =/cpm.max_children = 16" /etc/php5/fpm/pool.d/www.conf
fi
sed -i "/pm.max_requests =/cpm.max_requests = 500" /etc/php5/fpm/pool.d/www.conf
sed -i "/pm.status_path =/cpm.status_path = \/status" /etc/php5/fpm/pool.d/www.conf
sed -i "/listen =/clisten = /var/run/php5-fpm.sock" /etc/php5/fpm/pool.d/www.conf
sed -i "/listen.owner =/clisten.owner = www-data" /etc/php5/fpm/pool.d/www.conf
sed -i "/listen.group =/clisten.group = www-data" /etc/php5/fpm/pool.d/www.conf
sed -i "/listen.mode =/clisten.mode = 0666" /etc/php5/fpm/pool.d/www.conf
service php5-fpm restart
if [ -f /etc/init.d/php-cgi ];then
service php-cgi stop
update-rc.d php-cgi remove
rm /etc/init.d/php-cgi
service nginx restart
print_info "/etc/init.d/php-cgi removed"
fi
}
function install_cgi {
check_install fcgiwrap "fcgiwrap"
cat > /etc/nginx/fcgiwrap.conf <<END
location ~ (\.cgi|\.py|\.sh|\.pl|\.lua)$ {
gzip off;
root /var/www/\$server_name;
autoindex on;
fastcgi_pass unix:/var/run/fcgiwrap.socket;
include /etc/nginx/fastcgi_params;
fastcgi_param DOCUMENT_ROOT /var/www/\$server_name;
fastcgi_param SCRIPT_FILENAME /var/www/\$server_name\$fastcgi_script_name;
}
END
}
function install_domain {
if [ -z "$2" ]
then
die "Usage: `basename $0` domain <hostname>"
fi
if [ "$3" = "redo" ]; then
rm /etc/nginx/sites-available/$2.conf /etc/nginx/sites-enabled/$2.conf /var/www/$2/index.html
if [ -e /var/www/$2/index.sh ]; then
rm /var/www/$2/index.sh
fi
fi
if [ ! -d /var/www ]; then
mkdir /var/www
chown root:root /var/www
fi
if [ ! -d /var/www/$2 ]; then
mkdir /var/www/$2
fi
chown www-data:www-data /var/www/$2
cat > "/var/www/$2/index.html" <<END
<html><head>
<title>$2</title>
<meta name='description' content=$2>
<meta name='keywords' content=$2>
<meta http-equiv='Content-type' content='text/html;charset=UTF-8'>
<meta name='ROBOTS' content='INDEX, FOLLOW'>
</head><body>
<h1>It works!</h1>
<p>This is the default web page for $2</p>
<p>The web server software is running but no content has been added, yet.</p>
</body></html>
END
cat > "/var/www/$2/robots.txt" <<END
User-agent: *
Disallow: /
END
# Setting up Nginx mapping
cat > "/etc/nginx/sites-available/$2.conf" <<END
server {
listen 80;
END
if [ "$FLAGS" = "ipv6" ]; then
cat >> "/etc/nginx/sites-available/$2.conf" <<END
listen [::]:80;
END
fi
cat >> "/etc/nginx/sites-available/$2.conf" <<END
server_name www.$2;
return 301 http://$2\$request_uri;
}
server {
listen 80;
END
if [ "$FLAGS" = "ipv6" ]; then
cat >> "/etc/nginx/sites-available/$2.conf" <<END
listen [::]:80;
server_name $2;
END
else
cat >> "/etc/nginx/sites-available/$2.conf" <<END
server_name $2;
END
cat >> "/etc/nginx/sites-available/$2.conf" <<END
access_log /var/log/nginx/$2.log main;
include standard.conf;
# include fastcgi_php;
include nophp.conf;
# include fcgiwrap.conf;
include nocgi.conf;
include disallow.conf;
END
cat >> "/etc/nginx/sites-available/$2.conf" <<END
root /var/www/$2;
index index.html;
}
END
cat > "/etc/nginx/myips.conf" <<END
allow 127.0.0.1;
#deny all; # Used to restrict access to yourself for non-public areas of websites
# Uncomment the above line, comment the following line and add your allowed ip subnets after allow 127.0.0.1
allow all;
END
ln -s /etc/nginx/sites-available/$2.conf /etc/nginx/sites-enabled/$2.conf
invoke-rc.d nginx reload
fi
}
function install_iptables {
check_install iptables "iptables"
if [ -z "$1" ]
then
die "Usage: `basename $0` iptables <ssh-port-#>"
fi
KERNEL=`uname -r`
if [ ! -d /lib/modules/$KERNEL ]; then
mkdir /lib/modules/$KERNEL
depmod
fi
# Create startup rules
cat > /etc/init.d/iptables <<END
#! /bin/sh
#This is an Ubuntu adapted iptables script from gentoo
#(http://www.gentoo.org) which was originally distributed
#under the terms of the GNU General Public License v2
#and was Copyrighted 1999-2004 by the Gentoo Foundation
#
#This adapted version was intended for and ad-hoc personal
#situation and as such no warranty is provided.
### BEGIN INIT INFO
# Provides: iptables
# Required-Start: \$local_fs \$remote_fs \$network \$syslog
# Required-Stop: \$local_fs \$remote_fs \$network \$syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start the iptables firewall
### END INIT INFO
. /lib/lsb/init-functions
IPTABLES_SAVE="/etc/default/iptables-rules"
SAVE_RESTORE_OPTIONS="-c"
checkrules() {
if [ ! -f \${IPTABLES_SAVE} ]
then
echo "Not starting iptables. First create some rules then run"
echo "\"/etc/init.d/iptables save\""
return 1
fi
}
save() {
/sbin/iptables-save \${SAVE_RESTORE_OPTIONS} > \${IPTABLES_SAVE}
return \$?
}
start(){
checkrules || return 1
/sbin/iptables-restore \${SAVE_RESTORE_OPTIONS} < \${IPTABLES_SAVE}
return \$?
}
case "\$1" in
save)
echo -n "Saving iptables state..."
save
if [ \$? -eq 0 ] ; then
echo " ok"
else
echo " error !"
fi
;;
start)
log_begin_msg "Loading iptables state and starting firewall..."
start
log_end_msg \$?
;;
stop)
log_begin_msg "Stopping firewall..."
for a in \`cat /proc/net/ip_tables_names\`; do
/sbin/iptables -F -t \$a
/sbin/iptables -X -t \$a
if [ \$a == nat ]; then
/sbin/iptables -t nat -P PREROUTING ACCEPT
/sbin/iptables -t nat -P POSTROUTING ACCEPT
/sbin/iptables -t nat -P OUTPUT ACCEPT
elif [ \$a == mangle ]; then
/sbin/iptables -t mangle -P PREROUTING ACCEPT
/sbin/iptables -t mangle -P INPUT ACCEPT
/sbin/iptables -t mangle -P FORWARD ACCEPT
/sbin/iptables -t mangle -P OUTPUT ACCEPT
/sbin/iptables -t mangle -P POSTROUTING ACCEPT
elif [ \$a == filter ]; then
/sbin/iptables -t filter -P INPUT ACCEPT
/sbin/iptables -t filter -P FORWARD ACCEPT
/sbin/iptables -t filter -P OUTPUT ACCEPT
fi
done
log_end_msg 0
;;
restart)
log_begin_msg "Restarting firewall..."
for a in \`cat /proc/net/ip_tables_names\`; do
/sbin/iptables -F -t \$a
/sbin/iptables -X -t \$a
done;
start
log_end_msg \$?
;;
*)
echo "Usage: /etc/init.d/iptables {start|stop|restart|save}" >&2
exit 1
;;
esac
exit 0
END
chmod +x /etc/init.d/iptables
# Flush any existing iptables
/sbin/iptables -v -F
# http://articles.slicehost.com/2010/4/30/ubuntu-lucid-setup-part-1
# Allows all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
/sbin/iptables -v -A INPUT -i lo -j ACCEPT
/sbin/iptables -v -A INPUT ! -i lo -d 127.0.0.0/8 -j REJECT
# Accepts all established inbound connections
/sbin/iptables -v -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allows all outbound traffic
# You can modify this to only allow certain traffic
/sbin/iptables -v -A OUTPUT -j ACCEPT
# Allows HTTP and HTTPS connections from anywhere (the normal ports for websites)
/sbin/iptables -v -A INPUT -p tcp --dport 80 -j ACCEPT
/sbin/iptables -v -A INPUT -p tcp --dport 443 -j ACCEPT
# IF YOU USE INCOMMING MAIL UN-COMMENT THESE!!!
# Allows POP (and SSL-POP)
#/sbin/iptables -v -A INPUT -p tcp --dport 110 -j ACCEPT
#/sbin/iptables -v -A INPUT -p tcp --dport 995 -j ACCEPT
# SMTP (and SSMTP)
#/sbin/iptables -v -A INPUT -p tcp --dport 25 -j ACCEPT
#/sbin/iptables -v -A INPUT -p tcp --dport 465 -j ACCEPT
# IMAP (and IMAPS)
#/sbin/iptables -v -A INPUT -p tcp --dport 143 -j ACCEPT
#/sbin/iptables -v -A INPUT -p tcp --dport 993 -j ACCEPT
# Allows SSH connections (only 3 attempts by an IP every 2 minutes, drop the rest to prevent SSH attacks)
/sbin/iptables -v -A INPUT -p tcp -m tcp --dport $1 -m state --state NEW -m recent --set --name DEFAULT --rsource
/sbin/iptables -v -A INPUT -p tcp -m tcp --dport $1 -m state --state NEW -m recent --update --seconds 120 --hitcount 3 --name DEFAULT --rsource -j DROP
/sbin/iptables -v -A INPUT -p tcp -m state --state NEW --dport $1 -j ACCEPT
# Allow ping
/sbin/iptables -v -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# log iptables denied calls
/sbin/iptables -v -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Reject all other inbound - default deny unless explicitly allowed policy
/sbin/iptables -v -A INPUT -j REJECT
/sbin/iptables -v -A FORWARD -j REJECT
/etc/init.d/iptables save
update-rc.d iptables defaults
}
function install_syslogd {
# We just need a simple vanilla syslogd. Also there is no need to log to
# so many files (waste of fd). Just dump them into
# /var/log/(cron/mail/messages)
check_install /usr/sbin/syslogd "inetutils-syslogd"
invoke-rc.d inetutils-syslogd stop
for file in /var/log/*.log /var/log/mail.* /var/log/debug /var/log/syslog
do
[ -f "$file" ] && rm -f "$file"
done
for dir in fsck news
do
[ -d "/var/log/$dir" ] && rm -rf "/var/log/$dir"
done
cat > /etc/syslog.conf <<END
*.*;mail.none;cron.none -/var/log/messages
cron.* -/var/log/cron
mail.* -/var/log/mail
END
[ -d /etc/logrotate.d ] || mkdir -p /etc/logrotate.d
cat > /etc/logrotate.d/inetutils-syslogd <<END
/var/log/cron
/var/log/mail
/var/log/messages {
rotate 4
weekly
missingok
notifempty
compress
sharedscripts
postrotate
/etc/init.d/inetutils-syslogd reload >/dev/null
endscript
}
END
invoke-rc.d inetutils-syslogd start
}
function install_wordpress {
check_install wget "wget"
if [ -z "$1" ]
then
die "Usage: `basename $0` wordpress <hostname>"
fi
# Downloading the WordPress' latest and greatest distribution.
mkdir /tmp/wordpress.$$
wget -O - http://wordpress.org/latest.tar.gz | \
tar zxf - -C /tmp/wordpress.$$
if [ ! -d /var/www ]; then
mkdir /var/www
fi
mv /tmp/wordpress.$$/wordpress "/var/www/$1"
rm -rf /tmp/wordpress.$$
chown root:root -R "/var/www/$1"
# Setting up the MySQL database
dbname=`echo $1 | tr . _`
userid=`get_domain_name $1`
# MySQL userid cannot be more than 15 characters long
userid="${userid:0:15}"
passwd=`get_password "$userid@mysql"`
cp "/var/www/$1/wp-config-sample.php" "/var/www/$1/wp-config.php"
sed -i "s/database_name_here/$dbname/; s/username_here/$userid/; s/password_here/$passwd/" \
"/var/www/$1/wp-config.php"
mysqladmin create "$dbname"
echo "GRANT ALL PRIVILEGES ON \`$dbname\`.* TO \`$userid\`@localhost IDENTIFIED BY '$passwd';" | \
mysql
# Setting up Nginx mapping
cat > "/etc/nginx/sites-available/$1.conf" <<END
server {
listen 80;
listen [::]:80;
server_name $1;
root /var/www/$1;
access_log /var/log/nginx/$1.log main buffer=16k;
index index.php;
include standard.conf;
include fastcgi_php;
include nocgi.conf;
include disallow.conf;
END
cat >> "/etc/nginx/sites-available/$1.conf" <<END
location / {
try_files \$uri \$uri/ /index.php;
}
END
if [ -e /etc/nginx/myips.conf ]; then
cat >> "/etc/nginx/sites-available/$1.conf" <<END
location /wp-admin {
include myips.conf;
try_files \$uri \$uri/ /index.php;
}
END
fi
cat >> "/etc/nginx/sites-available/$1.conf" <<END
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi_params;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
}
END
ln -s /etc/nginx/sites-available/$1.conf /etc/nginx/sites-enabled/$1.conf
service nginx force-reload
}
function install_friendica {
if [ -z "$2" ]; then
die "Usage: `basename $0` friendica <hostname>"
fi
if [ -d /var/www/$2 -a ! "$3" = "redo" ]; then
die "$2 already exists"
fi
check_install "friendica dependencies" "git php5-imap php5-mcrypt"
if [ ! -d /var/www ]; then
mkdir /var/www
chown www-data:www-data /var/www
fi
cd /var/www
if [ -d friendica ]; then
rm -r friendica #Delete previous clone, which may have errors
fi
if [ ! "$3" = "redo" ]; then
git clone https://github.com/friendica/friendica.git
mv friendica $2
chown -R www-data:www-data $2
cd $2
git clone https://github.com/friendica/friendica-addons.git
mv friendica-addons addon
chown www-data:www-data addon view/smarty3
fi
cd /var/www/$2
cat > "/etc/nginx/sites-available/$2.conf" <<END
server {
listen 80;
END
if [ "$FLAGS" = "ipv6" -o "$FLAGS" = "all" ]; then
cat >> "/etc/nginx/sites-available/$2.conf" <<END
listen [::]:80;
END
fi
cat >> "/etc/nginx/sites-available/$2.conf" <<END
server_name $2;
access_log off;
return 301 https://$2/$request_uri;
}
END
cat >> "/etc/nginx/sites-available/$2.conf" <<END
server {
listen 443 ssl spdy;
END
if [ "$FLAGS" = "ipv6" -o "$FLAGS" = "all" ]; then
cat >> "/etc/nginx/sites-available/$2.conf" <<END
listen [::]:443 ssl spdy;
END
fi
cat >> "/etc/nginx/sites-available/$2.conf" <<END
server_name $2;
access_log /var/log/nginx/$2.log main buffer=16k;
root /var/www/$2;
ssl_certificate ssl_keys/default.pem;
ssl_certificate_key ssl_keys/default.key;
# ssl_certificate ssl_keys/$2.pem;
# ssl_certificate_key ssl_keys/$2.key;
location = /favicon.ico {
expires max;
log_not_found off;
access_log off;
return 204;
}
location = /robots.txt {
log_not_found off;
access_log off;
}
location ~ /\.(ht|git) {
return 444;
access_log off;
log_not_found off;
}
location ~ \.log$ {
return 444;
access_log off;
log_not_found off;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME \$request_filename;
fastcgi_param HTTPS on;
fastcgi_index index.php;
fastcgi_pass php;
try_files \$uri \$uri/ =404;
}
location / {
index index.php;
if (!-f \$request_filename) {
rewrite ^/(.+)\$ /index.php?q=\$1 last;