-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsetup.sh
294 lines (238 loc) · 8.37 KB
/
setup.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
echo "========================================================="
echo "==!! Pihole Android Private DNS With Docker !!=="
echo "========================================================="
echo ""
domain_name="$1"
email="$2"
tz="$3"
passwd="$4"
if [ -z "$domain_name" ]; then
echo "Provide a doamin name"
exit 1
fi
if [ -z "$email" ]; then
echo "Provide an email address for lets encrypt"
exit 1
fi
if [ -z "$tz" ]; then
echo "Provide a timezone"
exit 1
fi
if [ -z "$passwd" ]; then
echo "Provide a password"
exit 1
fi
touch docker-compose.yml
echo "version: '3'
services:
pihole:
image: pihole/pihole:latest
ports:
- \"53:53/tcp\"
- \"53:53/udp\"
- \"67:67/udp\"
volumes:
- './etc-pihole/:/etc/pihole/'
- './etc-dnsmasq.d/:/etc/dnsmasq.d/'
environment:
TZ: '$tz'
WEBPASSWORD: '$passwd'
VIRTUAL_HOST: '$domain_name'
networks:
- app-network
restart: unless-stopped
webserver:
image: nginx:mainline-alpine
container_name: webserver
restart: unless-stopped
ports:
- \"80:80\"
- \"443:443\"
- \"853:853\"
volumes:
- ./web-root:/var/www/html
- ./nginx-conf:/etc/nginx/conf.d
- ./certbot-etc:/etc/letsencrypt
- ./certbot-var:/var/lib/letsencrypt
depends_on:
- pihole
networks:
- app-network
certbot:
image: certbot/certbot
container_name: certbot
volumes:
- ./certbot-etc:/etc/letsencrypt
- ./certbot-var:/var/lib/letsencrypt
- ./web-root:/var/www/html
depends_on:
- webserver
command: certonly --webroot --webroot-path=/var/www/html --preferred-chain=\"ISRG Root X1\" --email $email --agree-tos --no-eff-email -d $domain_name
volumes:
etc-pihole:
nginx-conf:
etc-dnsmasq.d:
certbot-etc:
certbot-var:
web-root:
networks:
app-network:
driver: bridge" > docker-compose.yml
echo ""
echo "========================================================================================"
echo "Docker-compose file Created `tput setaf 2`✓ `tput setaf 7`"
echo "========================================================================================"
echo ""
mkdir nginx-conf
mkdir nginx-conf/stream
touch nginx-conf/1.conf
touch nginx-conf/stream/2.conf
echo "server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.html index.htm index.nginx-debian.html;
server_name $domain_name;
location ~ /.well-known/acme-challenge {
allow all;
root /var/www/html;
}
location / {
#proxy_pass http://pihole;
return 301 https://\$host\$request_uri;
}
}" > ./nginx-conf/1.conf
echo "upstream dns-servers {
server pihole:53;
}
upstream pihole {
server pihole:80;
}
server {
listen 853 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/$domain_name/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/$domain_name/privkey.pem; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_handshake_timeout 10s;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 4h;
proxy_pass dns-servers;
}
server {
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/$domain_name/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/$domain_name/privkey.pem; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers HIGH:!aNULL:!MD5;
#ssl_handshake_timeout 10s;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 4h;
proxy_pass pihole;
}
" > ./nginx-conf/stream/2.conf
echo ""
echo "==================================================================================="
echo "Nginx Configuartion created `tput setaf 2` ✓ `tput setaf 7`"
echo "==================================================================================="
echo ""
mkdir certbot-etc
echo ""
echo "==================================================================================="
echo "generating dhparam you might be prompted to enter the password "
echo ""
sudo openssl dhparam -out ./certbot-etc/ssl-dhparams.pem 1024
echo ""
echo "Genarated dhparams `tput setaf 2` ✓ `tput setaf 7`"
echo ""
echo "==================================================================================="
docker-compose up -d
echo ""
echo "===================================================================================="
echo "Docker containers started `tput setaf 2` ✓ `tput setaf 7`"
echo "===================================================================================="
echo ""
echo "
echo \"
stream {
include /etc/nginx/conf.d/stream/*;
}\" >>/etc/nginx/nginx.conf
" > setup-in-container.sh
echo ""
echo "====================================================================================="
echo "Additional Nginx configuration created `tput setaf 2` ✓ `tput setaf 7`"
echo "====================================================================================="
echo ""
docker cp setup-in-container.sh webserver:/tmp
docker exec webserver sh /tmp/setup-in-container.sh
echo ""
echo "======================================================================================"
echo "Nginx configuartion updated `tput setaf 2` ✓ `tput setaf 7`"
echo "======================================================================================"
echo ""
echo ""
echo "======================================================================================"
echo "Checking if Certbot Finished generating certificates if it get stuck here for long "
echo "Certbot might have failed. stop the script and check logs by running "
echo "'docker logs certbot'"
echo ""
test=`sudo ls ./certbot-etc/live/ 2> /dev/null | grep $domain_name | wc -l`
while [ "$test" != "1" ]
do
echo -ne "...."
sleep 2s
test=`sudo ls ./certbot-etc/live/ 2> /dev/null | grep $domain_name | wc -l`
done
echo -ne "Certificate genarated `tput setaf 2` ✓ `tput setaf 7`"
echo ""
echo "======================================================================================"
echo ""
docker exec webserver nginx -s reload
echo ""
echo "======================================================================================"
echo "Restarted Nginx `tput setaf 2`✓ `tput setaf 7`"
echo "======================================================================================"
echo ""
docker exec webserver rm /tmp/setup-in-container.sh
rm setup-in-container.sh
echo ""
echo "======================================================================================"
echo "Cleanup Jobs done `tput setaf 2` ✓ `tput setaf 7`"
echo "======================================================================================"
echo ""
echo "
#!/bin/bash
COMPOSE=\"/usr/local/bin/docker-compose --no-ansi\"
DOCKER=\"/usr/bin/docker\"
cd `pwd`
\$COMPOSE run certbot renew --force-renewal --preferred-chain=\"ISRG Root X1\" && \$COMPOSE kill -s SIGHUP webserver
\$DOCKER system prune -af
"> ssl_renew.sh
chmod +x ssl_renew.sh
echo ""
echo "======================================================================================"
echo "Renew Script created `tput setaf 2` ✓ `tput setaf 7`"
echo ""
echo "Add ssl_renew.sh to crontab to run every month to keep certificate renewed automatically"
echo ""
echo "Add the following line to the crontab file after running 'sudo crontab -e' to check and renew certificate very 10th day of month"
echo ""
echo " 0 0 */10 * * `pwd`/ssl_renew.sh >> /var/log/cron.log 2>&1"
echo "======================================================================================"
echo ""
echo ""
echo ""
echo ""
echo "======================================================================================"
echo "Congrats Pihole Android Private DNS with Docker is configured."
echo ""
echo "Private DNS Domain : $domain_name"
echo ""
echo "Access the admin page of pihole at : https://$domain_name/admin"
echo ""
echo "Webui Password : $passwd"
echo ""
echo "Now you can use the domain name in your android phone"
echo "======================================================================================"