Steps to Create Subdomain in cPanel using PHP

How To Create Subdomains in cPanel using PHP? We can easily do so by using the steps in this latest blog. Bobcares, as a part of our cPanel & WHM Support Services offers solutions to every query that comes our way.

How To Create Subdomains in cPanel using PHP?

Subdomains in cPanel are additional, separate parts of a website that operate under a main domain. They are often used to organize content into distinct sections, host different services, or create development environments.

For one or two subdomains, we can create manually in cPanel by entering the subdomain name and the primary domain. However, it becomes unworkable when several subdomains must be created based on user activities. Consider a website that requires users to register before they may use its services. After registering, a new subdomain is created. Manually creating each subdomain in this case is both unpleasant and impractical.

How to fix this?

In such a case, we can use PHP code to create subdomains dynamically. To do so, we must include a code snippet to the current codebase. Before applying the PHP code, make sure port 2082 is open for the cPanel setup and find the cPanel skin by going to cPanel > Preferences > Change Style.

The PHP Code for cPanel Subdomain Creation

Steps to Create Subdomain in cPanel using PHP

To handle file operations within this new subdomain, we must first need to find the cPanel directory path. Then, create an index.php file in the subdomain with the following code:

create subdomain in cpanel using php

Run this code to obtain the directory path. We can then use PHP to dynamically manage files in the subdomain using the below code:

create subdomain in cpanel using php

[Need to know more? Get in touch with us if you have any further inquiries.]

Conclusion

To sum up, our Tech team went over the details for creating a subdomain in cPanel using PHP code.