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