-
Notifications
You must be signed in to change notification settings - Fork 0
/
https.sh
162 lines (129 loc) · 4.41 KB
/
https.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
#!/bin/bash
apacheSuffix=apache.conf
nginxSuffix=nginx.conf
pathSiteAvailableNginx="/etc/nginx/sites-available"
pathSiteAvailableApache="/etc/apache2/sites-available"
echo "################################################"
echo "## CERTIFICATES LET'S ENCRYPT CREATED ? ##"
echo "################################################"
while true; do
read -rp "Certificates generated ? (y/n) " certbotUsed
case $certbotUsed in
[Yy]* ) certbotUsed="y" break;;
[Nn]* ) certbotUsed="n" break;;
* ) echo "Yes, y/No, n.";;
esac
done
if [ "$certbotUsed" == "y" ]; then
while true; do
read -rp "Activate on Apache or Nginx ? " servWeb
case $servWeb in
"apache" ) servWeb="apache" break;;
"nginx" ) servWeb="nginx" break;;
* ) echo "Apache ou Nginx.";;
esac
done
read -rp "Which vHost to edit ? (name without suffix) " siteName
while true; do
read -rp "Activate HSTS ? (y/n) " hsts
case $hsts in
[Yy]* ) hsts="y" break;;
[Nn]* ) hsts="n" break;;
* ) echo "Yes, y/No, n.";;
esac
done
##
# APACHE2
##
if [ "$servWeb" == "apache" ]; then
vHostEdit=$pathSiteAvailableApache/$siteName.$apacheSuffix
source generated_vhost/"$vHostEdit".variables
sed -i '/\/VirtualHost/d' "$vHostEdit"
sed -i '/#deleteifhttps/d' "$vHostEdit"
# ADD SSL BLOCK
cat base_vhost/apache/ssl >> "$vHostEdit"
sed -i 's/${serverName}/'"$serverName"'/' "$vHostEdit"
sed -i 's/${aliasName}/'"$aliasName"'/' "$vHostEdit"
sed -i 's~${documentRoot}~'"$documentRoot"'~' "$vHostEdit"
if [ "$hsts" == "y" ]; then
sed -i 's/#hsts//' "$vHostEdit"
else
sed -i '/#hsts/d' "$vHostEdit"
fi
sudo systemctl restart apache2
echo "vhost updated to handle HTTPS on Apache"
# DELETE FILE WITH VARIABLES
rm generated_vhost/"$vHostEdit".variables
fi
##
# NGINX
##
if [ "$servWeb" == "nginx" ]; then
vHostEdit=$pathSiteAvailableNginx/$siteName.$nginxSuffix
source generated_vhost/"$vHostEdit".variables
# ADD SSL BLOCK
cat base_vhost/nginx/ssl >> "$vHostEdit"
sed -i 's/${serverName}/'"$serverName"'/' "$vHostEdit"
sed -i 's/${aliasName}/'"$aliasName"'/' "$vHostEdit"
sed -i 's~${documentRoot}~'"$documentRoot"'~' "$vHostEdit"
if [ "$hsts" == "y" ]; then
sed -i 's/#hsts//' "$vHostEdit"
else
sed -i '/#hsts/d' "$vHostEdit"
fi
phpUsed=$(cat "$vHostEdit" | grep fastcgi_pass)
if [ -n "$phpUsed" ]; then
sed -i '/#deleteifhttps/d' "$vHostEdit"
sed -i '/#end/d' "$vHostEdit"
# ADD PHP BLOCK
cat base_vhost/nginx/php >> "$vHostEdit"
sed -i 's/${phpVersion}/'"$phpVersion"'/' "$vHostEdit"
sed -i 's/ #deleteifhttps//' "$vHostEdit"
elif [ -z "$phpUsed" ]; then
phpUsed=$(cat "$vHostEdit" | grep proxy_pass)
if [ -n "$phpUsed" ]; then
sed -i '/#deleteifhttps/d' "$vHostEdit"
sed -i '/#end/d' "$vHostEdit"
# ADD REVERSE_PROXY BLOCK
cat base_vhost/nginx/phpreverse >> "$vHostEdit"
sed -i 's/${ipToSend}/'"$ipToSend"'/' "$vHostEdit"
sed -i 's/${portToSend}/'"$portToSend"'/' "$vHostEdit"
sed -i 's/ #deleteifhttps//' "$vHostEdit"
fi
fi
sudo systemctl restart nginx
echo "vhost updated to handle HTTPS on NGINX"
# DELETE FILE WITH VARIABLES
rm generated_vhost/"$vHostEdit".variables
fi
else
echo "Create Let's Encrypt certificates first (see CertBot)"
while true; do
read -rp "Start Certbot ? (y/n)" certbot
case $certbot in
[Yy]* ) certbot="y" break;;
[Nn]* ) certbot="n" break;;
* ) echo "Yes, y/No, n.";;
esac
done
if [ "$certbot" == "y" ]; then
while true; do
read -rp "Which webserver ? (apache/nginx/exit) " certServWeb
case $certServWeb in
"apache" ) certServWeb="apache" break;;
"nginx" ) certServWeb="nginx" break;;
"exit" ) exit;;
* ) echo "apache or nginx";;
esac
done
if [ "$certServWeb" == "apache" ]; then
sudo certbot certonly --apache
enableCert=$(readlink -f "$0")
exec $enableCert
else
sudo certbot certonly --nginx
enableCert=$(readlink -f "$0")
exec $enableCert
fi
fi
fi