温馨提示×

tomcat在debian怎么配 ssl

小樊
50
2026-09-09 12:06:53
栏目: 云计算

下面给你 Debian 上 Tomcat 配置 SSL(HTTPS) 的常用做法,分 两种常见场景


一、准备 SSL 证书

方式 1:使用 Let’s Encrypt(推荐,免费)

sudo apt install certbot
sudo certbot certonly --webroot -w /var/www/html -d example.com

生成后证书一般在:

/etc/letsencrypt/live/example.com/
├── fullchain.pem
└── privkey.pem

方式 2:自签名证书(测试用)

keytool -genkeypair -alias tomcat \
  -keyalg RSA -keysize 2048 \
  -validity 365 \
  -keystore /etc/tomcat10/keystore.jks

二、Tomcat 配置 SSL(关键步骤)

Tomcat 9 / 10 为例,配置文件在:

/etc/tomcat9/server.xml
# 或
/etc/tomcat10/server.xml

1️⃣ 使用 PKCS12(Let’s Encrypt 推荐)

先转换证书:

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

2️⃣ 修改 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>

三、强制 HTTP → HTTPS(可选)

编辑:

/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>

四、重启 Tomcat

sudo systemctl restart tomcat9
# 或
sudo systemctl restart tomcat10

访问:

https://example.com:8443

五、常见问题

❌ 端口被防火墙挡

sudo ufw allow 8443

❌ Debian + Nginx 反向代理(更推荐)

很多人是:

Nginx(443) → Tomcat(8080)

这样 Tomcat 不用直接暴露 SSL。


六、推荐架构(生产)

Nginx 处理 SSLTomcat 只跑 HTTP ✅ 更安全、易维护


如果你告诉我:

  • Tomcat 版本(9 / 10)
  • 是否用 Nginx
  • 域名 or 内网 IP

我可以给你 最合适的一套配置

0