-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_nginx.py
102 lines (87 loc) · 3.24 KB
/
setup_nginx.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
import subprocess
import os
from dotenv import load_dotenv
def print_color(text, color):
print(f"\033[1;{color}m{text}\033[0m")
def setup_nginx():
load_dotenv()
contact = os.getenv("CONTACT")
domain_name = os.getenv("DOMAIN")
nginx_filepath = os.getenv("NGINX_FILE_PATH")
if os.path.exists(nginx_filepath):
try:
subprocess.run(["sudo", "rm", nginx_filepath], check=True)
print_color("NGINX file removed successfully.", "32")
except subprocess.CalledProcessError as e:
print(f"An error occurred while removing the file: {e}")
nginx_config = f"""
server {{
server_name {domain_name};
location / {{
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Authorization';
add_header 'Content-Type' 'application/json';
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://127.0.0.1:5555;
proxy_http_version 1.1;
}}
}}
"""
try:
# Open a subprocess running sudo tee to write to the file with elevated privileges
process = subprocess.Popen(
["sudo", "tee", nginx_filepath],
stdin=subprocess.PIPE,
universal_newlines=True,
)
process.communicate(nginx_config)
if process.returncode == 0:
print_color(
"The default configuration file has been written successfully.", "32"
)
else:
raise Exception(
f"tee command returned non-zero exit status {process.returncode}"
)
except PermissionError as e:
print_color(
"Permission denied: Please run this script with elevated privileges.", "31"
)
except Exception as e:
print_color(
f"An error occurred while writing the default configuration file: {e}", "31"
)
try:
subprocess.run(["sudo", "service", "nginx", "restart"], check=True)
except subprocess.CalledProcessError as e:
print(f"An error occurred while restarting nginx: {e}")
cert_file_path = f"/etc/letsencrypt/live/{domain_name}/fullchain.pem"
if os.path.isfile(cert_file_path):
print("The file exists!")
else:
print("The file doesn't exist!")
try:
subprocess.run(
[
"sudo",
"certbot",
"--nginx",
"-d",
domain_name,
"--non-interactive",
"--agree-tos",
"--email",
contact,
],
check=True,
)
except subprocess.CalledProcessError as e:
print(f"An error occurred while running certbot: {e}")
try:
subprocess.run(["sudo", "service", "nginx", "restart"], check=True)
except subprocess.CalledProcessError as e:
print(f"An error occurred while restarting nginx: {e}")
if __name__ == "__main__":
setup_nginx()