How To Programmatically Create Addon Domains on Shared Hosting Plans
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.
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";
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.
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.
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:pass@domain.com: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.
精彩评论