views:

65

answers:

4

On Linux-based shared hosting that is administered with cpanel, is there a programmatic way to create, sort of reliably, addon domains for my site if I know my cpanel login information and/or FTP information?

Note: interested in PHP and cpanel in this case.

+1  A: 

It depends on whether your DNS server is being hosted somewhere you control, and whether there's an API to modify the DNS entries (or just access to the DNS server's configurations themselves). I'm not a cPanel user, but I'm going to lean toward "no."

In an environment where you control the DNS serving system, it's fairly trivial to write code to add more entries to your DNS records (although obviously propagation still takes time) and throw new sites into sites-enabled. It can also be very difficult to do securely and safely, but there you go. But I doubt you have that ability through cPanel.

Something that just came to mind is the ability to use catchall domains (having a default site entry in your Apache configuration files that all unspecified subdomains might point to) and programmatically determining what site should run from there, but there are so many negatives to this approach that I would not recommend even looking at it.

Ed Ropple
+1 this is pretty exactly what I would say too. Unless cPanel offers an API, there's not much you can do.
Pekka
@Ed Roople: I don't understand what this answer has to do with the question, accroding to the tags, he is asking how to add domains via cPanel on shared-hosting and not how to add domains directly on DNS and Apache with server root access.
Marco Demajo
I think you should read the first comment; it answers it pretty clearly. Unless cPanel offers an API, there's very little that can be done. Hence, "but I doubt you have that ability through cPanel."
Ed Ropple
+1  A: 

I used to do this with fopen - I can't remember the exact URLS and I don't have access to cPanel anymore, but if you check the urls in use, you should be able to do something like

fopen("https://username:[email protected]:1084/cpanel/doaddparked.html?domain=domain.com", "r");

(you will have to check paths and ports, I can't remember these) - and I last did this on a project two years ago where our signup created emails via cpanel - they may have changed it since then, but I found that the easiest way. At the time, they definitely worked on GET data rather than POST, if it's changed to POST you might need to look into CURL.

Alex C
This may work, but if you choose to go this route you need to be *extremely* careful that it's OK by your shared host provider. Honestly if you're doing something complex enough to need this, you shouldn't be on shared hosting.
Ed Ropple
+3  A: 

You can use cPanel API, doc is here. It's not that easy to read, if you want to play with them with PHP you could use free PHP classes (that uses these API behind the scenes), some are here on cPanel forum, some other here.

Marco Demajo
+1  A: 

Thanks to @Alex C who got me on the right track. The following will work with many shared hosting plans, but you'll want to check with their policies on this first.

In the example below, I would have already purchased root.com as my main root domain of my shared hosting plan. Then, I would have desired to add on a domain called addon.com. I set the username to addon_user and the pass to addon_pass. As well, I placed the files for the new domain in public_html/addon.com. To connect to Cpanel to make this happen, I entered a cpanel homepage URL (which varies with hosting plan) so that it could be parsed and reused. Also, I provided my root.com's cpanel user/pass information as root_user and root_pass.

The last echo statement is just the output response whether it worked or not. However, if you want to parse it for failure, you probably can parse for the phrase "not added".

Note some hosting plans block file_get_contents connecting to a URL, so you may have to switch with fopen($sURL, 'r') or Curl API.

<?php

// @ input vars - change these as you see fit
$sPastedCpanelHomepageURL = 'https://root.com:2083/frontend/x3/index.html';
$sNewDomain = 'addon.com';
$sNewDomainUser = 'addon_user';
$sNewDomainPass = 'addon_pass';
$sNewDomainFolder = 'public_html/addon.com';
$sCPanelUser = 'root_user';
$sCPanelPass = 'root_pass';

// @ processing
$sCP = dirname($sPastedCpanelHomepageURL);
$sCP = str_replace('://','://' . $sCPanelUser . ':' . $sCPanelPass . '@',$sCP);

$sTask = '/addon/doadddomain.html?';

$sNewDomain = urlencode($sNewDomain);
$sNewDomainUser = urlencode($sNewDomainUser);
$sNewDomainPass = urlencode($sNewDomainPass);
$sNewDomainFolder = urlencode($sNewDomainFolder);
$sCPanelUser = urlencode($sCPanelUser);
$sCPanelPass = urlencode($sCPanelPass);

$asData = array(
  'domain' => $sNewDomain,
  'user' => $sNewDomainUser,
  'dir' => $sNewDomainFolder,
  'pass' => $sNewDomainPass,
  'pass2' => $sNewDomainPass
);
$sData = http_build_query($asData);

$s = file_get_contents($sCP . $sTask . $sData);

echo "$s\n";
Volomike
So the port is 2083 and not 1084 as Alex C suggested, just to know cause I'm also interested in this.
Marco Demajo
Actually, the port is sometimes changed by web hosting providers, as is the homepage URL to the cpanel.
Volomike