2

I have a VPS (CentOS) with CPanel and Suphp enabled. Apache/2.0.63 (Unix) mod_ssl/2.0.63 OpenSSL/0.9.8e-fips-rhel5 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 mod_fcgid/2.3.5

As a root I created an account and set up a domain for it. Now I can login to this account's CPanel and create addon domains. For my project I want to allow the visitors of my website to do same thing (create addon domains) from my PHP script.

I guess it's impossible with PHP only, since it requires changing config files which PHP probably has no right to do and I don't know if it's possible to obtain the necessary privileges for the PHP process. I suspect I have to create a command file and use system() or similar PHP function to call it. But again I'm not sure how to give this command file enough rights. Maybe there can be some user switch commands and then the necessary stuff to change config files... Please advise.

Thank you.

ealexnet
  • 31
  • 2

3 Answers3

0

You need to add a DNS entry to point *.yourdomain.com to your application and your application should handle which page to show based on the value of $_SERVER['HTTP_HOST']. To build a system that lets end-users create accounts, I guess you would have to add a column to your database to hold the subdomain they chose, and then show the appropriate page.

I'm not sure if CPanel lets you add wildcard DNS entries. You'd have to contact your hosting provider for that.

Nithesh Chandra
  • 1,840
  • 13
  • 12
  • It's not exactly the case. I want users with registered domain names point their 'A' records to my VPS and then come to my site, enter domains and click some button, say 'Add my domain'. The script than modifies the Apache config file and does other necessary updates to the system to allow instant availability of these new addon domains. – ealexnet Oct 08 '10 at 12:42
0

OK, figured this out. With CPanel it's very easy actually: it provides the API for administrative tasks, which can be used in PHP scripts. http://etwiki.cpanel.net/twiki/bin/view/AllDocumentation/AutomationIntegration/XmlApi

But what if I don't have CPanel? Many VPS hosters either don't offer CPanel option or require additional monthly fee for it. What can be done to automate creating addon domains in this case?

Thank you.

ealexnet
  • 31
  • 2
  • This would be the subset of hosters whom do not provide cpanel but do provide suphp? That's a very small subset unless you move the functionality outside of php and invoke it with `exec('sudo...` – symcbean Oct 12 '21 at 23:34
0
$cpanelusername = "******";
$cpanelpassword = "*******";
$url='rootdomain.com';
$domain = 'newdomain.com';
$subdomain = $domain.'.rootdomain.com';
$directory = "/public_html/";  // A valid directory path, relative to the user's home directory. Or you can use "/$subdomain" depending on how you want to structure your directory tree for all the subdomains.
//https://hostname.example.com:2087/cpsess###########/json-api/cpanel?cpanel_jsonapi_user=user&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=AddonDomain&cpanel_jsonapi_func=addaddondomain&dir=addondomain%2Fhome%2Fdir&newdomain=addondomain.com&subdomain=subdomain.example.com
$query = "https://$url:2083/json-api/cpanel?cpanel_jsonapi_func=addaddondomain&cpanel_jsonapi_module=AddonDomain&cpanel_jsonapi_version=2&newdomain=$domain&subdomain=$subdomain&dir=$directory";

$curl = curl_init();                                // Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER,0);       // Allow self-signed certs
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST,0);       // Allow certs that do not match the hostname
curl_setopt($curl, CURLOPT_HEADER,0);               // Do not include header in output
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);       // Return contents of transfer on curl_exec
$header[0] = "Authorization: Basic " . base64_encode($cpanelusername.":".$cpanelpassword) . "\n\r";
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);    // set the username and password
curl_setopt($curl, CURLOPT_URL, $query);            // execute the query
$result = curl_exec($curl);
//var_dump($result);
if ($result == false) {
    error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");   
                                                    // log error if curl exec fails
}
curl_close($curl);

print $result;