-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnginxinstaller2
executable file
·162 lines (123 loc) · 5.77 KB
/
nginxinstaller2
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
#!/usr/bin/env python
# Nginx Admin Installer
# Website: www.nginxcp.com
#
# Copyright (C) NGINXCP.COM.
#
import sys
import os
import subprocess
import time
import re
sys.path.append('/scripts/')
import createvhosts
import xmlapi
def findapacheversion():
proc = subprocess.Popen(["/usr/sbin/httpd", "-V"], stdout=subprocess.PIPE)
output = proc.communicate()[0]
m = re.search("Apache/(2\.[0-9]+\.[0-9]+)", output)
if not m:
print "Unsupported Apache version"
sys.exit(1)
return m.group(1)
def installmodule(name):
f=open('/usr/local/apache/conf/httpd.conf', 'r')
fl = f.readlines()
f.close
include="Include \"/usr/local/apache/conf/includes/" + name + ".conf\""
if include not in fl:
fl.insert(29, include)
f=open('/usr/local/apache/conf/httpd.conf', 'w')
f.writelines(fl)
f.close
ipliststring = createvhosts.getipliststring()
apache_version = findapacheversion()
if apache_version.startswith("2.4.") and os.path.exists("/var/cpanel/templates/apache2_4"):
remoteipliststring = ""
for ip in ipliststring.split(' '):
remoteipliststring += "RemoteIPInternalProxy " + ip + "\n"
moduleinclude = """cat >> "/usr/local/apache/conf/includes/remoteip.conf" <<EOF
LoadModule remoteip_module modules/mod_remoteip.so
#Mod_remoteip settings
RemoteIPHeader X-Real-IP
RemoteIPInternalProxy 127.0.0.1
%s
EOF""" % (remoteipliststring)
else:
moduleinclude = """cat >> "/usr/local/apache/conf/includes/rpaf.conf" <<EOF
LoadModule rpaf_module modules/mod_rpaf-2.0.so
#Mod_rpaf settings
RPAFenable On
RPAFproxy_ips 127.0.0.1 %s
RPAFsethostname On
RPAFheader X-Real-IP
EOF""" % (ipliststring)
proc = subprocess.Popen(moduleinclude, shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/$HTTPD -k .*/\\0\\n\\/etc\\/init.d\\/nginx $ARGV/g' /etc/init.d/httpd", shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/$HTTPD -k .*/\\0\\n\\/etc\\/init.d\\/nginx $ARGV/g' /usr/local/apache/bin/apachectl", shell=True)
output = proc.communicate()
if apache_version.startswith("2.4.") and os.path.exists("/var/cpanel/templates/apache2_4"):
remoteipinstall=installmodule("remoteip")
else:
rpafinstall=installmodule("rpaf")
proc = subprocess.Popen("/usr/local/cpanel/bin/apache_conf_distiller --update > /dev/null 2>&1", shell=True)
output = proc.communicate()
time.sleep(5)
rotatescript = """cat >> "/etc/logrotate.d/nginx" <<EOF
/var/log/nginx/*log {
missingok
notifempty
sharedscripts
postrotate
/sbin/service nginx reload > /dev/null 2>/dev/null || true
endscript
}
EOF"""
proc = subprocess.Popen(rotatescript, shell=True)
output = proc.communicate()
ncheckservd = "service[nginx]=x,x,x,/scripts/nginx_restart,nginx,root"
nchkservdf = open('/etc/chkserv.d/nginx', 'w')
nchkservdf.write(ncheckservd)
nchkservdf.close()
print " Reading Apache Configuration "
print " updating Apache configuration "
if not os.path.exists("/var/cpanel/templates/apache2/vhost.local"):
proc = subprocess.Popen("cp -a /var/cpanel/templates/apache2/vhost.default /var/cpanel/templates/apache2/vhost.local", shell=True)
output = proc.communicate()
if not os.path.exists("/var/cpanel/templates/apache2/ssl_vhost.local"):
proc = subprocess.Popen("cp -a /var/cpanel/templates/apache2/ssl_vhost.default /var/cpanel/templates/apache2/ssl_vhost.local", shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/CustomLog/#CustomLog/g' /var/cpanel/templates/apache2/vhost.local", shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/CustomLog/#CustomLog/g' /var/cpanel/templates/apache2/ssl_vhost.local", shell=True)
output = proc.communicate()
if apache_version.startswith("2.4.") and os.path.exists("/var/cpanel/templates/apache2_4"):
if not os.path.exists("/var/cpanel/templates/apache2_4/vhost.local"):
proc = subprocess.Popen("cp -a /var/cpanel/templates/apache2_4/vhost.default /var/cpanel/templates/apache2_4/vhost.local", shell=True)
output = proc.communicate()
if not os.path.exists("/var/cpanel/templates/apache2_4/ssl_vhost.local"):
proc = subprocess.Popen("cp -a /var/cpanel/templates/apache2_4/ssl_vhost.default /var/cpanel/templates/apache2_4/ssl_vhost.local", shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/CustomLog/#CustomLog/g' /var/cpanel/templates/apache2_4/vhost.local", shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/CustomLog/#CustomLog/g' /var/cpanel/templates/apache2_4/ssl_vhost.local", shell=True)
output = proc.communicate()
proc = subprocess.Popen("/scripts/rebuildhttpdconf > /dev/null 2>&1", shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/localhost/localhost:8081/g' /etc/init.d/httpd", shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/localhost/localhost:8081/g' /usr/local/apache/bin/apachectl", shell=True)
output = proc.communicate()
proc = subprocess.Popen("sed -i 's/apache_port=0.0.0.0:80$/apache_port=0.0.0.0:8081/' /var/cpanel/cpanel.config", shell=True)
output = proc.communicate()
subprocess.Popen("/usr/local/cpanel/whostmgr/bin/whostmgr2 --updatetweaksettings > /dev/null 2>&1", shell=True)
output = proc.communicate()
time.sleep(15)
proc = subprocess.Popen("/etc/init.d/httpd restart > /dev/null 2>&1", shell=True)
output = proc.communicate()
print " ****************************************************"
print " * Installation Complete *"
print " *run /etc/init.d/httpd restart to start Nginx Admin*"
print " ****************************************************"