Zimbra SSL证书一键安装和续期脚本

上篇我们讲述了使用 Let's Encrypt 为Zimbra 安装可信任的SSL证书,但是是手动方式,由于Let's Encrypt 证书有效期只有3个月,所以每3个月需要再次进行续期,每次都手动做一下的话很麻烦,于是我这里把步骤都整理成一个脚本,当需要续期的时候直接运行脚本即可,同时也可以用于首次安装证书。

说是一键安装脚本,实际上由于生成证书的时候有交互,且首次安装证书与续期的时候交互不一样,所以脚本里不好控制,就没有实现完全的一键安装,但已经大大的节省了时间成本,每3个月运行一次,一年就运行4次,有何不可呢。

系统环境:CentOS7.7 64位

Zimbra版本:Zimbra-8.8.15

说明:本脚本仅支持 Zimbra8.7及以上版本,Zimbra8.6及以下版本请访问zimbra wiki原文进行操作:https://wiki.zimbra.com/wiki/Installing_a_LetsEncrypt_SSL_Certificate

脚本内容如下:

cat zimbra-ssl-install-and-renew.sh

#!/bin/bash
# Author: Alex Chen 
# Website: https://www.chenxie.net/
# Create time: 2019/11/29
# Description: This script is for zimbra ssl certificate deploy and renew by Let's Encrypt.
# Note: Support Zimbra8.7 and above

if [ $USER != "root" ]; then
	echo "Must be root."
	exit
fi

domain=`hostname`
time=`date +%Y-%m-%d\ %H:%M:%S`

echo ">>> [$time] Start renew..."

echo ">>> [$time] Check local letsencrypt directory..."
if [ -d /opt/software/letsencrypt ]; then
	rm -rf /opt/software/letsencrypt
fi
if [ ! -f /usr/bin/git ]; then
	yum install -y git
fi

echo ">>> [$time] Clone letsencrypt to local /opt/software/letsencrypt"
mkdir -p /opt/software/letsencrypt
git clone https://github.com/letsencrypt/letsencrypt /opt/software/letsencrypt/

echo ">>> [$time] Stop service."
su - zimbra -c 'zmproxyctl stop'
su - zimbra -c 'zmmailboxdctl stop'

echo ">>> [$time] Build certificate..."
cd /opt/software/letsencrypt/ && ./letsencrypt-auto certonly --standalone

echo ">>> [$time] SSL Certificate files below:"
ls -l /etc/letsencrypt/live/$domain/

echo ">>> [$time] Build root CA."
echo '''
-----BEGIN CERTIFICATE-----
MIIDS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GL+T0yjWW06XyxV3bqxbYo
Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ
-----END CERTIFICATE-----
''' >> /etc/letsencrypt/live/$domain/chain.pem

echo ">>> [$time] Prepare verify certificate."
if [ ! -d /opt/zimbra/ssl/letsencrypt ]; then
	mkdir /opt/zimbra/ssl/letsencrypt
fi
cp /etc/letsencrypt/live/$domain/* /opt/zimbra/ssl/letsencrypt/
chown zimbra:zimbra /opt/zimbra/ssl/letsencrypt/*

echo ">>> [$time] Verify certificate."
su - zimbra -c 'cd /opt/zimbra/ssl/letsencrypt/ && /opt/zimbra/bin/zmcertmgr verifycrt comm privkey.pem cert.pem chain.pem'

echo ">>> [$time] Start deploy..."
echo ">>> [$time] Backup Zimbra SSL directory"
cp -a /opt/zimbra/ssl/zimbra /opt/zimbra/ssl/zimbra.$(date "+%Y%m%d")

echo ">>> [$time] Copy the private key under Zimbra SSL commercial path."
cp /opt/zimbra/ssl/letsencrypt/privkey.pem /opt/zimbra/ssl/zimbra/commercial/commercial.key
chown zimbra.zimbra /opt/zimbra/ssl/zimbra/commercial/commercial.key

echo ">>> [$time] Final SSL deployment"
su - zimbra  -c 'cd /opt/zimbra/ssl/letsencrypt/ && /opt/zimbra/bin/zmcertmgr deploycrt comm cert.pem chain.pem'

echo ">>> [$time] Restart zimbra service."
su - zimbra -c 'zmcontrol restart'

echo ">>> [$time] Clean /opt/software/letsencrypt/"
rm -rf /opt/software/letsencrypt/

echo ">>> [$time] Done."

授予执行权限:

chmod +x zimbra-ssl-install-and-renew.sh

运行脚本:

./zimbra-ssl-install-and-renew.sh

按照提示操作即可。

你可能感兴趣的:(邮件服务器,Zimbra,https)