· 8 years ago · Dec 14, 2017, 12:26 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
124 s = "abcdefghijklmnopqrstuvwxyz01234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()?"
125 passlen =16
126 mysqlp = "".join(random.sample(s,passlen ))
127
128
129 with open("/.mysqlpassword", "a") as myfile:
130 myfile.write("root,"+mysqlp)
131
132
133 os.system('echo "mysql-server mysql-server/root_password password '+mysqlp+'" | debconf-set-selections')
134 os.system('echo "mysql-server mysql-server/root_password_again password '+mysqlp+'" | debconf-set-selections')
135 os.sytem('apt-get -y install mysql-server')
136
137
138 os.system('apt-get install -y php7.0-mysql')
139 os.system('systemctl restart nginx')
140 foo="""server {
141 listen 80 default_server;
142 listen [::]:80 default_server;
143
144 root /var/www/html;
145 index index.php index.html index.htm index.nginx-debian.html;
146
147 server_name server_domain_or_IP;
148
149 location / {
150 try_files $uri $uri/ =404;
151 }
152
153 location ~ \.php$ {
154 include snippets/fastcgi-php.conf;
155 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
156 }
157
158 location ~ /\.ht {
159 deny all;
160 }
161 }"""
162
163 with open('/etc/nginx/sites-available/default','w') as f:
164 f.write(foo)
165
166 os.system('systemctl reload nginx')
167
168elif params=="-installwp":
169 domain=sys.argv[2]
170 os.system("mkdir wp")
171 wp_dl = 'https://wordpress.org/latest.tar.gz'
172 wp_file = '/root/wp.tar.gz'
173 os.system("mkdir "+"/var/www/"+domain)
174 doc_root = '/var/www/'+domain+'/html'
175 os.system("mkdir "+doc_root)
176
177 wuname=domain.split(".")[0][:-3]
178 wpassword=id_generator()
179 db_name=domain.split(".")[0]
180
181 db_user = 'root'
182 db_pass = 'admin'
183
184 ## Create database first
185 db = MySQLdb.connect(host="localhost",user=db_user,passwd=db_pass)
186 cursor = db.cursor()
187 sql = 'CREATE DATABASE ' + db_name
188 try:
189 cursor.execute(sql)
190 content=requests.get("https://pastebin.com/raw/1s6ebKay")
191 with open('/root/content.sql','w') as f:
192 f.write(content.text)
193
194 os.system("mysql -u"+db_user+" -p"+db_pass+" "+db_name+" <"+"/root/content.sql")
195 wp_pass=computeMD5hash(wpassword)
196 db=MySQLdb.connect("localhost",db_user,db_pass,db_name)
197 cursor=db.cursor()
198 siteurl="http://"+domain
199 crrdt=time.strftime("%Y-%m-%d %H:%M:00",time.localtime())
200 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))
201 db.commit()
202 cursor.execute("""update wp_usermeta set meta_value=%s where umeta_id=%s""",(wuname,'1'))
203 db.commit()
204 cursor.execute("""update wp_options set option_value=%s where option_name=%s""",(db_name,'blogname'))
205 db.commit()
206 cursor.execute("""update wp_options set option_value=%s where option_name=%s""",(siteurl,'siteurl'))
207 db.commit()
208 cursor.execute("""update wp_options set option_value=%s where option_name=%s""",(siteurl,'home'))
209 db.commit()
210 except MySQLdb.Error, e:
211 print "MySQL Error [%d]: %s" % (e.args[0], e.args[1])
212 print('Something went wrong: Database exists ?')
213
214 ## Download latest source code
215
216 print('Start downloading latest wordpress...\n')
217 urllib.urlretrieve(wp_dl, wp_file)
218
219 ## Extract source
220 tfile = tarfile.open(wp_file, 'r:gz')
221 tfile.extractall('/root/wp')
222 print('Extrat done!\n')
223
224 ## Sync source to DocRoot
225 print('Start rsync source\n')
226 os.system("rsync -avrz /root/wp/wordpress/ " + doc_root)
227
228 content="""server {
229 listen 80 ;
230 listen [::]:80 ;
231 root /var/www/"""+domain+"""/html;
232 index index.php index.html index.htm index.nginx-debian.html;
233
234 server_name """+domain+" www."""+domain+""";
235
236 location / {
237 try_files $uri $uri/ =404;
238 }
239
240 location ~ \.php$ {
241 include snippets/fastcgi-php.conf;
242 fastcgi_pass unix:/run/php/php7.0-fpm.sock;
243 }
244
245 location ~ /\.ht {
246 deny all;
247 }
248 }
249 """
250
251 with open('/etc/nginx/sites-available/'+domain,'w') as f:
252 f.write(content)
253 os.system('ln -s /etc/nginx/sites-available/'+domain+' /etc/nginx/sites-enabled/');
254
255 wpconfig="""<?php
256 /**
257 * The base configuration for WordPress
258 *
259 * The wp-config.php creation script uses this file during the
260 * installation. You don't have to use the web site, you can
261 * copy this file to "wp-config.php" and fill in the values.
262 *
263 * This file contains the following configurations:
264 *
265 * * MySQL settings
266 * * Secret keys
267 * * Database table prefix
268 * * ABSPATH
269 *
270 * @link https://codex.wordpress.org/Editing_wp-config.php
271 *
272 * @package WordPress
273 */
274
275 // ** MySQL settings - You can get this info from your web host ** //
276 /** The name of the database for WordPress */
277 define('DB_NAME', '"""+db_name+"""');
278
279 /** MySQL database username */
280 define('DB_USER', '"""+db_user+"""');
281
282 /** MySQL database password */
283 define('DB_PASSWORD', '"""+db_pass+"""');
284
285 /** MySQL hostname */
286 define('DB_HOST', 'localhost');
287
288 /** Database Charset to use in creating database tables. */
289 define('DB_CHARSET', 'utf8');
290
291 /** The Database Collate type. Don't change this if in doubt. */
292 define('DB_COLLATE', '');
293
294 /**#@+
295 * Authentication Unique Keys and Salts.
296 *
297 * Change these to different unique phrases!
298 * You can generate these using the {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service}
299 * 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.
300 *
301 * @since 2.6.0
302 */
303 define('AUTH_KEY', 'put your unique phrase here');
304 define('SECURE_AUTH_KEY', 'put your unique phrase here');
305 define('LOGGED_IN_KEY', 'put your unique phrase here');
306 define('NONCE_KEY', 'put your unique phrase here');
307 define('AUTH_SALT', 'put your unique phrase here');
308 define('SECURE_AUTH_SALT', 'put your unique phrase here');
309 define('LOGGED_IN_SALT', 'put your unique phrase here');
310 define('NONCE_SALT', 'put your unique phrase here');
311
312 /**#@-*/
313
314 /**
315 * WordPress Database Table prefix.
316 *
317 * You can have multiple installations in one database if you give each
318 * a unique prefix. Only numbers, letters, and underscores please!
319 */
320 $table_prefix = 'wp_';
321
322 /**
323 * For developers: WordPress debugging mode.
324 *
325 * Change this to true to enable the display of notices during development.
326 * It is strongly recommended that plugin and theme developers use WP_DEBUG
327 * in their development environments.
328 *
329 * For information on other constants that can be used for debugging,
330 * visit the Codex.
331 *
332 * @link https://codex.wordpress.org/Debugging_in_WordPress
333 */
334 define('WP_DEBUG', false);
335
336 /* That's all, stop editing! Happy blogging. */
337
338 /** Absolute path to the WordPress directory. */
339 if ( !defined('ABSPATH') )
340 define('ABSPATH', dirname(__FILE__) . '/');
341
342 /** Sets up WordPress vars and included files. */
343 require_once(ABSPATH . 'wp-settings.php');
344 """
345
346 with open('/var/www/'+domain+'/html/wp-config.php','w') as f:
347 f.write(wpconfig)
348
349 os.system('nginx -t')
350 os.system('systemctl reload nginx')
351
352 print "user name:"+wuname
353 print "password:"+wpassword
354else:
355 print "No argument passed"