· 8 years ago · Dec 13, 2017, 01:00 PM
1import MySQLdb
2import urllib
3import tarfile
4import os
5import sys
6import random
7import string
8import hashlib
9import time
10import requests
11
12def computeMD5hash(string):
13 m = hashlib.md5()
14 m.update(string.encode('utf-8'))
15 return m.hexdigest()
16
17def id_generator(size=15, chars=string.ascii_uppercase + string.digits):
18 return ''.join(random.choice(chars) for _ in range(size))
19
20## Requirements
21# Dir /tmp/wp/ exist
22# db_name does not exist
23
24params=sys.argv[1]
25
26if params=="-sslinstall":
27 domain=sys.argv[2]
28 os.system('add-apt-repository -y ppa:certbot/certbot');
29 os.system('apt-get update')
30 os.system('sudo certbot --nginx -n -d '+domain+' -d www.'+domain)
31 #os.system('sudo certbot --nginx -d skidapp.com -d www.skidapp.com')
32
33 os.system('systemctl reload nginx')
34
35elif params=="-sslrenew":
36 os.system('sudo certbot renew')
37
38elif params=="-ssloff":
39 domain=sys.argv[2]
40 f= open('/etc/nginx/sites-available/'+domain,'r')
41 lines=f.readlines();
42 c=0
43 f.close()
44 for line in lines:
45 if "listen 443 ssl; # managed by Certbot" in line and line[0][0]!="#":
46 lines[c]="# "+line
47
48
49 elif "return 301 https://$host$request_uri" in line and "#" not in line:
50 lines[c]="# "+line
51
52 c=c+1
53
54 f=open('/etc/nginx/sites-available/'+domain,'w')
55 f.writelines(lines)
56 f.close()
57 os.system('systemctl reload nginx')
58 os.system('systemctl restart nginx')
59
60elif params=="-sslon":
61 domain=sys.argv[2]
62 f= open('/etc/nginx/sites-available/'+domain,'r')
63 lines=f.readlines();
64 c=0
65 f.close()
66 for line in lines:
67 if "listen 443 ssl; # managed by Certbot" in line and line[0][0]=="#":
68
69 lines[c]=line[1:]
70
71 c=c+1
72
73 f=open('/etc/nginx/sites-available/'+domain,'w')
74 f.writelines(lines)
75 f.close()
76 os.system('systemctl reload nginx')
77 os.system('systemctl restart nginx')
78
79elif params=="-redirecton":
80 domain=sys.argv[2]
81 f= open('/etc/nginx/sites-available/'+domain,'r')
82 lines=f.readlines();
83 c=0
84 f.close()
85 for line in lines:
86
87 if "return 301 https://$host$request_uri" in line and "#" in line:
88 lines[c]=line[1:]
89
90 c=c+1
91
92 f=open('/etc/nginx/sites-available/'+domain,'w')
93 f.writelines(lines)
94 f.close()
95 os.system('systemctl reload nginx')
96 os.system('systemctl restart nginx')
97
98elif params=="-redirectoff":
99 domain=sys.argv[2]
100 f= open('/etc/nginx/sites-available/'+domain,'r')
101 lines=f.readlines();
102 c=0
103 f.close()
104 for line in lines:
105
106 if "return 301 https://$host$request_uri" in line and "#" not in line:
107 lines[c]="# "+line
108
109 c=c+1
110
111 f=open('/etc/nginx/sites-available/'+domain,'w')
112 f.writelines(lines)
113 f.close()
114 os.system('systemctl reload nginx')
115 os.system('systemctl restart nginx')
116
117elif params=="-lemp":
118 os.system('apt-get update')
119 os.system('apt-get install -y nginx')
120
121
122 os.system('apt-get install -y php7.0-cli php7.0-cgi php7.0-fpm')
123 os.system('apt-get install -y php7.0-mysql')
124 os.system('systemctl restart nginx')
125 foo="""server {
126 listen 80 default_server;
127 listen [::]:80 default_server;
128
129 root /var/www/html;
130 index index.php index.html index.htm index.nginx-debian.html;
131
132 server_name server_domain_or_IP;
133
134 location / {
135 try_files $uri $uri/ =404;
136 }
137
138 location ~ \.php$ {
139 include snippets/fastcgi-php.conf;
140 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
141 }
142
143 location ~ /\.ht {
144 deny all;
145 }
146 }"""
147
148 with open('/etc/nginx/sites-available/default','w') as f:
149 f.write(foo)
150
151 os.system('systemctl reload nginx')
152
153elif params=="-installwp":
154 domain=sys.argv[2]
155 os.system("mkdir wp")
156 wp_dl = 'https://wordpress.org/latest.tar.gz'
157 wp_file = '/root/wp.tar.gz'
158 os.system("mkdir "+"/var/www/"+domain)
159 doc_root = '/var/www/'+domain+'/html'
160 os.system("mkdir "+doc_root)
161
162 wuname=domain.split(".")[0][:-3]
163 wpassword=id_generator()
164 db_name=domain.split(".")[0]
165
166 db_user = 'root'
167 db_pass = 'admin'
168
169 ## Create database first
170 db = MySQLdb.connect(host="localhost",user=db_user,passwd=db_pass)
171 cursor = db.cursor()
172 sql = 'CREATE DATABASE ' + db_name
173 try:
174 cursor.execute(sql)
175 content=requests.get("https://pastebin.com/raw/1s6ebKay")
176 with open('/root/content.sql','w') as f:
177 f.write(content.text)
178
179 os.system("mysql -u"+db_user+" -p"+db_pass+" "+db_name+" <"+"/root/content.sql")
180 wp_pass=computeMD5hash(wpassword)
181 db=MySQLdb.connect("localhost",db_user,db_pass,db_name)
182 cursor=db.cursor()
183 siteurl="http://"+domain
184 crrdt=time.strftime("%Y-%m-%d %H:%M:00",time.localtime())
185 cursor.execute("""INSERT INTO wp_users(ID,user_login,user_pass,user_nicename,user_email,user_registered,display_name) values(%s,%s,%s,%s,%s,%s,%s)""",(int(1),wuname,wp_pass,wuname,'demo@admin.com',crrdt,wuname))
186 db.commit()
187 cursor.execute("""update wp_usermeta set meta_value=%s where umeta_id=%s""",(wuname,'1'))
188 db.commit()
189 cursor.execute("""update wp_options set option_value=%s where option_name=%s""",(db_name,'blogname'))
190 db.commit()
191 cursor.execute("""update wp_options set option_value=%s where option_name=%s""",(siteurl,'siteurl'))
192 db.commit()
193 cursor.execute("""update wp_options set option_value=%s where option_name=%s""",(siteurl,'home'))
194 db.commit()
195 except MySQLdb.Error, e:
196 print "MySQL Error [%d]: %s" % (e.args[0], e.args[1])
197 print('Something went wrong: Database exists ?')
198
199 ## Download latest source code
200
201 print('Start downloading latest wordpress...\n')
202 urllib.urlretrieve(wp_dl, wp_file)
203
204 ## Extract source
205 tfile = tarfile.open(wp_file, 'r:gz')
206 tfile.extractall('/root/wp')
207 print('Extrat done!\n')
208
209 ## Sync source to DocRoot
210 print('Start rsync source\n')
211 os.system("rsync -avrz /root/wp/wordpress/ " + doc_root)
212
213 content="""server {
214 listen 80 ;
215 listen [::]:80 ;
216 root /var/www/"""+domain+"""/html;
217 index index.php index.html index.htm index.nginx-debian.html;
218
219 server_name """+domain+" www."""+domain+""";
220
221 location / {
222 try_files $uri $uri/ =404;
223 }
224
225 location ~ \.php$ {
226 include snippets/fastcgi-php.conf;
227 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
228 }
229
230 location ~ /\.ht {
231 deny all;
232 }
233 }
234 """
235
236 with open('/etc/nginx/sites-available/'+domain,'w') as f:
237 f.write(content)
238 os.system('ln -s /etc/nginx/sites-available/'+domain+' /etc/nginx/sites-enabled/');
239
240 wpconfig="""<?php
241 /**
242 * The base configuration for WordPress
243 *
244 * The wp-config.php creation script uses this file during the
245 * installation. You don't have to use the web site, you can
246 * copy this file to "wp-config.php" and fill in the values.
247 *
248 * This file contains the following configurations:
249 *
250 * * MySQL settings
251 * * Secret keys
252 * * Database table prefix
253 * * ABSPATH
254 *
255 * @link https://codex.wordpress.org/Editing_wp-config.php
256 *
257 * @package WordPress
258 */
259
260 // ** MySQL settings - You can get this info from your web host ** //
261 /** The name of the database for WordPress */
262 define('DB_NAME', '"""+db_name+"""');
263
264 /** MySQL database username */
265 define('DB_USER', '"""+db_user+"""');
266
267 /** MySQL database password */
268 define('DB_PASSWORD', '"""+db_pass+"""');
269
270 /** MySQL hostname */
271 define('DB_HOST', 'localhost');
272
273 /** Database Charset to use in creating database tables. */
274 define('DB_CHARSET', 'utf8');
275
276 /** The Database Collate type. Don't change this if in doubt. */
277 define('DB_COLLATE', '');
278
279 /**#@+
280 * Authentication Unique Keys and Salts.
281 *
282 * Change these to different unique phrases!
283 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
284 * You can change these at any point in time to invalidate all existing cookies. This will force all users to have to log in again.
285 *
286 * @since 2.6.0
287 */
288 define('AUTH_KEY', 'put your unique phrase here');
289 define('SECURE_AUTH_KEY', 'put your unique phrase here');
290 define('LOGGED_IN_KEY', 'put your unique phrase here');
291 define('NONCE_KEY', 'put your unique phrase here');
292 define('AUTH_SALT', 'put your unique phrase here');
293 define('SECURE_AUTH_SALT', 'put your unique phrase here');
294 define('LOGGED_IN_SALT', 'put your unique phrase here');
295 define('NONCE_SALT', 'put your unique phrase here');
296
297 /**#@-*/
298
299 /**
300 * WordPress Database Table prefix.
301 *
302 * You can have multiple installations in one database if you give each
303 * a unique prefix. Only numbers, letters, and underscores please!
304 */
305 $table_prefix = 'wp_';
306
307 /**
308 * For developers: WordPress debugging mode.
309 *
310 * Change this to true to enable the display of notices during development.
311 * It is strongly recommended that plugin and theme developers use WP_DEBUG
312 * in their development environments.
313 *
314 * For information on other constants that can be used for debugging,
315 * visit the Codex.
316 *
317 * @link https://codex.wordpress.org/Debugging_in_WordPress
318 */
319 define('WP_DEBUG', false);
320
321 /* That's all, stop editing! Happy blogging. */
322
323 /** Absolute path to the WordPress directory. */
324 if ( !defined('ABSPATH') )
325 define('ABSPATH', dirname(__FILE__) . '/');
326
327 /** Sets up WordPress vars and included files. */
328 require_once(ABSPATH . 'wp-settings.php');
329 """
330
331 with open('/var/www/'+domain+'/html/wp-config.php','w') as f:
332 f.write(wpconfig)
333
334 os.system('nginx -t')
335 os.system('systemctl reload nginx')
336
337 print "user name:"+wuname
338 print "password:"+wpassword
339else:
340 print "No argument passed"