getssl icon indicating copy to clipboard operation
getssl copied to clipboard

Perl script to automate installing SSL in cPanel

Open JakeQZ opened this issue 1 year ago • 1 comments

Is your feature request related to a problem? Please describe. With cPanel hosting, the SSL does not actually get installed. An additional step is required, which can be automated.

Describe the solution you'd like I'm providing a Perl script which will do this via the RELOAD_CMD setting in the getssl.cfg. I'd like to share this, and hope you would consider including it in the documentation. It works with GoDaddy shared cPanel hosting (GoDaddy do not enable the Let's Encrypt option in WHM/cPanel, probably because they want to sell SSL certificates).

Describe alternatives you've considered Manually installing the generated certificates in cPanel every couple of months is not practical if you're hosting several websites.

Additional context The script is below. It works with cPanel hosting only (and requires uapi command line access to be enabled). It has two command line arguments. The first is the domain name (without www., e.g. example.org). The second is the account username, which is needed to locate the home directory (I tried using ~ and found it didn't seem to work). (It is adapted from something I found when using a PHP implementation of ACME - which has been discontinued - which did not have the uapi call, so is probably beyond recognition of that.)

It can be invoked via the RELOAD_CMD setting in a site-specific getssl.cfg file:

RELOAD_CMD="/path/to/script.pl example.org account-username"

where /path/to/script.pl has the following content:

#!/usr/local/cpanel/3rdparty/bin/perl

use strict;
use URI::Escape;

my $dom = $ARGV[0];
my $user = $ARGV[1];

my $certdir = "/home/$user/.getssl/$dom";

my $certfile = "$certdir/$dom.crt";
my $keyfile = "$certdir/$dom.key";
my $cafile =  "$certdir/chain.crt";

my $certdata;
my $keydata;
my $cadata;

open(my $certfh, '<', $certfile) or die "cannot open file $certfile";
{
  local $/;
  $certdata = <$certfh>;
}
close($certfh);

open(my $keyfh, '<', $keyfile) or die "cannot open file $keyfile";
{
  local $/;
  $keydata = <$keyfh>;
}
close($keyfh);

open(my $cafh, '<', $cafile) or die "cannot open file $cafile";
{
  local $/;
  $cadata = <$cafh>;
}
close($cafh);

my $cert = uri_escape($certdata);
my $key = uri_escape($keydata);
my $ca = uri_escape($cadata);

my $result = `uapi SSL install_ssl domain=$dom cert=$cert key=$key cabundle=$ca`;
print $result;

Note: I've added the username command line argument for reusability. In my case it is hardcoded. It's possible I've made a mistake in doing so, but if so, should be easy to fix. It is working for me on two separate GoDaddy shared hosting accounts,

JakeQZ avatar Nov 26 '22 00:11 JakeQZ