在Debian上为Tomcat配置SSL证书主要有两种方式:使用自签名证书或Let’s Encrypt免费证书,以下是具体步骤:
安装依赖工具
sudo apt update && sudo apt install tomcat9 openssl
生成自签名证书
sudo openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 \
-keyout /etc/tomcat9/tomcat.key -out /etc/tomcat9/tomcat.crt
创建密钥库(JKS格式)
sudo keytool -import -alias tomcat -file /etc/tomcat9/tomcat.crt \
-keyfile /etc/tomcat9/tomcat.key -keystore /etc/tomcat9/keystore.jks \
-storepass 123456 -noprompt
(密码需替换为实际值,若需双向认证可添加客户端证书)
配置Tomcat
编辑/etc/tomcat9/server.xml,找到或添加以下配置:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           SSLEnabled="true" keystoreFile="/etc/tomcat9/keystore.jks"
           keystorePass="123456" sslProtocol="TLS" />
重启Tomcat并验证
sudo systemctl restart tomcat9
访问https://服务器IP:8443,若出现安全锁图标则配置成功。
安装Certbot获取证书
sudo apt install certbot
sudo certbot certonly --standalone -d yourdomain.com -d www.yourdomain.com
证书默认存储在/etc/letsencrypt/live/yourdomain.com/。
配置Tomcat
编辑/etc/tomcat9/server.xml,添加以下内容:
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
           SSLEnabled="true">
    <SSLHostConfig>
        <Certificate certificateKeystoreFile="/etc/letsencrypt/live/yourdomain.com/keystore.jks"
                     certificateKeystorePassword="your_password"
                     type="RSA" />
    </SSLHostConfig>
</Connector>
(需先将证书导入JKS,参考的keytool命令)
设置自动续期
编辑/etc/letsencrypt/renewal-hooks/deploy/post.sh,添加:
systemctl restart tomcat9
确保Certbot续期后自动重启Tomcat。
重启服务并验证
sudo systemctl restart tomcat9
访问https://yourdomain.com:8443,确认证书有效。
<Connector>中指定。sudo ufw allow 443/tcp)。参考来源: