swbrains

Well-Known Member
Sep 13, 2006
262
37
178
In the past on my old server (CentOS7; cPanel 68), my Perl script called the API function addsubdomain to create a new subdomain. On my new server (CentOS7, cPanel 70.0.44), the same code returns an error from the addsubdomain API call:

"You must choose a docroot that the system does not reserve"

I checked Tweak Settings->Restrict document roots to public_html and it is ON.

In my script I set up the call to the API using this URL:

Code:
$URI = "http://$g_accounthost:2082/json-api/cpanel?user=$g_accountuserid&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&cpanel_jsonapi_apiversion=2&domain=$newsubdomain&rootdomain=$g_accountdomain
This worked previously with no "dir" parameter being passed (i.e. default). Now it issues the above error message. In the cPanel API docs, it says for the "dir" parameter:
------------------------------------------------------------------------
The subdomain's document root within your home directory.

This parameter's default depends on the server's settings:
cPanel & WHM version 58 and higher:
- If the Restrict document roots to public_html value is On, the parameter defaults to the public_html/subdomain_namepath, where subdomain_name is the subdomain's name.
- If the Restrict document roots to public_html value is Off, the parameter defaults to the subdomain_name path, where subdomain_name is the subdomain's name.

------------------------------------------------------------------------
Since my setting is ON, the dir param should default to public_html/$newsubdomain in my case, which is what I want it to do, and that's where it created the new subdomains in the past on my old server without any problems.

On the new server, however, if I explicitly add
Code:
&dir=public_html/$newsubdomain
to the end of the the URL, it properly creates the subdomain as public_html/$newsubdomain as it used to.

Since the Restrict document roots to public_html setting is ON, I would expect that if I didn't specify &dir, it would have defaulted to the same value I passed when I added the &dir=public_html/$newsubdomain parameter explicitly.

Am I missing something or should my call work the old way (without &dir) if I want to create public_html/$newsubdomain?

Thanks!
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,298
1,279
313
Houston
Hi @swbrains

I'm running this using the Command Line and not encountering any issues. Here's what I'm running:

Code:
# cpapi2 --user=example SubDomain addsubdomain domain=test.example.com rootdomain=example.com
---
cpanelresult:
  apiversion: 2
  data:
    -
      reason: The subdomain “test.example.com” has been added.
      result: 1
  event:
    result: 1
  func: addsubdomain
  module: SubDomain
I have also ensured that restrict document roots to public_html is on in tweak settings as well. I've tested with the JSON version as well:

Code:
https://server.example.com:2087/cpsess2448913101/json-api/cpanel?cpanel_jsonapi_user=example&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=subdomain.example.com&rootdomain=example.com
With the result:
Code:
{"cpanelresult":{"data":[{"result":1,"reason":"The subdomain “subdomain.example.com” has been added."}],"apiversion":2,"module":"SubDomain","event":{"result":1},"func":"addsubdomain"}}
Thanks!
 

swbrains

Well-Known Member
Sep 13, 2006
262
37
178
Hi,

The only difference I can see between your params and mine is with the "domain" parameter value. The docs say:

domain string Required The subdomain name to create. example: subdomain

So in my code, I set &domain=test (for example), if I want to create test.example.com, whereas you have the full subdomain specified including the main domain its being created under.

I haven't yet tried setting domain to "test.example.com" (without &dir) in my code to see if it makes a difference so I can't say if that's the difference.

I've got rootdomain set as you do: example.com, which is the primary domain parked on the account. The account was created as a subdomain of my main business domain (for example, "custacct.domain.com"). Then I parked "example.com" on the customer account and tried to create the subdomain on "example.com". I doubt that all makes a difference, but I figured I'd mention it. :)

Thanks!
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,298
1,279
313
Houston
Hi @swbrains

I tried modifying it a bit to not include the full subdomain:

Code:
https://server.example.com:2087/cpsess2448913101/json-api/cpanel?cpanel_jsonapi_user=example&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=test2&rootdomain=example.com
The result:

Code:
{"cpanelresult":{"func":"addsubdomain","apiversion":2,"event":{"result":1},"module":"SubDomain","data":[{"reason":"The subdomain “test2.example.com” has been added.","result":1}]}}
Let me try it with a parked domain as well, I'll update this thread shortly.


Thanks!
 

cPanelLauren

Product Owner
Staff member
Nov 14, 2017
13,298
1,279
313
Houston
Same result for a parked domain as well:

CLI:
Code:
# cpapi2 --user=example SubDomain addsubdomain domain=test3 rootdomain=parkeddomain.com
---
cpanelresult:
  apiversion: 2
  data:
    -
      reason: The subdomain “test3.parkeddomain.com” has been added.
      result: 1
  event:
    result: 1
  func: addsubdomain
  module: SubDomain
JSON
Code:
https://server.lauren.tech:2083/cpsess2083998335/json-api/cpanel?cpanel_jsonapi_user=example&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=SubDomain&cpanel_jsonapi_func=addsubdomain&domain=test4&rootdomain=parkeddomain.com
Result:
Code:
{"cpanelresult":{"module":"SubDomain","func":"addsubdomain","data":[{"result":1,"reason":"The subdomain “test4.parkeddomain.com” has been added."}],"apiversion":2,"event":{"result":1}}}