下面给你 Debian 上 Tomcat 配置 SSL(HTTPS) 的常用做法,分 两种常见场景:
sudo apt install certbot
sudo certbot certonly --webroot -w /var/www/html -d example.com
生成后证书一般在:
/etc/letsencrypt/live/example.com/
├── fullchain.pem
└── privkey.pem
keytool -genkeypair -alias tomcat \
-keyalg RSA -keysize 2048 \
-validity 365 \
-keystore /etc/tomcat10/keystore.jks
以 Tomcat 9 / 10 为例,配置文件在:
/etc/tomcat9/server.xml
# 或
/etc/tomcat10/server.xml
先转换证书:
sudo openssl pkcs12 -export \
-in /etc/letsencrypt/live/example.com/fullchain.pem \
-inkey /etc/letsencrypt/live/example.com/privkey.pem \
-out /etc/tomcat10/ssl.p12 \
-name tomcat
server.xml找到并启用(或新增)Connector:
<Connector
port="8443"
protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150"
SSLEnabled="true"
scheme="https"
secure="true">
<SSLHostConfig>
<Certificate
certificateKeystoreFile="/etc/tomcat10/ssl.p12"
certificateKeystorePassword="你的密码"
certificateKeystoreType="PKCS12"
type="RSA" />
</SSLHostConfig>
</Connector>
编辑:
/etc/tomcat9/web.xml
在 </web-app> 前加:
<security-constraint>
<web-resource-collection>
<web-resource-name>HTTPS</web-resource-name>
<url-pattern>/*</url-pattern>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
sudo systemctl restart tomcat9
# 或
sudo systemctl restart tomcat10
访问:
https://example.com:8443
sudo ufw allow 8443
很多人是:
Nginx(443) → Tomcat(8080)
这样 Tomcat 不用直接暴露 SSL。
✅ Nginx 处理 SSL ✅ Tomcat 只跑 HTTP ✅ 更安全、易维护
如果你告诉我:
我可以给你 最合适的一套配置。