如何在使用Plesk时动态创建vhost(Apache)?

时间:2012-03-02 15:05:07

标签: php apache plesk

我在PHP中构建了一个在Apache上运行的应用程序。 我需要能够动态添加虚拟主机,这样我就可以将我的应用程序写入sites-enabled,然后重新加载Apache。

唯一的问题是,我别无选择,只能在这台服务器上使用Plesk。我会在Plesk上编辑什么才能在没有Plesk覆盖或导致任何问题的情况下即时添加虚拟主机?

我从来没有机会过多地挖掘Plesk,所以任何指导都会受到赞赏。

2 个答案:

答案 0 :(得分:3)

这是Plesk API的Zend Framework模块类,可以帮助您:

class Application_Model_PleskApi {

/**
 * Creates Web User
 *
 */
public function createWebUser($params) {
    $xmldoc = new DomDocument('1.0', 'UTF-8');
    $xmldoc->formatOutput = true;

    $packet = $xmldoc->createElement('packet');
    $packet->setAttribute('version', '1.6.3.0');
    $xmldoc->appendChild($packet);

    $webuser = $xmldoc->createElement('webuser');
    $packet->appendChild($webuser);

    $add = $xmldoc->createElement('add');
    $webuser->appendChild($add);

    $add->appendChild($xmldoc->createElement('site-id', $params['site-id']));
    $add->appendChild($xmldoc->createElement('login', $params['login']));
    $add->appendChild($xmldoc->createElement('password', $params['password']));
    $add->appendChild($xmldoc->createElement('ftp-quota', 100));

    return $xmldoc;
}

public function createSite($params) {
    $xmldoc = new DomDocument('1.0', 'UTF-8');
    $xmldoc->formatOutput = true;

    $packet = $xmldoc->createElement('packet');
    $packet->setAttribute('version', '1.6.3.0');
    $xmldoc->appendChild($packet);

    $site = $xmldoc->createElement('site');
    $packet->appendChild($site);

    $add = $xmldoc->createElement('add');
    $site->appendChild($add);

    $gen_setup = $xmldoc->createElement('gen_setup');
    $add->appendChild($gen_setup);
    $hosting = $xmldoc->createElement('hosting');
    $add->appendChild($hosting);

    $gen_setup->appendChild($xmldoc->createElement('name', $params['name']));
    $gen_setup->appendChild($xmldoc->createElement('webspace-id', $params['webspace-id']));

    $vrt_hst = $xmldoc->createElement('vrt_hst');
    $hosting->appendChild($vrt_hst);

    $property = $xmldoc->createElement('property');
    $vrt_hst->appendChild($property);
    $property->appendChild($xmldoc->createElement('name', 'php'));
    $property->appendChild($xmldoc->createElement('value', 'true'));

    return $xmldoc;
}

/**
 * Creates mail account
 *
 */
public function createMailAccount($params) {
    $xmldoc = new DomDocument('1.0', 'UTF-8');
    $xmldoc->formatOutput = true;

    $packet = $xmldoc->createElement('packet');
    $packet->setAttribute('version', '1.6.3.0');
    $xmldoc->appendChild($packet);

    $mail = $xmldoc->createElement('mail');
    $packet->appendChild($mail);

    $create = $xmldoc->createElement('create');
    $mail->appendChild($create);

    $filter = $xmldoc->createElement('filter');
    $create->appendChild($filter);

    $site_id = $xmldoc->createElement('site-id', $params['site-id']);
    $filter->appendChild($site_id);

    $mailname = $xmldoc->createElement('mailname');
    $filter->appendChild($mailname);

    $name = $xmldoc->createElement('name', $params['mailname']);
    $mailname->appendChild($name);

    $mailbox = $xmldoc->createElement('mailbox');
    $mailname->appendChild($mailbox);

    $enabled = $xmldoc->createElement('enabled', 'true');
    $mailbox->appendChild($enabled);

    $password = $xmldoc->createElement('password');
    $mailname->appendChild($password);

    $value = $xmldoc->createElement('value', $params['password']);
    $password->appendChild($value);

    $type = $xmldoc->createElement('type', $params['password-type']);
    $password->appendChild($type);

    return $xmldoc;
}

/**
 * Returns DOM object representing request for information about all available domains
 *
 * @return DOMDocument
 */
function domainsInfoRequest() {
    $xmldoc = new DomDocument('1.0', 'UTF-8');
    $xmldoc->formatOutput = true;

    // <packet>
    $packet = $xmldoc->createElement('packet');
    $packet->setAttribute('version', '1.4.1.2');
    $xmldoc->appendChild($packet);

    // <packet/domain>
    $domain = $xmldoc->createElement('domain');
    $packet->appendChild($domain);

    // <packet/domain/get>
    $get = $xmldoc->createElement('get');
    $domain->appendChild($get);

    // <packet/domain/get/filter>
    $filter = $xmldoc->createElement('filter');
    $get->appendChild($filter);

    // <packet/domain/get/dataset>
    $dataset = $xmldoc->createElement('dataset');
    $get->appendChild($dataset);

    // dataset elements
    $dataset->appendChild($xmldoc->createElement('limits'));
    $dataset->appendChild($xmldoc->createElement('prefs'));
    $dataset->appendChild($xmldoc->createElement('user'));
    $dataset->appendChild($xmldoc->createElement('hosting'));
    $dataset->appendChild($xmldoc->createElement('stat'));
    $dataset->appendChild($xmldoc->createElement('gen_info'));

    return $xmldoc;
}

/**
 * Prepares CURL to perform Plesk API request
 *
 * @param type $host
 * @param type $login
 * @param type $password
 * @return resource
 */
function curlInit($host, $login, $password) {
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "https://{$host}:8443/enterprise/control/agent.php");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_HTTPHEADER, array("HTTP_AUTH_LOGIN: {$login}", "HTTP_AUTH_PASSWD: {$password}", "HTTP_PRETTY_PRINT: TRUE", "Content-Type: text/xml"));

    return $curl;
}

/**
 * Performs a Plesk API request, returns raw API response text
 *
 * @param type $curl
 * @param type $packet
 * @return string
 * @throws ApiRequestException
 */
function sendRequest($curl, $packet) {
    curl_setopt($curl, CURLOPT_POSTFIELDS, $packet);
    $result = curl_exec($curl);
    if (curl_errno($curl)) {
        $errmsg = curl_error($curl);
        $errcode = curl_errno($curl);
        curl_close($curl);
        throw new ApiRequestException($errmsg, $errcode);
    }
    curl_close($curl);
    var_dump($result);
    return $result;
}

/**
 * Looks if API responded with correct data
 *
 * @param type $response_string
 * @return SimpleXMLElement
 * @throws ApiRequestException
 */
function parseResponse($response_string) {
    $xml = new SimpleXMLElement($response_string);
    if (!is_a($xml, 'SimpleXMLElement'))
        throw new ApiRequestException("Can not parse server response: {$response_string}");
    return $xml;
}

/**
 * Check data in API response
 *
 * @param SimpleXMLElement $response
 * @throws ApiRequestException
 */
function checkResponse(SimpleXMLElement $response) {
    $resultNode = $response->domain->get->result;
    // check if request was successful
    if ('error' == (string) $resultNode->status)
        throw new ApiRequestException("Plesk API returned error: " . (string) $resultNode->result->errtext);
}

}

class ApiRequestException extends Exception {

}

这里有plesk API参考: http://download1.parallels.com/Plesk/PP10/10.1.1/Doc/en-US/online/plesk-api-rpc/index.htm

答案 1 :(得分:2)

处理Plesk有点棘手,因为控制面板喜欢控制Apache配置文件,Plesk会自动生成某些文件,以确保系统设置保持一致。

根据您的要求,您可以采取多种方法。

1)使用Plesk API让您的应用在本地计算机上设置新站点。这将使您可以使用控制面板和所需的一切来完全控制站点。这也是最复杂的解决方案。

2)如果站点可以使用相同的域进行寻址,即您正在设置子域,则主域记录的conf / vhost.conf中的简单通配符条目将执行此操作:

ServerAlias * .mydomain.com

然后,您可以使用此文件将其他域与主域关联,方法是使用您的应用程序(小心!)写入。

3)设置并配置Apache模块mod_vhost_alias - 尽管Plesk可能会让您对此方法感到头疼。