How do we create a sub-domain in cPanel from WordPress

c3PanoH

Member
Apr 16, 2020
10
1
3
UK
cPanel Access Level
Root Administrator
Hello,
As the tile suggests i'm looking to provision sites on my WordPress Multisite network. I have the WP side of things sorted OK so i can create new sites on demand but would like to create the required subdomain in CPanel for a new site created by by users (always on my main domain). I have 40+ subsites already setup manually and love the simplicity of the process; zero propagation time, just create a new subdomain and its accessible instantly. I've read the similar threads but many seem out of date referencing old cpanel whm APIs etc. There's also info advising that API tokens would be needed. I'm swamped! and no more clued up after jumping from link to link as to where to start.

I use SiteGround Hosting and have Admin CPanel Access - id prefer a pure PHP solution as its my primary language but JSON/jQuery are par for the course so i dont mind.

Cheers
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
82
36
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
Hi @c3PanoH

If you want to add subdomains via the cPanel API, you'll want to use Subdomain::addsubdomain.

You can use the LiveAPI PHP system to call the API using PHP. LiveAPI does not require API Token authentication when run locally on the server.

The API call would look something like this:

PHP:
$cpanel = new CPANEL(); // Connect to cPanel - only do this once.

// Create a subdomain.
$get_userdata = $cpanel->uapi(
    'SubDomain', 'addsubdomain',
        array(
        'domain'                => 'subdomain',
        'rootdomain'            => 'example.com',
        'dir'                   => '/public_html/directory_name',
        'disallowdot'           => '1',
    )
);
 
  • Like
Reactions: cPanelLauren

c3PanoH

Member
Apr 16, 2020
10
1
3
UK
cPanel Access Level
Root Administrator
Thanks for the quick reply @cPanelTJ -
ahha! That's the page i should've landed on; the one i found must have been the deprecated API linking to this page but i went astray somewhere!
Ill give this a shot ...

much appreciated,
Cheers
 
  • Like
Reactions: cPanelLauren

c3PanoH

Member
Apr 16, 2020
10
1
3
UK
cPanel Access Level
Root Administrator
Hi again,
Ive just tried the UAPI and hit a problem - im getting an error which suggests CPanel API is not configured by my host and after contacting them they say they cannot offer configuration for this feature. The error appears when i use the basic browser-based call to the Subdomain module - thus :

Code:
The system failed to load the module “Cpanel::API::SubDomain” because of an error: Can't locate Cpanel/API/SubDomain.pm in @INC (you may need to install the Cpanel::API::SubDomain module) ... etc ...
The prototype of the url method used is:
Code:
https://hostname.example.com:2083/cpsess##########/execute/SubDomain/addsubdomain?domain=subdomain&rootdomain=example.com&dir=%2Fpublic_html%2Fdirectory_name&disallowdot=1
This being a test which i thought would give me an idea of my servers capability - so i was surprised that this simple method doesn't work before i try the PHP API method. Is there anything i can do here? I have admin and SSH access to my server @ SiteGround.
 

cPanelTJ

Product Owner
Staff member
Jan 29, 2019
82
36
93
Houston, TX
cPanel Access Level
Root Administrator
Twitter
@c3PanoH

Your server may be on an earlier product version; these UAPI subdomain calls were added recently in cPanel&WHM version 11.86.

My favorite way to test the functionality and output of one of our APIs is over the Command Line. You can do it as root for WHM API, as root impersonating a user for any cPanel API, and as any user for any cPanel API. Each API documentation page includes an example of calling the API over Command Line.

Please let me know the cPanel&WHM version of your server, and we may be able to identify some alternatives for you!
 

c3PanoH

Member
Apr 16, 2020
10
1
3
UK
cPanel Access Level
Root Administrator
I think i may have overlooked a point here ( I've slapped the back of my own legs and sent myself to bed with no pudding ). I dont see WHM as an option, not only in my CPanel but indeed any host package offered by my host.. I contact them over the weekend and they did confirm my suspicions. Anything described as 'reseller' is actually their own custom interface. They dont provide root access to any package whether it be the shared, dedicated or cloud; top of the range, hosting. They've been great up until now and i've no issue with them at all; i think my projects' needs have just outgrown their services. I drew up a short list a few months ago before settling with them - ill now change to my 'plan-B' host.