PHPMatters Help You Better Hosting Your PHP-based Sites
How to Create Add-on Domain with PHP Script and cPanel

How to Create Add-on Domain with PHP Script and cPanel

For people who are wishing to set up separate websites in addition to the main site on one hosting account, the existence of add-on domain is really helpful. It is a fully functional domain that can be hosted in a new folder of the account within the public_html directory, storing contents and files of different sites separately. Generally, you are allowed to create multiple add-on domains as you want, and give them exclusive e-mail addresses and forwarders the same way as the primary domain. Here, we have compiled an easy-to-understand tutorial telling you how to create an add-on domain with the help of cPanel and PHP scripts.

Create an Add-on Domain from cPanel

In fact, the add-on domain can be created from any kinds of control panels. Here, we simply assume you make use of the most widely-used and user-friendly cPanel control panel.

First of all, you need to log into your cPanel. On the intuitive user interface, you can find a Stats tab on the left hand of the whole screen, with which you can check out how many add-on domains you can have in total, and how many add-on domains you have currently created. To be honest, this is a very important preparing step. Although the majority of rich-featured hosting companies nowadays allow webmasters to have an unlimited number of add-on domains like BlueHost and JustHost, there are still some companies set a restriction in this aspect.

domain stats

Next, you need to scroll down to look for the Domains tab, and click the Add Domains link.

add domain

Then, you can be directed to a page that requires you to fill out the details of your new add-on domain, including the domain name, sub-domain/FTP username, document root, and password. Note that after adding a new domain on your account, cPanel will create an additional FTP user for it automatically, so you can enter a FTP username that is same as your add-on domain name. In terms of the document root, it is the directory in your main account where the data and files of the new domain will be pointing. Generally, the directory name is publick_html/newdomainname.

After finishing the required information, click the Add Domain button. That’s it! The new add-on domain has been created, and you can remove it and manage redirection within the cPanel easily.

create new domain

Add an Add-on Domain with PHP Configuration

This method is much more complicated than the first one, and requires webmasters to have the advanced coding knowledge. Therefore, we highly suggest beginners adopting the method one. If you are an experienced developer, you can also consider this way.

To create an add-on domain from the PHP script, you firstly need to download the XML-API of cPanel for PHP as you will be using the API2 function. In the following, we’d like to show you the coding information included within your api2_example_withargs.php file along with the above download.

api2 coding

configurationNote that the IP is the IP address of your server, and the root_pass stores your password that allows you to log into the XML API.

Generally, people use the API as a root user like the example presented above, but you can also login as a cPanel user by changing “root”,$root_pass to $account,$root_pass.

To create an add-on domain by using API2 function, you have to realize the existence of 4 parameters. The first one is expected to be the cPanel account allowing you to perform this action; the second one is the module; the third one indicates the function to call; the fourth one refers to an array of input arguments for this function.

These changes are used to create an add-on domain on your account, so the final step is to save these modifications with API2 function calls. Therefore, simply go to the cPanel’s XML-API for PHP and find the file of api2_example_addondomain.php. Then, modify this file with the changes presented above.

Lastly, there is a special tip for Access Denied Error. If you encounter this problem, simply add the set_port attribute before the api2_query, and replace the X with the port number of your cPanel control panel.