Create subdomain and database dynamically

Hi, does anybody now what is the best way to create a new subdomain and a new database and then populating them with files and sql dump when a new user register to a website? I read online it could be done with cPanel API and php but all the articles I’ve found so fare are quite dated.

Many thanks

Disclaimer
I have never used a cPanel API
I have only used cPanel from the UI

My first concern would be that creating a mess of sub-domains and databases wouldn’t upset the host provider.
My second concern would be cost.

But if those are not a concern for you, maybe the starting point would be to decide which API would best suit your needs?

https://documentation.cpanel.net/display/SDK/Guide+to+cPanel+API+2#GuidetocPanelAPI2-APIMethod

cPanel, Inc. produces four current and two deprecated APIs, and they all include separate sets of functions. Make certain that the function and module that you call exist in the API version that your code uses.

I would like to add ‘trial and error’ to Mittineague’s remarks.

Be prepared to make lots of mistakes and loose data occasionally so backups are a must. I’ve been coding a long time and managed to mess up my localhost about 8 weeks ago. It took me about an an hour to put it back together.

Every coding mistake is a learning experience. The bigger the mistake the easier it is to remember what you learned.

I ALWAYS look forward to days that I get all the learning I can handle.

Hi @Mittineague thanks for your reply, I’m thinking to get a VPS and not a normal shared host, I’m not going to create infinite subdomain because it will be a payment subscription so the application will be installed into a new subdomain only if the user has paid for a yearly subscription.
Thanks for providing the link to the API, I’ve found this article http://www.learnsourcecode.com/blogs/create-mysql-database-using-cpanel-api-in-php that explains how to create a database but nothing for new subdomains.
Do you think this approach will work in a VPS I tried to test it on shared host and it doesn’t work maybe because there are some limitation made by the host provider.

Hi @nichemtktg this is definitely a good point, thanks for your suggestions.

I don’t recall how one does it exactly, but IIRC, you have to set Apache to allow random subdomains. Then you can generate your subdomains with PHP.

Not that it isn’t possible, but the only way I know of requires having access to the httpd.conf file to create VirtualHost configurations

https://httpd.apache.org/docs/2.4/vhosts/examples.html

2 Likes

Found one that gives an example of what you were talking about.

1 Like

Hi @spaceshiptrooper thanks for your help. Now I need to work out how to create the php application to check subdomain availability, create the subdomain and move files into the subdomain folder. Also how can create a new database?
Many thanks

I have never done this before so I can’t say that I’m an expert at it. I have just read up on a few of these. So it’s out of my scope to say how one would create new databases based on the subdomains.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.