温馨提示×

debian tomcat怎样配 ssl

小樊
35
2026-10-21 00:28:04
栏目: 云计算

在 Debian 上给 Tomcat 配置 SSL(HTTPS),通常有两种常见方式:


方式一:Tomcat 自身配置 SSL(推荐用于单一应用)

1. 安装 Tomcat(如未安装)

sudo apt update
sudo apt install tomcat9

Tomcat 默认目录:

  • 配置:/etc/tomcat9
  • 日志:/var/log/tomcat9

2. 准备 SSL 证书

方式 A:使用 Let’s Encrypt(推荐)

sudo apt install certbot
sudo certbot certonly --webroot -w /var/lib/tomcat9/webapps/ROOT -d example.com

生成文件:

/etc/letsencrypt/live/example.com/fullchain.pem
/etc/letsencrypt/live/example.com/privkey.pem

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

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

3. 转换证书为 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/tomcat9/tomcat.p12 \
  -name tomcat

4. 修改 Tomcat SSL 配置

编辑:

sudo nano /etc/tomcat9/server.xml

找到并修改 Connector(默认被注释):

<Connector
    port="8443"
    protocol="org.apache.coyote.http11.Http11NioProtocol"
    maxThreads="150"
    SSLEnabled="true">
    <SSLHostConfig>
        <Certificate
            certificateKeystoreFile="/etc/tomcat9/tomcat.p12"
            certificateKeystorePassword="你的密码"
            certificateKeystoreType="PKCS12"
            certificateKeyAlias="tomcat" />
    </SSLHostConfig>
</Connector>

5. 重启 Tomcat

sudo systemctl restart tomcat9

访问:

https://服务器IP:8443

方式二:使用 Nginx 反向代理 SSL(生产推荐)

1. 安装 Nginx

sudo apt install nginx

2. 配置 Nginx

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

3. Tomcat 不需要改 SSL,只跑 HTTP


常见问题

  • 防火墙:放行 8443 或 443
  • 权限:keystore 文件需 tomcat 用户可读
  • 自动续期:Let’s Encrypt 需配置 cron 重启 Tomcat

如果你告诉我:

  • Tomcat 版本(8 / 9 / 10)
  • 是否用域名
  • 是否生产环境

我可以给你更精确的配置。

0 踩