{"id":224,"date":"2016-05-28T14:29:04","date_gmt":"2016-05-28T14:29:04","guid":{"rendered":"http:\/\/www.nerdlingen.de\/?p=224"},"modified":"2018-10-12T09:45:23","modified_gmt":"2018-10-12T08:45:23","slug":"lets-encrypt-plesk","status":"publish","type":"post","link":"https:\/\/www.nerdlingen.de\/?p=224","title":{"rendered":"Let&#8217;s Encrypt + Plesk"},"content":{"rendered":"<p><a href=\"http:\/\/wpguru.co.uk\/2014\/12\/plesk-mail-ssl\/\">Let&#8217;s encrypt<\/a> issues SSL-Certificates for free &#8211; meanwhile most Systems trust their CA. Let&#8217;s Encrypt features a CLI to request, update and install certificates &#8211; which work&#8217;s nicely as long as your server&#8217;s setup is compatible. A Plesk based setup, however, is not.<\/p>\n<p>Luckily, <a href=\"http:\/\/download1.parallels.com\/Plesk\/PP11\/11.0\/Doc\/fr-FR\/online\/plesk-unix-cli\/index.htm?fileName=39009.htm\">Plesk features it&#8217;s own CLI<\/a> &#8211; so let&#8217;s make a short script to renew and update certificates. Here&#8217;s the script, I&#8217;ll explain later. It requires Let&#8217;s Encrypt&#8217;s &#8220;certbot&#8221; to be installed in $HOME.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">#!\/bin\/sh\r\n\r\nIP=&quot;your.ip.v4.address&quot;\r\nIP6=&quot;your:ip:v6:address&quot;\r\n\r\n#Domains, format: domain.tld sub1.domain.tld sub2.domain.tld&quot;\r\nDomains=(&quot;domain.tld sub1.domain.tld sub2.domain.tld&quot; &quot;domain2.tld2&quot;)\r\n#&quot;Main&quot; domain - the name of the certificate that will be associated to the IPs above...\r\nMainDomain=&quot;domain.tld&quot;\r\n\r\necho &quot;Certificates will be renewed and installed in Plesk:&quot;\r\nfor dom in &quot;${Domains[@]}&quot;; do\r\n  echo &quot; $dom&quot;\r\ndone\r\nif [ -n &quot;$1&quot; ]; then\r\n  echo &quot;Renew will be issued for all, however only domain $1 will be updated in Plesk!&quot;\r\nfi\r\necho;\r\necho &quot; IPv4: $IP&quot;\r\necho &quot; IPv6: $IP6&quot;\r\n\r\n#&quot;MonthName&quot; should be something that is unique between subsequent calles of the script\r\n# date +%B should give the name of the current month, for more frequent calls use e.g.\r\n# date +%Y-%M-%d_%H-%m-%S\r\nMonthName=&quot;$(date +%Y-%m-%d_%H-%M-%S)&quot;\r\necho &quot; Unique part for each certificate&#039;s name: $MonthName&quot;\r\n\r\necho;\r\n\r\nread -p &quot;This might brake your setup. Type YES to continue: &quot; Keypress\r\n\r\nif [ &quot;$Keypress&quot; != &quot;YES&quot; ]; then\r\n  echo &quot;I asked for \\&quot;YES\\&quot;, you gave me \\&quot;$Keypress\\&quot;. Exiting...&quot; \r\n  exit 1\r\nfi\r\n\r\necho;\r\n\r\nread -p &quot;Call letsencrypt-auto ? [y\/N] &quot; Keypress\r\n\r\nif [ &quot;$Keypress&quot; = &quot;y&quot; ]; then\r\n  ~\/certbot\/letsencrypt-auto renew\r\n  echo;\r\n  echo &quot;Done.&quot;\r\nfi\r\necho;\r\n\r\nfunction installcert {\r\n  Certname=&quot;LetsEncrypt-Auto_($MonthName)_$1&quot;\r\n  echo;\r\n  echo &quot;Installing certifikate \\&quot;$Certname\\&quot; to domain repository...&quot;\r\n  key_file=&quot;\/etc\/letsencrypt\/live\/$1\/privkey.pem&quot;\r\n  cert_file=&quot;\/etc\/letsencrypt\/live\/$1\/cert.pem&quot;\r\n  cacert_file=&quot;\/etc\/letsencrypt\/live\/$1\/fullchain.pem&quot;\r\n  \/opt\/psa\/bin\/certificate -c $Certname -domain $1 -key-file $key_file -cert-file $cert_file -cacert-file $cacert_file\r\n}\r\n\r\nfunction installcert_admin {\r\n        Certname=&quot;LetsEncrypt-Auto_($MonthName)_admin_$1&quot;\r\n        echo;\r\n        echo &quot;Installing certifikate \\&quot;$Certname\\&quot; to admin&#039;s repository...&quot;\r\n        key_file=&quot;\/etc\/letsencrypt\/live\/$1\/privkey.pem&quot;\r\n        cert_file=&quot;\/etc\/letsencrypt\/live\/$1\/cert.pem&quot;\r\n        cacert_file=&quot;\/etc\/letsencrypt\/live\/$1\/fullchain.pem&quot;\r\n        \/opt\/psa\/bin\/certificate -c $Certname -admin -key-file $key_file -cert-file $cert_file -cacert-file $cacert_file\r\n}\r\n\r\n\r\nfunction assigncert {\r\n  Certname=&quot;LetsEncrypt-Auto_($MonthName)_$1&quot;\r\n  \/opt\/psa\/bin\/subscription -u $2 -certificate-name $Certname\r\n}\r\n\r\nread -p &quot;Install certificates in Plesk? [y\/N] &quot; Keypress\r\n\r\nif [ &quot;$Keypress&quot; = &quot;y&quot; ]; then\r\n  for domain in &quot;${Domains[@]}&quot;; do\r\n    #Split into another array\r\n    subs=($domain)\r\n    main=&quot;${subs[0]}&quot;\r\n    echo &quot;Working on domain $main...&quot;\r\n    #Domain $domain will only been updated if either no arguments are given, or the one and only argument matches $domain\r\n    if ( [ &quot;$1&quot; == &quot;$main&quot; ] ) || ( [ -z &quot;$1&quot; ] ); then\r\n      installcert &quot;$main&quot;\r\n      #Then assign all certificates\r\n      for sub in &quot;${subs[@]}&quot;; do\r\n        assigncert &quot;$main&quot; &quot;$sub&quot;\r\n      done\r\n    else\r\n      echo &quot;==&amp;gt; Skipped.&quot;\r\n    fi\r\n  done\r\n\r\n  echo;\r\n  echo &quot;Done.&quot;\r\n  echo;\r\nfi\r\n\r\nread -p &quot;Install &amp;amp; Assign SSL-Certificate of $MainDomain for IPs (Admin&#039;s repository)? [y\/N] &quot; Keypress\r\n\r\nif [ &quot;$Keypress&quot; = &quot;y&quot; ]; then\r\n  installcert_admin &quot;$MainDomain&quot;\r\n  \/opt\/psa\/bin\/certificate -ac &quot;LetsEncrypt-Auto_($MonthName)_admin_$MainDomain&quot; -admin -ip $IP\r\n  \/opt\/psa\/bin\/certificate -ac &quot;LetsEncrypt-Auto_($MonthName)_admin_$MainDomain&quot; -admin -ip $IP6 \r\nfi\r\n\r\nfunction mailcert {\r\n  Uhr=&quot;$(date +%Y-%m-%d_%H-%M-%S)&quot;\r\n  key_file=&quot;\/etc\/letsencrypt\/live\/$1\/privkey.pem&quot;\r\n        cert_file=&quot;\/etc\/letsencrypt\/live\/$1\/cert.pem&quot;\r\n        cacert_file=&quot;\/etc\/letsencrypt\/live\/$1\/fullchain.pem&quot;\r\n        tar -chjf Cert-Backup-$Uhr.tar.bz2 \/etc\/postfix\/postfix_default.pem \/usr\/share\/imapd.pem \/usr\/share\/pop3d.pem\r\n        #Concat certificate for eMail...\r\n  cat $key_file &amp;gt;\/tmp\/newcert.pem\r\n  cat $cert_file &amp;gt;&amp;gt;\/tmp\/newcert.pem\r\n  cat $cacert_file &amp;gt;&amp;gt;\/tmp\/newcert.pem\r\n  cp \/tmp\/newcert.pem \/etc\/postfix\/postfix_default.pem\r\n  cp \/tmp\/newcert.pem \/usr\/share\/imapd.pem\r\n  cp \/tmp\/newcert.pem \/usr\/share\/pop3d.pem\r\n  chmod 400 \/usr\/share\/imapd.pem\r\n  chmod 400 \/usr\/share\/pop3d.pem\r\n  chmod 600 \/etc\/postfix\/postfix_default.pem\r\n  rm \/tmp\/newcert.pem\r\n  \/usr\/local\/psa\/admin\/sbin\/mailmng --restart-service\r\n}\r\n\r\nread -p &quot;Install certificate of $MainDomain to Postfix\/IMAP\/POP ? [y\/N] &quot;\r\n\r\nif [ &quot;$Keypress&quot; = &quot;y&quot; ]; then\r\n  mailcert &quot;$MainDomain&quot;\r\nfi<\/pre>\n<p>The script has a short configuration section at the top, most notably the domains you want to work on. They are give in a string array, containing the domain name given first when registering a certificate with LetsEncrypt as the first token, and all sub domains you want to secure with the same certificate separated with spaces. More precisely: Let&#8217;s encrypt stores your certificates into \/etc\/letsencrypt\/live\/&lt;domain&gt;, the first token is used to generate this path. I recommend using your domain without any subdomain for this purpose, i.e. &#8220;domain.tld sub.domain.tld sub2.domain.tld&#8221;. To achieve this, when calling lestencrypt-auto, give this domain as the first one, i.e.<\/p>\n<p><code class=\"EnlighterJSRAW\" data-enlighter-language=\"shell\">.\/letsencrypt-auto certonly --webroot -w \/var\/www\/httpdocs -d domain.tld -d sub.domain.tld<\/code><\/p>\n<p>This, of course, requires your domains to be set up in Plesk the same way, i.e. in Plesk, domains with the names &#8220;domain.tld&#8221;, &#8220;sub.domain.tld&#8221; and &#8220;sub2.domain.tld&#8221; must exist. The Plesk-CLI commands used to register and setup are:<\/p>\n<ul>\n<li>\/opt\/psa\/bin\/certificate -c &lt;certname&gt; -domain &lt;domaine&gt; -key-file &lt;key_file&gt; -cert-file &lt;cert_file&gt; -cacert-file &lt;cacert_file&gt;<\/li>\n<li>\/opt\/psa\/bin\/subscription -u &lt;domain&gt; -certificate-name &lt;certname&gt;<\/li>\n<\/ul>\n<p>If you want your IP-Adresses given a default certificate, you should set both IP-Adress variables AND the &#8220;main domain&#8221; setting. The script will look for a certificate issued for this domain, register it in the admin&#8217;s repositiry and the assign it to both addresses. The respective commands issued are:<\/p>\n<ul>\n<li>\/opt\/psa\/bin\/certificate -c &lt;certname&gt; -admin -key-file &lt;key_file&gt; -cert-file &lt;cert_file&gt; -cacert-file &lt;cacert_file&gt;<\/li>\n<li>\/opt\/psa\/bin\/certificate -ac &lt;certname&gt; -admin -ip &lt;IP&gt;<\/li>\n<\/ul>\n<p>If you want your email communication to be secured with your Lets Encrypt certificates, you have to request a certificate for your &#8220;main domain&#8221; that includes your MX subdomain. For example, if &#8220;main domain&#8221; is set to &#8220;example.com&#8221; and MX for this domain is &#8220;mail.example.com&#8221;, your certificate must include this subdomain. Securing a Plesk-based system&#8217;s email with your own SSL-Certificates has been described by <a href=\"http:\/\/wpguru.co.uk\/2014\/12\/plesk-mail-ssl\/\">Jay Versluis on wpguru.co.uk<\/a>, and I simply scripted his approach.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s encrypt issues SSL-Certificates for free &#8211; meanwhile most Systems trust their CA. Let&#8217;s Encrypt features a CLI to request, update and install certificates &#8211; which work&#8217;s nicely as long as your server&#8217;s setup is compatible. A Plesk based setup, &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"more-link\" href=\"https:\/\/www.nerdlingen.de\/?p=224\"> <span class=\"screen-reader-text\">Let&#8217;s Encrypt + Plesk<\/span> Read More &raquo;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,4],"tags":[],"class_list":["post-224","post","type-post","status-publish","format-standard","hentry","category-english","category-software"],"_links":{"self":[{"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=\/wp\/v2\/posts\/224","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=224"}],"version-history":[{"count":6,"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions"}],"predecessor-version":[{"id":261,"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=\/wp\/v2\/posts\/224\/revisions\/261"}],"wp:attachment":[{"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=224"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=224"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.nerdlingen.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=224"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}