Softaculous如何在cPanel中创建MySQL数据库,用户和用户> db映射?

时间:2015-12-10 21:40:22

标签: php mysql wordpress cpanel softaculous

目前,使用PHP代码,我在cPanel中创建MySQL数据库,然后是MySQL用户,然后使用这4个PHP例程将它们映射到数据库。麻烦的是,每当我们认为它与服务器负载有关时,每当在一个蓝色的月亮中,这个脚本说它创建了MySQL,用户和用户 - > 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

1 个答案:

答案 0 :(得分:3)

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']);
        }
    }

消息来源:https://gist.github.com/mickaelandrieu/7271939