下面以 Debian + Tomcat(常见 9/10/11) 为例,说明如何配置 SSL(HTTPS)。你可以按需选择 自签名证书 或 正式证书(如 Let’s Encrypt)。
systemctl status tomcat
# 或
systemctl status tomcat9
Tomcat 配置文件一般在:
/etc/tomcat9/server.xml
keytool -genkeypair \
-alias tomcat \
-keyalg RSA \
-keysize 2048 \
-validity 365 \
-keystore /etc/tomcat9/keystore.jks
按提示输入:
编辑:
nano /etc/tomcat9/server.xml
找到并修改(或取消注释)Connector:
<Connector
port="8443"
protocol="org.apache.coyote.http11.Http11NioProtocol"
maxThreads="150"
SSLEnabled="true">
<SSLHostConfig>
<Certificate
certificateKeystoreFile="/etc/tomcat9/keystore.jks"
certificateKeystorePassword="你的密码"
type="RSA" />
</SSLHostConfig>
</Connector>
systemctl restart tomcat9
访问:
https://服务器IP:8443
apt update
apt install certbot
certbot certonly --standalone -d www.example.com
证书位置:
/etc/letsencrypt/live/www.example.com/
openssl pkcs12 -export \
-in fullchain.pem \
-inkey privkey.pem \
-out /etc/tomcat9/tomcat.p12 \
-name tomcat
<Connector
port="8443"
protocol="org.apache.coyote.http11.Http11NioProtocol"
SSLEnabled="true">
<SSLHostConfig>
<Certificate
certificateKeystoreFile="/etc/tomcat9/tomcat.p12"
certificateKeystorePassword="导出的密码"
certificateKeystoreType="PKCS12"
type="RSA" />
</SSLHostConfig>
</Connector>
certbot renew --dry-run
可在 renew-hook 中重启 Tomcat。
编辑:
/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>
journalctl -xe
ufw allow 8443
如果你告诉我:
我可以给你精确到命令的配置方案。