0
Category

WordPress

Automatically Install WordPress and Create Database on Plesk

By WordPress No Comments

Overview

This code was inspired, well, by laziness.

It automatically creates a MySql database and User using the provided information; Downloads the latest version of WP and unzips it; Changes your config-wp.php to work with the newly created database. Uses SSH2 to connect to shell.

Feel free to use it as is or modify it to your needs. Please report bugs and fixes.

Tested on Plesk 12.0.18.

Requirements

  1. Odin (former Parallels) Plesk
  2. Root access
  3. PHP 5+
Download ZIP
<?php

///////////////////////
// CONFIG
///////////////////////

$rootuser = "root";     // the root user
$rootpass = "PASSWORD"; // the root password
$domainspath = "/var/www/vhosts"; //path to all your domains

///////////////////////
///////////////////////

$db_name = $_REQUEST['db'];
$domain = $_REQUEST['domain'];
$dir = $_REQUEST['dir'];
$user = $_REQUEST['user'];
$pass = $_REQUEST['pass'];
$del = $_REQUEST['del'];
?>

<style>
body {
font-family:sans-serif;
margin:1em;
font-size:1.2em;
color:grey
}

input {
font-size:1.2em;
padding:.3em;
border-radius:4px;
border:1px solid #DEDEDE
}

label {
margin-top:1em
}

input[type='text'] {
width:600px
}

label,input {
display:block
}

.small {
font-size:.5em;
text-transform:uppercase
}

input[type='submit'] {
padding:.5em 1em;
margin:1em 0;
background:#7c3
}

input[type='checkbox'] {
display:inline;
width:.8em;
height:.8em
}
</style>

<h1>Install WordPress &amp; Create Database</h1>
<form id="doit" method="post">
  <label>
    Create Database
    <input id="db" name="db" type="text" />
  </label>
  <label>
    Domain
    <input id="domain" name="domain" type="text" />
  </label>
  <label>
    Directory
    <input id="dir" name="dir" type="text" />
  </label>
  <label>
    Username
    <input id="user" name="user" type="text" />
  </label>
  <label>
    Password
    <input id="pass" name="pass" type="text" />
    <span class="small">(Must not contain username)</span>
  </label>
  <label>
    Delete folder contents? <input type="checkbox" name="del" value="Yes" />
  </label>
  <input type="submit" value="Install WP">
</form>

<?php

echo "<pre>";

if(empty($domain)){ die("API usage syntax example: ?db=mydb&domain=domain.com&dir=projects/company&user=user&pass=pass"); }
 

//////////////////////////////
// Connect to SSH
//////////////////////////////

//Incude SSH2 phpseclib to use shell commands
set_include_path(get_include_path() . 'SSH2' . PATH_SEPARATOR . 'phpseclib');
include('SSH2/Net/SSH2.php');
$ssh = new Net_SSH2('localhost');
if (!$ssh->login($rootuser, $rootpass)) {
    exit('SSH2 Login Failed');
}


/////////////////////////////
// CREATE USER
/////////////////////////////
echo $ssh->exec("/usr/sbin/useradd -g psacln -d /var/www/vhosts/".$domain."/httpdocs/".$dir." ".$user);
//CHMOD to User
echo $ssh->exec("chown -R ".$user.":psacln /var/www/vhosts/".$domain."/httpdocs/".$dir); 
//echo $ssh->exec("chown -R apache:psaserv /var/www/vhosts/".$domain."/httpdocs/".$dir); //CHMOD to Apache
echo $ssh->exec("chmod 775 /var/www/vhosts/".$domain."/httpdocs/".$dir);

/////////////////////////////
// Change Password
/////////////////////////////

$ssh->write("passwd ".$user."\n");
echo $ssh->read('New UNIX password:');
$ssh->write($pass."\n");
echo $ssh->read('Retype new UNIX password:');
$ssh->write($pass."\n");
echo $ssh->read('passwd: all authentication tokens updated successfully.');

echo "<p>===== End Create User =====</p>";

//////////////////////////
// Plesk DB Create
//////////////////////////

echo $ssh->exec('cd '.$domainspath);

echo $ssh->exec('/usr/local/psa/bin/database --create '.$db_name.' -domain '.$domain.' -print-id -server localhost -type mysql');
echo $ssh->exec('/usr/local/psa/bin/database --update '.$db_name.' -add_user '.$user.' -passwd '.$pass);
echo "<p>===== End DB Create =====</p>";
echo die();

////////////////////////////////////
// Download and Untar wordpress
////////////////////////////////////
$c = 'cd '.$domainspath.'/'.$domain.'/httpdocs/;';

if(!empty($dir)){
    $c .= 'mkdir -p '.$dir.';'
    .'cd '.$dir.';';
}

if(isset($del) && $del == 'Yes'){
    $c .='rm * -r;';  //DELETES EVEYTHING INSIDE 
}

$c .='wget --no-check-certificate https://www.wordpress.org/latest.tar.gz;'
    .'tar xfvz latest.tar.gz;'
    .'mv wordpress/* .;'
    .'rmdir wordpress;'
    .'rm latest.tar.gz;'
    .'cp wp-config-sample.php wp-config.php;'
    .'perl -pi -e "s/database_name_here/'.$db_name.'/g" wp-config.php;'
    .'perl -pi -e "s/username_here/'.$user.'/g" wp-config.php;'
    .'perl -pi -e "s/password_here/'.$pass.'/g" wp-config.php;'
    .'mkdir wp-content/uploads;'
    .'chmod 777 wp-content/uploads;';


$pc = str_replace(';', ';<br>', $c);
echo '<br>$ '.$pc;
echo $ssh->exec($c);

echo "<p>===== End WP download & wp-config =====</p>";
?>
</pre>