Softaculous 如何在 cPanel 中创建 MySQL 数据库、用户和 user->db 映射?
How does Softaculous create a MySQL database, user, and user->db mapping in cPanel?
目前,使用 PHP 代码,我在 cPanel 中创建 MySQL 数据库,然后是 MySQL 用户,然后将它们映射到数据库,使用这 4 PHP 套路。麻烦的是,每逢千载难逢,当我们认为它与服务器负载有关时,这个脚本说它创建了 MySQL、用户和 user->db 映射,但我们看不到它在 cPanel 中。当我们转到 MySQL 命令行时,我们会在那里看到它。 WordPress 无法部署到它——说它无法访问数据库。我们必须转到命令行 MySQL 来删除数据库。当我们在较低的服务器负载下手动 运行 这些例程时,问题就消失了。问题是,我通常会在问题实际发生一天后才收到回电——所以我看不出发生了什么。我的日志只显示 WordPress 无法部署到 MySQL 数据库。当我去查看时,我没有在 cPanel 中看到它,但在 MySQL.
的命令行中看到它
所以,既然这些套路都有问题,我想知道Softaculous是如何解决这个问题的。因为即使我的脚本失败,Softaculous 仍然可以正常工作。
<?php
class cPanel {
public static function cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData, $nTimeoutSeconds = 0) {
$sURL = ($sCpanelPort == 2083) ? 'https://' : 'http://';
$sURL .= $sRootDomain . ':' . $sCpanelPort . '/xml-api/cpanel';
$asHeader[0] = 'Authorization: Basic ' . base64_encode($sCpanelUser . ':' . $sCpanelPass) . "\r\n" .
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($sPostData) . "\r\n" . "\r\n" . $sPostData;
$hCurl = curl_init ();
curl_setopt($hCurl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt($hCurl, CURLOPT_REFERER, 'https://www.google.com/?gws_rd=ssl#q=' . $sURL);
curl_setopt($hCurl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($hCurl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($hCurl, CURLOPT_URL, $sURL);
curl_setopt($hCurl, CURLOPT_HTTPHEADER, $asHeader);
curl_setopt($hCurl, CURLOPT_POST, 1);
if ($nTimeoutSeconds > 0) {
@ curl_setopt($hCurl, CURLOPT_TIMEOUT, $nTimeoutSeconds);
@ curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, 30);
}
$s = curl_exec($hCurl);
return $s;
}
public static function cp_createDatabase($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBName) {
global $gsCpanel_Last_Result;
$sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adddb&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBName;
$nTimeoutSeconds = 60;
$s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData, $nTimeoutSeconds);
$gsCpanel_Last_Result = "$s";
if (strpos($s, '<result>1</result>') > 0) {
$sDBName = $sCpanelUser . '_' . $sUnPrefixedDBName;
return $sDBName;
} else {
return FALSE;
}
}
public static function cp_createDBUser($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBUser, $sDBPass) {
global $gsCpanel_Last_Result;
$sTest = $sCpanelUser . '_' . $sUnPrefixedDBUser;
if (strlen($sTest)>16) {
$sUnPrefixedDBUser = substr($sUnPrefixedDBUser, 0, (strlen($sUnPrefixedDBUser) - (strlen($sTest) - 16)));
}
$sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduser&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBUser . '&arg-1='. $sDBPass;
$s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData);
$gsCpanel_Last_Result = "$s";
if (strpos($s, '<result>1</result>') > 0) {
$sDBUser = $sCpanelUser . '_' . $sUnPrefixedDBUser;
return $sDBUser;
} else {
return FALSE;
}
}
public static function cp_addUserToDatabase($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBName, $sUnPrefixedDBUser) {
global $gsCpanel_Last_Result;
$sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduserdb&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBName . '&arg-1=' . $sUnPrefixedDBUser . '&arg-2=all';
$s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData);
$gsCpanel_Last_Result = "$s";
return (strpos($s, '<result>1</result>') > 0);
}
} // end class
Softaculous 有一个很棒的 SDK,可在此处免费获得:
http://www.softaculous.com/docs/File:Softaculous_Development_Kit.zip
那么,这段代码应该可以帮助您安装一个使用它的博客。它的速度非常快——比 cPanel API 快得多。
<?php
// SOURCE: https://gist.github.com/mickaelandrieu/7271939
require_once('sdk.php');
$new = new Softaculous_API();
$new->login = 'https://user:password@domain.com:2083/frontend/x3/softaculous/index.live.php';
// Domain Name
$data['softdomain'] = 'root-or-addon-domain.com'; // OPTIONAL - By Default the primary domain will be used
// The directory is relative to your domain and should not exist. e.g. To install at http://mydomain/dir/ just type dir. To install only in http://mydomain/ leave this empty.
$data['softdirectory'] = ''; // OPTIONAL - By default it will be installed in the /public_html folder
// Admin Username
$data['admin_username'] = 'admin';
// Admin Pass
$data['admin_pass'] = 'pass';
// Admin Email
$data['admin_email'] = 'admin@domain.com';
// Database
$data['softdb'] = 'wp887'; // make something up that MySQL likes and won't cause a collision
//Database User Name
$data['dbusername'] = 'wp887'; // ditto
// DB User Pass
$data['dbuserpass'] = 'wp887'; // ditto
// Language
$data['language'] = 'en';
// Site Name
$data['site_name'] = 'Wordpess wp887';
// Site Description
$data['site_desc'] = 'WordPress API Test';
// Response
$res = $new->install(26, $data); // Will install WordPress(26 is its script ID)
// Unserialize
$res = unserialize($res);
// Done/Error
if(!empty($res['done'])){
echo 'Installed';
}else{
echo 'Installation Failed<br/>';
if(!empty($res['error'])){
print_r($res['error']);
}
}
目前,使用 PHP 代码,我在 cPanel 中创建 MySQL 数据库,然后是 MySQL 用户,然后将它们映射到数据库,使用这 4 PHP 套路。麻烦的是,每逢千载难逢,当我们认为它与服务器负载有关时,这个脚本说它创建了 MySQL、用户和 user->db 映射,但我们看不到它在 cPanel 中。当我们转到 MySQL 命令行时,我们会在那里看到它。 WordPress 无法部署到它——说它无法访问数据库。我们必须转到命令行 MySQL 来删除数据库。当我们在较低的服务器负载下手动 运行 这些例程时,问题就消失了。问题是,我通常会在问题实际发生一天后才收到回电——所以我看不出发生了什么。我的日志只显示 WordPress 无法部署到 MySQL 数据库。当我去查看时,我没有在 cPanel 中看到它,但在 MySQL.
的命令行中看到它所以,既然这些套路都有问题,我想知道Softaculous是如何解决这个问题的。因为即使我的脚本失败,Softaculous 仍然可以正常工作。
<?php
class cPanel {
public static function cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData, $nTimeoutSeconds = 0) {
$sURL = ($sCpanelPort == 2083) ? 'https://' : 'http://';
$sURL .= $sRootDomain . ':' . $sCpanelPort . '/xml-api/cpanel';
$asHeader[0] = 'Authorization: Basic ' . base64_encode($sCpanelUser . ':' . $sCpanelPass) . "\r\n" .
"Content-Type: application/x-www-form-urlencoded\r\n" .
"Content-Length: " . strlen($sPostData) . "\r\n" . "\r\n" . $sPostData;
$hCurl = curl_init ();
curl_setopt($hCurl, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1');
curl_setopt($hCurl, CURLOPT_REFERER, 'https://www.google.com/?gws_rd=ssl#q=' . $sURL);
curl_setopt($hCurl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($hCurl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($hCurl, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($hCurl, CURLOPT_URL, $sURL);
curl_setopt($hCurl, CURLOPT_HTTPHEADER, $asHeader);
curl_setopt($hCurl, CURLOPT_POST, 1);
if ($nTimeoutSeconds > 0) {
@ curl_setopt($hCurl, CURLOPT_TIMEOUT, $nTimeoutSeconds);
@ curl_setopt($hCurl, CURLOPT_CONNECTTIMEOUT, 30);
}
$s = curl_exec($hCurl);
return $s;
}
public static function cp_createDatabase($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBName) {
global $gsCpanel_Last_Result;
$sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adddb&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBName;
$nTimeoutSeconds = 60;
$s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData, $nTimeoutSeconds);
$gsCpanel_Last_Result = "$s";
if (strpos($s, '<result>1</result>') > 0) {
$sDBName = $sCpanelUser . '_' . $sUnPrefixedDBName;
return $sDBName;
} else {
return FALSE;
}
}
public static function cp_createDBUser($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBUser, $sDBPass) {
global $gsCpanel_Last_Result;
$sTest = $sCpanelUser . '_' . $sUnPrefixedDBUser;
if (strlen($sTest)>16) {
$sUnPrefixedDBUser = substr($sUnPrefixedDBUser, 0, (strlen($sUnPrefixedDBUser) - (strlen($sTest) - 16)));
}
$sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduser&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBUser . '&arg-1='. $sDBPass;
$s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData);
$gsCpanel_Last_Result = "$s";
if (strpos($s, '<result>1</result>') > 0) {
$sDBUser = $sCpanelUser . '_' . $sUnPrefixedDBUser;
return $sDBUser;
} else {
return FALSE;
}
}
public static function cp_addUserToDatabase($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sUnPrefixedDBName, $sUnPrefixedDBUser) {
global $gsCpanel_Last_Result;
$sPostData = 'user=' . $sCpanelUser . '&cpanel_xmlapi_module=Mysql&cpanel_xmlapi_func=adduserdb&cpanel_xmlapi_apiversion=1&arg-0=' . $sUnPrefixedDBName . '&arg-1=' . $sUnPrefixedDBUser . '&arg-2=all';
$s = self::cp_curlData($sRootDomain, $sCpanelUser, $sCpanelPass, $sCpanelPort, $sPostData);
$gsCpanel_Last_Result = "$s";
return (strpos($s, '<result>1</result>') > 0);
}
} // end class
Softaculous 有一个很棒的 SDK,可在此处免费获得:
http://www.softaculous.com/docs/File:Softaculous_Development_Kit.zip
那么,这段代码应该可以帮助您安装一个使用它的博客。它的速度非常快——比 cPanel API 快得多。
<?php
// SOURCE: https://gist.github.com/mickaelandrieu/7271939
require_once('sdk.php');
$new = new Softaculous_API();
$new->login = 'https://user:password@domain.com:2083/frontend/x3/softaculous/index.live.php';
// Domain Name
$data['softdomain'] = 'root-or-addon-domain.com'; // OPTIONAL - By Default the primary domain will be used
// The directory is relative to your domain and should not exist. e.g. To install at http://mydomain/dir/ just type dir. To install only in http://mydomain/ leave this empty.
$data['softdirectory'] = ''; // OPTIONAL - By default it will be installed in the /public_html folder
// Admin Username
$data['admin_username'] = 'admin';
// Admin Pass
$data['admin_pass'] = 'pass';
// Admin Email
$data['admin_email'] = 'admin@domain.com';
// Database
$data['softdb'] = 'wp887'; // make something up that MySQL likes and won't cause a collision
//Database User Name
$data['dbusername'] = 'wp887'; // ditto
// DB User Pass
$data['dbuserpass'] = 'wp887'; // ditto
// Language
$data['language'] = 'en';
// Site Name
$data['site_name'] = 'Wordpess wp887';
// Site Description
$data['site_desc'] = 'WordPress API Test';
// Response
$res = $new->install(26, $data); // Will install WordPress(26 is its script ID)
// Unserialize
$res = unserialize($res);
// Done/Error
if(!empty($res['done'])){
echo 'Installed';
}else{
echo 'Installation Failed<br/>';
if(!empty($res['error'])){
print_r($res['error']);
}
}