Wordpress Auto-Installer

Perl, Wordpress February 20th, 2008

I've written a Wordpress auto-installer that takes all of the labour out of setting up a new Wordpress v2.3.3 installation if you're running cPanel 10 on a Linux server. All you have to do is:

  • Install the required Perl modules (see below)
  • Place the folder 'wordpress' in the same folder as the script
  • Run the script via the command line and watch it go!

It will create a database, database user, etc, and if you're running it on a subdomain, it will change the Wordpress settings so that it works on subdomains out of the box. Then it will upload all of the files in the 'wordpress' directory mentioned above.

Of course this code comes with absolutely no warranty as to whether it works on your server, and you use it at your own risk... You'll need to install the Net::Recursive module from CPAN before running this script. Please note that the version of this module I used fucked up binary file uploads (ie images), so you might want to customise it.

PERL:
  1. #!/usr/bin/perl -w
  2.  
  3. # Wordpress Auto-Installer v0.1 for Wordpress v2.3.3
  4. # This script is provided 'as is' under a Creative Commons License:
  5. #  http://creativecommons.org/licenses/by-nc-sa/3.0/us/
  6.  
  7. # If you find this useful, drop a link to:
  8. #  http://www.pagespank.com/
  9.  
  10. use strict;
  11. use WWW::Mechanize;
  12. use Net::FTP;
  13. use Net::FTP::Recursive;
  14.  
  15. print "Please enter Base URL of site (ie xyz.com): ";
  16. my $baseurl = <STDIN>;
  17. $baseurl =~ s/^http:\/\///;
  18. $baseurl =~ s/^www\.//;
  19. chomp($baseurl);
  20. print "Please enter cPanel location (ie ':2082' or '/cpanel'): ";
  21. my $cpanel = <STDIN>;
  22. chomp($cpanel);
  23. print "Please enter cPanel user: ";
  24. my $cpuser = <STDIN>;
  25. chomp($cpuser);
  26. print "Please enter cPanel pass: ";
  27. my $cppass = <STDIN>;
  28. chomp($cppass);
  29. print "Please enter DB name: ";
  30. my $db = <STDIN>;
  31. chomp($db);
  32. print "Please enter DB user: ";
  33. my $dbuser = <STDIN>;
  34. chomp($dbuser);
  35. print "Please enter DB pass: ";
  36. my $dbpass = <STDIN>;
  37. chomp($dbpass);
  38. print "Please enter WP destination folder (if root install, just press enter): ";
  39. my $wpdest = <STDIN>;
  40. chomp($wpdest);
  41. print "Subdomain install? (y/n): ";
  42. my $sub = <STDIN>;
  43. chomp($sub);
  44. print "Blog title: ";
  45. my $blogtitle = <STDIN>;
  46. chomp($blogtitle);
  47. print "Admin email address: ";
  48. my $email = <STDIN>;
  49. chomp($email);
  50. print "FTP location (ie 'ftp.xyz.com' / 'xyz.com'): ";
  51. my $ftploc = <STDIN>;
  52. chomp($ftploc);
  53. print "Default public FTP folder (ie 'public_html' / 'www'): ";
  54. my $ftpbase = <STDIN>;
  55. chomp($ftpbase);
  56. $ftpbase =~ s/^\///;
  57. $ftpbase =~ s/\/$//;
  58. print "Blogroll keyword (for use to gather blogroll links): ";
  59. my $blogroll = <STDIN>;
  60. chomp($blogroll);
  61.  
  62. my $mech = WWW::Mechanize->new( agent => 'wp-auto-installer 0.1' );
  63. $cpanel =~ s/http:\/\///;
  64. $cpanel =~ s/\/$//;
  65. $mech->credentials( $cpuser => $cppass );
  66.  
  67. my $url = "http://" . $baseurl . $cpanel . "/frontend/x/sql/";
  68. $mech->get($url);
  69.  
  70. if ($mech->response->is_success) {
  71.     $mech->get($url . "adddb.html?db=$db");
  72.     if ($mech->response->is_success) {
  73.         $mech->get($url . "adduser.html?user=$dbuser&pass=$dbpass");
  74.         if ($mech->response->is_success) {
  75.             $mech->get($url . "addusertodb.html?user=$cpuser" . "_$dbuser&db=$cpuser" . "_$db&ALL=ALL");
  76.             unless ($mech->response->is_success) { die "Error adding user to DB"; }
  77.         } else { die "Error creating user"; }
  78.     } else { die "Error creating database."; }
  79. } else { die "Error retrieving cPanel page."; }
  80.  
  81. my $config_old = 'wordpress/wp-config-sample.php';
  82. my $config_file = 'wordpress/wp-config.php';
  83. my $wpdbname = $cpuser . "_" . $db;
  84. my $wpdbuser = $cpuser . "_" . $dbuser;
  85.  
  86. # Change config file with DB settings etc and make sure it works ok on subdomains
  87. open (OLD, "<$config_old") or die "Couldn't open sample config file: $!";
  88. open (CONFIG, ">$config_file") or die "Couldn't open config file: $!";
  89. my $count = 0;
  90. my $wphome;
  91. if ($sub =~ m/(?:y|yes)/i) {
  92.     $wphome = "http://$wpdest.$baseurl";
  93. } else {
  94.     $wphome = "http://www.$baseurl/$wpdest";
  95. }
  96. while (<OLD>) {
  97.     my $line = $_;
  98.     $line =~ s/'DB_NAME', '[^']*'/'DB_NAME', '$wpdbname'/;
  99.     $line =~ s/'DB_USER', '[^']*'/'DB_USER', '$wpdbuser'/;
  100.     $line =~ s/'DB_PASSWORD', '[^']*'/'DB_PASSWORD', '$dbpass'/;
  101.     print CONFIG $line;
  102.     if ($count == 9) {
  103.         print CONFIG "\ndefine('WP_SITEURL', '$wphome'); # added by wp auto-installer\ndefine('WP_HOME', '$wphome'); # added by wp auto-installer\n\n";
  104.     }
  105.     $count++;
  106. }
  107. close OLD;
  108. close CONFIG;
  109. # Change blogroll links
  110. # scrape the big G
  111. $blogroll =~ s/\s+/+/g;
  112. my $goorl = 'http://www.google.com/search?q=' . $blogroll;
  113. $mech->get($goorl) or die $mech->response->status_line;
  114. my $gcontent = $mech->content;
  115. my $gi = 0;
  116. my (@gurl, @gtitle);
  117. while ( ($gcontent =~ m{<div class=g><a href="(.*?)" class=l>(.*?)</a>}gis) && ($gi <7) ) {
  118.     push(@gurl,$1);
  119.     my $cleantitle = $2;
  120.     $cleantitle =~ s/<b>//g;
  121.     $cleantitle =~ s/<\/b>//g;
  122.     push(@gtitle,$cleantitle);
  123.     $gi++;
  124. }
  125. my $blogold = 'wordpress/wp-admin/includes/upgrade.php';
  126. my $blognew = 'wordpress/wp-admin/includes/upgrade.tmp.php';
  127. open (BLOGOLD, "<$blogold") or die "Couldn't open blogroll file: $!";
  128. open (BLOGNEW, ">$blognew") or die "couldn't change blogroll file";
  129. my $bi = 0;
  130. while (<BLOGOLD>) {
  131.     my $line = $_;
  132.     if ($line =~ m{\$wpdb->query\("INSERT INTO \$wpdb->links \(link_url, link_name, link_category, link_rss, link_notes\) VALUES \('.*?', '.*?', 0, '.*?', '.*?'\);"\);}g) {
  133.         my $url   = $gurl[$bi];
  134.         my $title = $gtitle[$bi];
  135.         $line =~ s!\$wpdb->query\("INSERT INTO \$wpdb->links \(link_url, link_name, link_category, link_rss, link_notes\) VALUES \('.*?', '.*?', 0, '.*?', '.*?'\);"\);!\$wpdb->query("INSERT INTO \$wpdb->links (link_url, link_name, link_category, link_rss, link_notes) VALUES ('$url', '$title', 0, '', '');");!g;
  136.         $bi++;
  137.     }
  138.    
  139.     print BLOGNEW $line;
  140. }
  141. close BLOGOLD;
  142. close BLOGNEW;
  143. rename($blogold, "$blogold.old.php") or die "Can't rename old file";
  144. rename($blognew, $blogold) or die "Can't rename new file";
  145. # Upload files
  146. my $host = $ftploc;
  147. my $ftp = Net::FTP::Recursive->new($host, Debug => 1) or die "Cannot connect to $host: $@";
  148. my $local = 'wordpress';
  149. chdir $local or die "could not change directory to wordpress install";
  150. $ftp->login($cpuser,$cppass) or die "Cannot login ", $ftp->message;
  151. $ftp->cwd("/$ftpbase/$wpdest") or die "Cannot change working directory ", $ftp->message;
  152. $ftp->rput();
  153. $ftp->quit;
  154. # Get newly installed Wordpress URL
  155. my $wpurl;
  156. if ($sub =~ m/(?:y|yes)/i) {
  157.     $wpurl = "http://$wpdest.$baseurl/wp-admin/install.php";
  158. } else {
  159.     if ($wpdest ne '') {
  160.         $wpdest = $wpdest . '/';
  161.     }
  162.     $wpurl = "http://www.$baseurl/$wpdest" . "wp-admin/install.php";
  163. }
  164. $mech->get($wpurl);
  165. if ($mech->response->is_success) {
  166.     $mech->field("weblog_title",$blogtitle);
  167.     $mech->field("admin_email",$email);
  168.     $mech->click;
  169.     if ($mech->response->is_success) {
  170.         if ($mech->content =~ m{<strong>username</strong> "<code>(.*?)</code>" and <strong>password</strong> "<code>(.*?)</code>"}i) {
  171.             open (LOG, ">>install-log.txt");
  172.             print "Installation finished.\n\nYour username is: $1\n\nYour password is: $2";
  173.             print LOG "Installation finished.\n\nYour username is: $1\nYour password is: $2\n\n-----------------\n\n";
  174.             close LOG;
  175.         }
  176.     }
  177. }

If you don't 'get' perl, you can download a fully functional Windows executable below:

Wordpress Auto-Installer for Windows

Now remember to use this responsibly ;)

Share and Enjoy:
  • Digg
  • del.icio.us
  • Furl
  • Slashdot
  • Technorati
  • YahooMyWeb
  • Google

Leave a Reply