Last active
July 19, 2022 12:19
-
-
Save amityweb/aebe560247903165bab0ad3cc6b398c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Replace variables with yours | |
// Run script with "php clone_wp.php" | |
// Requires All in One WP Migration Unlimited stored locally and path updated below - other backup plugins can be used with some teaks to WP CLI commands | |
// Requires All in One Backup file created source website and path and filename copied to that below | |
// iThemes securiy is used on source system to hide the admin URL | |
// WP Migrate DB is installed in source system to do find and replace of some settings, like a new Admin URL | |
// Check WP Config settings at bottom, you probably will want your own or none of these | |
$rc_server_id = '12345'; | |
$api_key = '1234567890qwertyuiopasdfghjkl'; | |
$api_secret = '1234567890qwertyuiopasdfghjkl'; | |
// Settings | |
$webapp_domain = 'mydomainname.rc1.amityweb.co.uk'; | |
$wp_title = 'Website Title'; | |
$wp_email = '[email protected]'; | |
$wp_user = 'myadminuser'; | |
$wp_pass = 'Xyq?3T^e59qYV$Q9'; | |
// Skeleton Wpress File | |
$skeleton_file_name = 'mywpsite.co.uk-20220310-122641-o0myww.wpress'; | |
$skeleton_file = 'https://mywpsite.co.uk/wp-content/ai1wm-backups/'.$skeleton_file_name; | |
// All In One Unlimited File | |
$all_in_one_unlimited_file_name = '/home/all-in-one-wp-migration-unlimited-extension.zip'; | |
// System User | |
$username_arr = explode(".", $webapp_domain, 2); | |
$username = $username_arr[0]; | |
$password = randomPassword(); | |
// Database | |
$database_name = str_replace('-', '_',str_pad($username, 5, "___")); | |
$database_user = $database_name; | |
$database_pass = randomDBPassword(8); | |
// Alias | |
//$webapp_alias = 'sub-domain.my-domain.co.uk'; | |
// Admin Login | |
$admin_url = preg_replace("/[^a-z ]/", '', $username).'admin'.randomAdminUrl(7); | |
// Customer Login | |
$customer_email = $username.'@myemail.co.uk'; // | |
$customer_user = $username; | |
$customer_pass = randomPassword(); | |
/* CREATE USER */ | |
// Create System User | |
$endpoint = 'servers/'.$rc_server_id.'/users'; | |
$data = array( | |
'username' => $username, | |
'password' => $password | |
); | |
$user = curlexec($endpoint, $data); | |
// Create Database | |
$endpoint = 'servers/'.$rc_server_id.'/databases'; | |
$data = array( | |
'name' => $database_name, | |
); | |
$database_obj = curlexec($endpoint, $data); | |
// Create Database User | |
$endpoint = 'servers/'.$rc_server_id.'/databaseusers'; | |
$data = array( | |
'username' => $database_user, | |
'password' => $database_pass, | |
); | |
$database_user_obj = curlexec($endpoint, $data); | |
// Grant Database User | |
$endpoint = 'servers/'.$rc_server_id.'/databases/'.$database_obj->id.'/grant'; | |
$data = array( | |
'id' => $database_user_obj->id, | |
); | |
curlexec($endpoint, $data); | |
// Create Web Application | |
$webapp_name = $username; // The name may only contain letters, numbers, dashes and underscores | |
$endpoint = 'servers/'.$rc_server_id.'/webapps/custom'; | |
$data = array( | |
'name' => $webapp_name, | |
"domainName" => $webapp_domain, | |
'user' => $user->id, | |
'publicPath' => null, | |
'phpVersion' => 'php74rc', | |
'stack' => 'hybrid', | |
'stackMode' => 'production', | |
'clickjackingProtection' => true, | |
'xssProtection' => true, | |
'mimeSniffingProtection' => true, | |
'processManager' => 'ondemand', | |
'processManagerMaxChildren' => 50, | |
'processManagerMaxRequests' => 500, | |
'openBasedir' => "/home/$username/webapps/$username:/var/lib/php/session:/tmp", | |
'timezone' => 'UTC', | |
'disableFunctions' =>'getmyuid,passthru,leak,listen,diskfreespace,tmpfile,link,ignore_user_abort,shell_exec,dl,exec,system,highlight_file,source,show_source,fpassthru,virtual,posix_ctermid,posix_getcwd,posix_getegid,posix_geteuid,posix_getgid,posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid,posix,_getppid,posix_getpwuid,posix_getrlimit,posix_getsid,posix_getuid,posix_isatty,posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid,posix_setpgid,posix_setsid,posix_setuid,posix_times,posix_ttyname,posix_uname,proc_open,proc_close,proc_nice,proc_terminate,escapeshellcmd,ini_alter,popen,pcntl_exec,socket_accept,socket_bind,socket_clear_error,socket_close,socket_connect,symlink,posix_geteuid,ini_alter,socket_listen,socket_create_listen,socket_read,socket_create_pair,stream_socket_server', | |
'maxExecutionTime' => 30, | |
'maxInputTime' => 60, | |
'maxInputVars' => 1000, | |
'memoryLimit' => 256, | |
'postMaxSize' => 256, | |
'uploadMaxFilesize' => 256, | |
'sessionGcMaxlifetime' => 1440, | |
'allowUrlFopen' => true | |
); | |
$webapp = curlexec($endpoint, $data); | |
// Add Web Alias | |
$endpoint = 'servers/'.$rc_server_id.'/webapps/'.$webapp->id.'/domains'; | |
$data = array( | |
'name' => 'www.'.$webapp_domain, | |
); | |
curlexec($endpoint, $data); | |
if(isset($webapp_alias)) | |
{ | |
$endpoint = 'servers/'.$rc_server_id.'/webapps/'.$webapp->id.'/domains'; | |
$data = array( | |
'name' => $webapp_alias, | |
); | |
curlexec($endpoint, $data); | |
} | |
// Install SSL | |
$endpoint = 'servers/'.$rc_server_id.'/webapps/'.$webapp->id.'/ssl'; | |
$data = array( | |
'provider' => 'letsencrypt', | |
'enableHttp' => false, | |
'enableHsts' => true, | |
'environment' => 'live', | |
'authorizationMethod' => 'http-01', | |
); | |
curlexec($endpoint, $data); | |
sleep(5); // Cant remember why | |
echo shell_exec("cp /home/wp-cli.yml /home/$username/webapps/$username/"); | |
echo shell_exec("wp core download --path=/home/$username/webapps/$username/ --locale=en_GB --allow-root"); | |
echo shell_exec("wp config create --dbname=$database_name --dbuser=$database_user --dbpass=$database_pass --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp core install --url=$webapp_domain --title='$wp_title' --admin_user=$wp_user --admin_password=$wp_pass --admin_email=$wp_email --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("chown -Rf $username:$username /home/$username/webapps/$username/*"); | |
echo shell_exec("wp rewrite structure '/%postname%/' --hard --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp rewrite flush --hard --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("chown $username:$username /home/$username/webapps/$username/.htaccess"); | |
// Install All in One Migration | |
echo shell_exec("wp plugin install all-in-one-wp-migration --activate --path=/home/$username/webapps/$username/ --allow-root"); | |
// Install All in One Migration Pro | |
echo shell_exec("wp plugin install $all_in_one_unlimited_file_name --activate --path=/home/$username/webapps/$username/ --allow-root"); | |
// Restore Skeleton | |
echo shell_exec("wget --no-check-certificate --directory-prefix=/home/$username/webapps/$username/wp-content/ai1wm-backups/ $skeleton_file"); | |
echo shell_exec("yes | wp ai1wm restore $skeleton_file_name --path=/home/$username/webapps/$username/ --allow-root"); | |
// Restore Skeleton | |
echo shell_exec("wp rewrite flush --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("rm /home/$username/webapps/$username/index.html"); | |
echo shell_exec("rm /home/$username/webapps/$username/wp-content/ai1wm-backups/$skeleton_file_name"); | |
/* Create Customer Admin */ | |
echo shell_exec("wp user create $customer_user $customer_email --user_pass='$customer_pass' --role=administrator --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp rewrite flush --hard --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("chown -Rf $username:$username /home/$username/webapps/$username/*"); | |
echo shell_exec("chown $username:$username /home/$username/webapps/$username/.htaccess"); | |
echo shell_exec("wp migratedb find-replace --find='Amity Base' --replace='$wp_title' --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp migratedb find-replace --find='skel289jsk289fnsa' --replace='$admin_url' --path=/home/$username/webapps/$username/ --allow-root"); | |
// These are our own personal preferences in WP Config, so adjust accordingly | |
echo shell_exec("wp config set WP_POST_REVISIONS 10 --raw --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp config set DISABLE_WP_CRON true --raw --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp config set WP_DEBUG false --raw --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp config set WP_SITEURL 'https://$webapp_domain/' --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp config set WP_HOME 'https://$webapp_domain/' --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp config set WP_CACHE_KEY_SALT \'$webapp_domain\' --raw --path=/home/$username/webapps/$username/ --allow-root"); | |
echo shell_exec("wp config set AUTOMATIC_UPDATER_DISABLED true --raw --path=/home/$username/webapps/$username/ --allow-root"); | |
define( '', true ); // Can't remember what this was for | |
$message = ''; | |
$message .= "Find your site at https://$webapp_domain\n"; | |
$message .= "Find your site admin at https://$webapp_domain/$admin_url\n\n"; | |
$message .= "Last Pass Data\n\n"; | |
$message .= "-------------------------------\n"; | |
$message .= "Domain\n"; | |
$message .= "-------------------------------\n"; | |
$message .= "URL: https://$webapp_domain\n"; | |
$message .= "\n"; | |
$message .= "-------------------------------\n"; | |
$message .= "Site\n"; | |
$message .= "-------------------------------\n"; | |
$message .= "URL: https://$webapp_domain\n"; | |
$message .= "Admin URL: https://$webapp_domain/$admin_url\n"; | |
$message .= "\n"; | |
$message .= "-------------------------------\n"; | |
$message .= "SFTP\n"; | |
$message .= "-------------------------------\n"; | |
$message .= "Host: rc1.amityweb.co.uk\n"; | |
$message .= "Username: $username\n"; | |
$message .= "Password: $password\n"; | |
$message .= "\n"; | |
$message .= "-------------------------------\n"; | |
$message .= "ADMINS\n"; | |
$message .= "-------------------------------\n"; | |
$message .= "Admin URL: https://$webapp_domain/$admin_url\n"; | |
$message .= "Username: $customer_user\n"; | |
$message .= "Password: $customer_pass\n"; | |
$message .= "\n"; | |
echo $message; | |
// send email | |
mail($wp_email,"New Wordpress Site ".$webapp_domain,$message); | |
function curlexec($endpoint, $data = null) | |
{ | |
global $api_key, $api_secret; | |
echo "--------------------------------------------\n"; | |
echo "Starting: $endpoint\n"; | |
echo "--------------------------------------------\n"; | |
echo "Data:"; | |
echo '<pre>'; | |
print_r($data); | |
echo '</pre>'; | |
// Authentication | |
$api_key_secret = $api_key . ':' . $api_secret; | |
$api_url = 'https://manage.runcloud.io/api/v2/'.$endpoint; | |
//echo $api_url."\n"; | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | |
'accept: application/json', | |
'content-type: application/json' | |
)); | |
curl_setopt_array($ch, [ | |
CURLOPT_RETURNTRANSFER => 1, | |
CURLOPT_URL => $api_url, | |
CURLOPT_USERPWD => $api_key_secret, | |
]); | |
if($data) | |
{ | |
$query = json_encode($data); | |
curl_setopt_array($ch, [ | |
CURLOPT_POSTFIELDS => $query | |
]); | |
} | |
$resp = json_decode(curl_exec($ch)); | |
curl_close($ch); | |
echo '<pre>'; | |
print_r($resp); | |
echo '</pre>'; | |
if($resp->errors) | |
{ | |
die(); | |
} | |
else | |
{ | |
echo "--------------------------------------------\n\n"; | |
echo "Finished: $endpoint\n"; | |
echo "--------------------------------------------\n\n"; | |
} | |
sleep(3); | |
return $resp; | |
} | |
function randomPassword() | |
{ | |
$alphabet = 'abcdefghijklmnopqrstuvwxyz'; | |
$capitals = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; | |
$numbers = '1234567890'; | |
$specialChars = '!@#$%^&8()-=?'; | |
$pass = array(); //remember to declare $pass as an array | |
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache | |
$capitalsLength = strlen($capitals) - 1; //put the length -1 in cache | |
$numbersLength = strlen($numbers) - 1; //put the length -1 in cache | |
$specialCharsLength = strlen($specialChars) - 1; //put the length -1 in cache | |
for ($i = 0; $i < 4; $i++) | |
{ | |
$n = rand(0, $alphaLength); | |
$pass[] = $alphabet[$n]; | |
} | |
for ($i = 0; $i < 4; $i++) | |
{ | |
$n = rand(0, $capitalsLength); | |
$pass[] = $capitals[$n]; | |
} | |
for ($i = 0; $i < 4; $i++) | |
{ | |
$n = rand(0, $numbersLength); | |
$pass[] = $numbers[$n]; | |
} | |
for ($i = 0; $i < 4; $i++) | |
{ | |
$n = rand(0, $specialCharsLength); | |
$pass[] = $specialChars[$n]; | |
} | |
shuffle($pass); | |
return implode($pass); //turn the array into a string | |
} | |
function randomDBPassword($numChars) | |
{ | |
$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; | |
$pass = array(); //remember to declare $pass as an array | |
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache | |
for ($i = 0; $i < $numChars; $i++) | |
{ | |
$n = rand(0, $alphaLength); | |
$pass[] = $alphabet[$n]; | |
} | |
return implode($pass); //turn the array into a string | |
} | |
function randomAdminUrl($numChars) | |
{ | |
$alphabet = 'abcdefghijklmnopqrstuvwxyz1234567890'; | |
$pass = array(); //remember to declare $pass as an array | |
$alphaLength = strlen($alphabet) - 1; //put the length -1 in cache | |
for ($i = 0; $i < $numChars; $i++) | |
{ | |
$n = rand(0, $alphaLength); | |
$pass[] = $alphabet[$n]; | |
} | |
return implode($pass); //turn the array into a string | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment