Today I will show you a short but powerful script which will renew all Zimbra SSL certificates.

Required for this script to work is certbot package installed on email server and sudo rights to add script in crontab.

You can add script in crontab at a weekly run like this: 0 0 * * 0 root /path_to_script.

The following script will help you to auto-renew SSL certificates for your email server:

#!/bin/bash

#Set domain for renew (in format openthreat.ro)
DOMAIN=""

certbot certificates

EXPIRE_DAYS=`cat /var/log/letsencrypt/letsencrypt.log | grep VALID | cut -d\( -f2 | cut -d\) -f1 | awk '{print $2}'`

if [[ $EXPIRE_DAYS -lt 30 ]]; then

    #Stop the jetty or nginx service at Zimbra level
    runuser -l zimbra -c 'zmproxyctl stop'
    runuser -l zimbra -c 'zmmailboxdctl stop'

    #Renew SSL
    certbot renew --standalone

    #Copy new SSL to Zimbra SSL folder
    cp /etc/letsencrypt/live/mail.$DOMAIN/* /opt/zimbra/ssl/letsencrypt/
    chown zimbra:zimbra /opt/zimbra/ssl/letsencrypt/*

    #Build the proper Intermediate CA plus Root CA
    echo "-----BEGIN CERTIFICATE-----
    MIIFazCCA1OgAwIBAgIRAIIQz7DSQONZRGPgu2OCiwAwDQYJKoZIhvcNAQELBQAw
    TzELMAkGA1UEBhMCVVMxKTAnBgNVBAoTIEludGVybmV0IFNlY3VyaXR5IFJlc2Vh
    cmNoIEdyb3VwMRUwEwYDVQQDEwxJU1JHIFJvb3QgWDEwHhcNMTUwNjA0MTEwNDM4
    WhcNMzUwNjA0MTEwNDM4WjBPMQswCQYDVQQGEwJVUzEpMCcGA1UEChMgSW50ZXJu
    ZXQgU2VjdXJpdHkgUmVzZWFyY2ggR3JvdXAxFTATBgNVBAMTDElTUkcgUm9vdCBY
    MTCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoCggIBAK3oJHP0FDfzm54rVygc
    h77ct984kIxuPOZXoHj3dcKi/vVqbvYATyjb3miGbESTtrFj/RQSa78f0uoxmyF+
    0TM8ukj13Xnfs7j/EvEhmkvBioZxaUpmZmyPfjxwv60pIgbz5MDmgK7iS4+3mX6U
    A5/TR5d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q4JUagEiuTDkHzsxHpFKVK7q4+63SM1N95R1NbdWhscdCb+ZAJzVc
    oyi3B43njTOQ5yOf+1CceWxG1bQVs5ZufpsMljq4Ui0/1lvh+wjChP4kqKOJ2qxq
    4RgqsahDYVvTH9w7jXbyLeiNdd8XM2w9U/t7y0Ff/9yi0GE44Za4rF2LN9d11TPA
    mRGunUHBcnWEvgJBQl9nJEiU0Zsnvgc/ubhPgXRR4Xq37Z0j4r7g1SgEEzwxA57d
    emyPxgcYxn/eR44/KJ4EBs+lVDR3veyJm+kXQ99b21/+jh5Xos1AnX5iItreGCc=
    -----END CERTIFICATE-----" >> /opt/zimbra/ssl/letsencrypt/chain.pem

    #Backup Zimbra SSL directory
    cp -a /opt/zimbra/ssl/zimbra /opt/zimbra/ssl/zimbra.$(date "+%Y%m%d")

    #Copy the private key under Zimbra SSL path
    cp /opt/zimbra/ssl/letsencrypt/privkey.pem /opt/zimbra/ssl/zimbra/commercial/commercial.key

    #Final SSL deployment
    runuser -l zimbra -c 'cd /opt/zimbra/ssl/letsencrypt/ && /opt/zimbra/bin/zmcertmgr deploycrt comm cert.pem chain.pem'
    runuser -l zimbra -c 'zmcontrol restart'

else
    echo "Not expire, skipping!"
fi
Categories: Blog

7 Comments

RooL · July 26, 2021 at 9:12 am

#Set domain for renew (in format openthreat.ro)
DOMAIN=””

How set more domain?

RooL · July 28, 2021 at 1:13 pm

Hi,
thx for reply…

I have one zimbra server and more domains.
I first time LetsEncrypt run command
sudo letsencrypt certonly –standalone -d ipostar.idnes.cz -d posta.pravda.cz -d posta.jablko.cz -d posta.operator.sk -d posta.rambo.com

DOMAIN=”ipostar.idnes.cz posta.pravda.cz posta.jablko.cz posta.operator.sk posta.rambo.com”

Is this right?

thx for help

RooL · July 28, 2021 at 2:02 pm

Oh ok, I already understand how it works …

thank you for the script …

invain · January 3, 2022 at 8:14 am

Thank you for the script – is now in use with zimbra 9

Marin · January 25, 2022 at 10:51 am

Starting with some time I was unable to use this script because the certification authority at Let’sEncrypt changed and the new authority does not identify correctly in my environment with Zimbra on CentOS 8. This script made me a headache because of wrong certification path and uncontrolled application of the modifications. When fresh certifications were applied, my Zimbra didn’t start because of the wrong certificates. Thus my company’s email was down for a whole day.
On the link below you can find another script with the chain issues resolved. But be aware that the script is not recommended to be fully automatic, you must take the controls in your hands. 🙂

https://community.letsencrypt.org/t/can-somebody-help-me-to-solve-the-issue-with-letsencrypt-certificate-for-zimbra/169929/27?u=imagic

ben · February 23, 2022 at 11:05 am

Thank You.

Cool · March 22, 2022 at 6:22 am

Thank you. Such a very short but powerful script.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *