Housni
Housni

Reputation: 983

How do I create vhosts (Apache) on the fly while using Plesk?

I've built an app in PHP that runs on Apache. I need to be able to add vhosts on the fly so I would make my app write to sites-enabled and then reload Apache.

The only problem is, I have no choice but to use Plesk on this server as well. What would I edit on Plesk in order to be able to add vhosts on the fly without having Plesk overwrite it or cause any problems?

I've never had the chance to dig into Plesk too much so any guidance would be appreciated.

Upvotes: 0

Views: 904

Answers (2)

Bartek Kosa
Bartek Kosa

Reputation: 842

Here is Zend Framework module class for Plesk API witch can help you:

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 {

}

And here you have plesk API Reference: http://download1.parallels.com/Plesk/PP10/10.1.1/Doc/en-US/online/plesk-api-rpc/index.htm

Upvotes: 3

George
George

Reputation: 5086

This is slightly tricky to handle with Plesk as the control panel likes to be in control of the Apache configuration files and certain files are automatically generated by Plesk to ensure that system settings remain consistent.

There are a number of approaches you could take depending on your requirements.

1) Make your app setup new sites on the local machine using the Plesk API. This would give you full control of the sites with the control panel and everything that entails. It's also the most complicated solution.

2) If the sites can be addressed with the same domain, i.e. you are setting up subdomains a simple wildcard entry in the conf/vhost.conf for the master domain record would do it:

ServerAlias *.mydomain.com

You could then use this file to also associate other domains with the master domain by writing to it (carefully!) with your app.

3) Setup and configure the Apache module mod_vhost_alias - although Plesk is likely to cause you a headache with this approach.

Upvotes: 2

Related Questions