Skip to content

Instantly share code, notes, and snippets.

@finalwebsites
Last active September 15, 2019 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save finalwebsites/74f3745a457642cda3fb72f667842d22 to your computer and use it in GitHub Desktop.
Save finalwebsites/74f3745a457642cda3fb72f667842d22 to your computer and use it in GitHub Desktop.
Add a WordPress website via ServerPilot API and use WP-CLI for some modifications.
<?php
include_once 'ServerPilot.php';
include_once 'ServerPilotException.php';
function randomPassword($pw_length = 8) {
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
$pass = array();
$alphaLength = strlen($alphabet) - 1;
for ($i = 0; $i < $pw_length; $i++) {
$n = rand(0, $alphaLength);
$pass[] = $alphabet[$n];
}
return implode($pass);
}
// Add here your ServerPilot details
$client_id = 'cid_XXXXX';
$api_key = 'XXXXX';
$server_id = 'XXXXX'; // copy paste from the browser address bar
/***************************************************************
Add here the data for the new app and WordPress
You will change this every time if you use this script
***************************************************************/
$username = 'user';
$appname = 'myapp';
$php_ver = 'php7.2'; // php7.3
$domain = 'app.wordtest.nl';
$admin_email = 'email@example.com';
$pwd = randomPassword(12);
echo PHP_EOL;
$config = array('id' => $client_id, 'key' => $api_key);
$sp = new ServerPilot($config);
$user_id = '';
// First check if the user on this server exists
$systemUsers = $sp->sysuser_list();
foreach ($systemUsers->data as $user) {
if ($user->serverid == $server_id && $user->name == $username) {
$user_id = $user->id;
echo 'Existing user name "'.$username.'"'.PHP_EOL;
break;
}
}
// The user doesn't exists, let's create system user
if ($user_id == '') {
$systemUser = $sp->sysuser_create($server_id, $username, $pwd);
$user_id = $systemUser->data->id;
}
// First check if the app (name) on this server exists
$apps = $sp->app_list();
foreach ($apps->data as $ap) {
if ($ap->serverid == $server_id && $ap->name == $appname) {
die('There is already an app with this name'.PHP_EOL);
}
}
// Create an app in Serverpilot
$wp_site_data = array(
'site_title' => ucfirst($appname),
'admin_user' => $appname.'_admin',
'admin_password' => randomPassword(12),
'admin_email' => $admin_email
);
$app = $sp->app_create($appname, $user_id, $php_ver, array($domain), $wp_site_data);
echo 'App and WordPress website created.'.PHP_EOL;
sleep(5); // wait a few seconds to finish the previous tasks
echo 'Change some settings for the WP website...'.PHP_EOL;
// WP is installed, let's install some plugins and change some settings
$path = '/srv/users/'.$username.'/apps/'.$appname.'/public/';
$empty_desc = shell_exec(sprintf('sudo -u %s -i -- wp option delete blogdescription --path=%s', $username, $path));
$timezone = shell_exec(sprintf('sudo -u %s -i -- wp option update timezone_string "Europe/Amasterdam" --path=%s', $username, $path));
$date_format = shell_exec(sprintf('sudo -u %s -i -- wp option update date_format "d/m/Y" --path=%s', $username, $path));
$time_format = shell_exec(sprintf('sudo -u %s -i -- wp option update time_format "H:i" --path=%s', $username, $path));
$blog_public = shell_exec(sprintf('sudo -u %s -i -- wp option update blog_public 0 --path=%s', $username, $path));
$default_pingback_flag = shell_exec(sprintf('sudo -u %s -i -- wp option delete default_pingback_flag --path=%s', $username, $path));
$default_ping_status = shell_exec(sprintf('sudo -u %s -i -- wp option delete default_ping_status --path=%s', $username, $path));
$permalink_structure = shell_exec(sprintf('sudo -u %s -i -- wp option update permalink_structure "/%%postname%%/" --path=%s', $username, $path));
echo 'All done!'.PHP_EOL;
@finalwebsites
Copy link
Author

finalwebsites commented Sep 15, 2019

I'm using ServerPilot now for several years and I think I created more that 200 apps with the "One click" WordPress feature.
The feature is great and is a time saver for sure, but after that I change in WordPress always the some settings:

  • Install the Dutch language
  • changing the date and time format
  • hide "new" websites from search engines
  • fix the comment settings
  • and of course install some common plugins.

This small script is using the ServerPilot API and WP-CLI to create a custom WordPress installation before you login for the first time.

How to use?
First download the ServerPilot API Wrapper from Dave Rogers and upload the two files with this script to your servers root directory. You need root access because otherwise you need to enter a password for all the sudo calls if you run WP-CLI commands.
Change the client ID, API Key and server ID and than add the data for the first app you like to create.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment