Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 540
Volomike Avatar asked Jul 10 '10 15:07

Volomike


1 Answers

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";
like image 106
Volomike Avatar answered Sep 29 '22 08:09

Volomike