loader image
View Categories

cPanel API: Creating an Addon Domain

In web hosting, managing multiple domains efficiently is crucial. One common task is creating addon domains – additional domains hosted under a primary domain. In this blog post, we’ll guide you through a PHP script designed to automate this process using the cPanel API, and we’ll also walk you through the steps to create an API token from cPanel for authentication.

Why Use the cPanel API? #

cPanel simplifies domain and server management through its control panel, and its API provides a programmatic way to interact with cPanel’s features. This enables automation of tasks like creating addon domains, which saves time and reduces manual errors.

Creating an API Token from cPanel #

Before using the API, you need to generate an API token in cPanel for secure authentication. Here’s how you can create one:

  1. Log in to cPanel
  2. Navigate to API Tokens:
    • Look for the “Security” section in cPanel.
    • Click on “Manage API Tokens.”
  3. Create a New API Token:
    • Click the “Create” button.
    • Enter a name for the token (e.g., “Addon Domain Script”).
  4. Copy the API Token:
    • After creating the token, copy it. You will need this token for authentication in your PHP script.

PHP Script to Create an Addon Domain #

Now that you have your API token, you can use it in the PHP script to create an addon domain. Here’s a step-by-step breakdown of the script:

<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);

function createAddonDomain($newDomain, $directory) {
    $cpanelUser = 'your_cpanel_username';
    $cpanelApiToken = 'your_cpanel_api_token';
    $cpanelHost = 'your_cpanel_host';

    // The directory should be relative to the root domain's public_html folder
    $query = "https://$cpanelHost:2083/json-api/cpanel?cpanel_jsonapi_user=$cpanelUser&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_module=AddonDomain&cpanel_jsonapi_func=addaddondomain&newdomain=$newDomain&dir=public_html/$directory&subdomain=$newDomain";

    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_SSL_VERIFYPEER => false,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER => array(
            'Authorization: cpanel ' . $cpanelUser . ':' . $cpanelApiToken
        ),
        CURLOPT_URL => $query
    ));

    $result = curl_exec($curl);
    $error = curl_error($curl);
    $errno = curl_errno($curl);

    curl_close($curl);

    if ($error) {
        return array('error' => "cURL Error ($errno): $error");
    }

    return json_decode($result, true);
}

// Define the new domain and directory
$newDomain = 'newdomain.com';  // The domain you want to add
$directory = 'newdomain';      // Directory where the site files will be stored

$response = createAddonDomain($newDomain, $directory);

if (isset($response['error'])) {
    echo "Error creating domain: " . $response['error'];
} else {
    echo "Domain created successfully!";
}
?>

You cannot copy content of this page