PHP How To Install SSL on Subdomain via cPanel API

Reading Time: 4 minutes
1,319 Views

Inside this article we will see the concept i.e PHP How To Install SSL on subdomain via cPanel API. Article contains classified information about SSL installation on subdomain.

To install SSL either on domain or subdomain we have several ways. We can use cPanel UI to install SSL into sites. Or we can use cPanel APIs for it. In this tutorial we will how to consume cPanel SSL API to install SSL on wildcard subdomains.

Suppose we have a domain, Example: sampletuts.com. Already we have SSL installed on it. Now, we want to add or install SSL on these subdomains. Example: xyz.sampletuts.com, pqr.sampletuts.com, etc.

Wilcard Domains be like – *.sampletuts.com

Learn More –

Let’s get started.

Application Programming

In this article, we will create a single file called install-ssl.php. Inside this we will code to to install SSL on wildcard subdomains.

Before working on code you need these –

  • cPanel username
  • cPanel password
  • cPanel domain name
  • cPanel PORT
  • Either on all wildcard subdomain or select any one.
  • SSL Certificate Keys
  • cPanel XML API

Downlad cPanel XML API

Open install-ssl.php file and write this complete code into it.

<?php

include './xmlapi.php';

// cPanel credentials
$username = "CPANEL_USERNAME";
$password = "CPANEL_PASSWORD";
$cpanelDomain = "CPANEL_DOMAIN_URL"; // sampletuts.com
$domainPort = 2083; // DEFAULT
$sslForDomain = "YOUR SUBDOMAIN"; // xyz.sampletuts.com

// SSL Certificate Keys
$crt1 = "-----BEGIN CERTIFICATE-----
     ********
-----END CERTIFICATE-----";

$key = "-----BEGIN RSA PRIVATE KEY-----
     ********
-----END RSA PRIVATE KEY-----";

$crt2 = "-----BEGIN CERTIFICATE-----
     ********
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
     ********
-----END CERTIFICATE-----
-----BEGIN CERTIFICATE-----
     ********
-----END CERTIFICATE-----";

// cPanel APIs - Authentication
$xmlapi = new \xmlapi($cpanelDomain);
$xmlapi->password_auth($username, $password);
$xmlapi->set_port($domainPort);
$api2args = array();
$result = $xmlapi->api2_query($password, 'CustInfo', 'contactemails', $api2args);
$result = json_decode($result);

// echo "<pre>";
// print_r($result);
// die;

try {
    // Web server certificate SSL Upload
    
    $api2args = array(
        'domain' => $sslForDomain,
        'cert' => $crt1,
        'key' => $key,
        'cabundle' => $crt2,
    );
    $result = $xmlapi->uapi_query($username, 'SSL', 'install_ssl', $api2args);
    $result = json_decode($result);

    echo "<pre>";
    print_r($result);
} catch (ErrorException $ex) {

    echo "<pre>";
    print_r($ex->getMessage());
}

Application Testing

Now,

Open URL into browser.

URL: http://localhost/ssl/install-ssl.php

Download Complete Source Code –

We hope this article helped you to learn PHP How To Install SSL on Subdomain via cPanel API in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.