Skip to content

Instantly share code, notes, and snippets.

@Xavron
Last active July 24, 2019 19:27
Show Gist options
  • Save Xavron/8ced1441fc48a6937e5a711a20c55854 to your computer and use it in GitHub Desktop.
Save Xavron/8ced1441fc48a6937e5a711a20c55854 to your computer and use it in GitHub Desktop.
cert installation
#!/bin/sh
# Install cert files in cPanel directly using cPanel's CLI
# (compatible with namecheap shared hosting)
# This is provided as-is. You can do what you want with it.
# Set domain name cert is to be used with (CHANGE TO YOUR DOMAIN)
# Currently, only works for one domain
dom='example.com'
# Set the username of cPanel (CHANGE TO YOUR CPANEL USERNAME)
username='cPanelUsername'
##############
# Instructions
##############
# 1) Change the two items above and save
#
# 2) Setup python by adding the following two lines to .htaccess file in the letsacme folder (I added to the top for refernce)
# Options +ExecCGI
# AddHandler cgi-script .py
#
# 3) Upload this script to letsacme folder and chmod 600
#
# 4) cPanel > cron jobs > fill in:
#
# Select 1st,15th then change to 2nd,16th so that the script runs after renew cert (or do 1,15 but have some hour after, etc)
#
# Copy/paste to command field (don't forget to change to your cPanel username):
# /bin/sh /home/cPanelUsername/letsacme/certinstall.sh
# (add ">> /home/cPanelUsername/letsacme/certinstall.log 2>&1" without quotes to redirect to log file instead of email)
#
# And save the cron job
#
# That's it! Now all you have to do is go have some tea :-)
#
##############################
#### DO NOT MAKE CHANGES BELOW
##############################
# usage: uapi --user=username[ROOT-USE-ONLY] SSL install_ssl domain=example.com cert=THECERTIFICATETEXT key=THEKEYTEXT cabundle=THECABUNDLETEXT
# Read in the files that should exist - currently, if not exists, this should hopefully fail gracefully
ca=$(</home/$username/letsacme/chain.crt)
crt=$(</home/$username/letsacme/dom.crt)
key=$(</home/$username/letsacme/dom.key)
##############
# URI encode (crt, key, and ca: "must URI-encode this value")
##############
# Requires cPanel python to be allowed - see .htaccess note in instructions above
# https://unix.stackexchange.com/questions/159253/decoding-url-encoding-percent-encoding
ca=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])" "$ca")
crt=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])" "$crt")
key=$(python -c "import urllib, sys; print urllib.quote(sys.argv[1])" "$key")
# Requires perl module to be installed
# https://www.namecheap.com/support/knowledgebase/article.aspx/9693/29/how-to-install-perl-modules-on-shared-servers
# ca=$(perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$ca")
# Requires URL extension which namecheap doesn't provide (another one bites the dust)
# ca=$(php -r "echo rawurlencode('$ca');")
##############
# Install cert
##############
uapi\
SSL\
install_ssl\
domain="$dom"\
cert="$crt"\
key="$key"\
cabundle="$ca"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment