-
Notifications
You must be signed in to change notification settings - Fork 1
/
nginx-docker.py
151 lines (119 loc) · 5 KB
/
nginx-docker.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import sys
import pathlib
import datetime
import pwd
def usage():
print("""sudo python nginx.py <domain> <ssl>
<domain> (optional) - if none, defaults to catchall
<ssl> (optional) - true|false - letsencrypt ssl""")
exit(0)
def chown(f, user):
gid = pwd.getpwnam(user).pw_gid
uid = pwd.getpwnam(user).pw_uid
os.chown(f, uid, gid)
def create_and_chown(path, name):
if not os.path.exists(path):
if path.endswith('/'):
os.makedirs(path)
else:
pathlib.Path(path).touch()
chown(path, name)
def init_domain(domain, service_port, ssl=False):
if not os.path.exists("/etc/nginx/"):
os.system("apt-get install -y nginx")
# Create default files
create_and_chown('/srv/www/', 'www-data')
create_and_chown('/srv/www/default/', 'www-data')
# Create default files
default_index = '/srv/www/default/index.html'
if not os.path.exists(default_index):
open(default_index, 'w+').write('hello')
chown(default_index, 'www-data')
default_robots = '/srv/www/default/robots.txt'
if not os.path.exists(default_robots):
open(default_robots, 'w+').write('')
chown(default_robots, 'www-data')
# Create domain files
if domain:
domain_root = os.path.join('/srv/www/', domain, '')
create_and_chown(domain_root, 'www-data')
domain_index = os.path.join(domain_root, 'index.html')
domain_robots = os.path.join(domain_root, 'robots.txt')
try:
os.symlink(default_index, domain_index)
os.symlink(default_robots, domain_robots)
except Exception as e:
print(
"[!] Probably tried to write over existing symlink for default files. Not doing it\n\t",
e)
pass
# check if our log files exist
log_dir = os.path.join('/var/log/nginx', domain, '')
create_and_chown(log_dir, 'www-data')
error_file = os.path.join(log_dir, 'error.log')
create_and_chown(error_file, 'www-data')
access_file = os.path.join(log_dir, 'access.log')
create_and_chown(access_file, 'www-data')
# Create NGINX Config
if domain == 'default': # We want a catchall default route
config_data = open('./templates/nginx_default').read()
else: # user specified a domain so we'll set that up for them
config_data = open('./templates/nginx_domain_service').read().replace(
'repl_domain', domain)
config_data = config_data.replace('repl_port', service_port)
nginx_config = os.path.join('/etc/nginx/sites-available', domain)
if os.path.exists(nginx_config):
new_name = nginx_config + str(datetime.datetime.now()).replace(' ',
'_')
print(
"[!] Found an existing nginx config at {0}. Backing up to {1}".
format(nginx_config, new_name))
os.rename(nginx_config, new_name)
open(nginx_config, 'w+').write(config_data)
nginx_enabled_config = os.path.join('/etc/nginx/sites-enabled', domain)
if not os.path.exists(nginx_enabled_config):
os.symlink(nginx_config, nginx_enabled_config)
# Start nginx
os.system("service nginx start")
if os.system("service nginx restart"):
print("[!] Oh no. nginx failed to start. Aborting")
exit(-1)
print("[+] Successfully created nginx stuff for {0}".format(
domain if domain else 'default'))
print("[+] Now doing Letsencrypt")
if ssl:
os.system("./certbot-auto certonly --agree-tos --webroot -w /srv/www/{domain}/ -d {domain}".format(domain = domain))
nginx_content = open(nginx_config, 'r').read()
ssl_template = open('./templates/ssl_snippet').read().replace("template", domain)
if '#ssl' in nginx_content:
nginx_content = open(nginx_config, 'r').read().replace('#ssl', ssl_template)
else:
print("[!] Seems please add #ssl into the nginx config where you would like to add the ssl stuff")
print("[!] Your file to edit: ", nginx_config)
print("[!] Then run this again")
exit(-1)
open(nginx_config, 'w').write(nginx_content)
if not os.path.exists("/etc/nginx/dhparam.pem"):
print("[!] Generating DH Param for better SSL Security. Will take a while")
os.system("openssl dhparam -dsaparam -out /etc/nginx/dhparam.pem 4096")
if os.system("service nginx restart"):
print("[!] Oh no. nginx failed to start.")
os.system("nginx -t")
exit(-1)
if __name__ == "__main__":
domain = 'default'
ssl = False
help_words = ['-h', 'h', 'help', '?']
if len(sys.argv) > 1:
if sys.argv[1] in help_words:
usage()
exit(0)
domain = sys.argv[1]
port = sys.argv[2]
if len(sys.argv) > 3:
ssl = sys.argv[3]
os.system("apt-get update")
init_domain(domain, port, ssl)