1. Create key and CSR
keytool -genkeypair -alias tomcat -keyalg RSA -keysize 3072 -validity 730 \ -keystore conf/keystore.p12 -storetype PKCS12 \ -dname "CN=www.example.com, O=Example Ltd, C=GB" \ -ext "SAN=dns:www.example.com,dns:example.com" keytool -certreq -alias tomcat -keystore conf/keystore.p12 -file tomcat.csr \ -ext "SAN=dns:www.example.com,dns:example.com"
The CSR goes to the CA. The SAN extension matters: without it, the certificate is valid for no name in browsers. Check the CSR contents in the CSR generator.
2. Import the CA reply
First the chain, then the server certificate under the same alias as the key:
keytool -importcert -alias root -file root.crt -keystore conf/keystore.p12 -trustcacerts keytool -importcert -alias inter -file intermediate.crt -keystore conf/keystore.p12 -trustcacerts keytool -importcert -alias tomcat -file server.crt -keystore conf/keystore.p12 -trustcacerts
The last command must print Certificate reply was installed in keystore. If it asks “Trust this certificate?” instead, the alias was wrong: the certificate would end up as a separate entry without a key. If the CA delivers a file with the whole chain (PKCS#7, .p7b), the last command with that file is enough.
Check:
keytool -list -v -keystore conf/keystore.p12 -alias tomcat | grep -E "Entry type|chain length" # Entry type: PrivateKeyEntry # Certificate chain length: 3
3. Configure the connector
<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
SSLEnabled="true" maxThreads="150" maxParameterCount="1000">
<UpgradeProtocol className="org.apache.coyote.http2.Http2Protocol" />
<SSLHostConfig protocols="TLSv1.2+TLSv1.3">
<Certificate certificateKeystoreFile="conf/keystore.p12"
certificateKeystoreType="PKCS12"
certificateKeystorePassword="secret"
certificateKeyAlias="tomcat" />
</SSLHostConfig>
</Connector>
For a JKS keystore set certificateKeystoreType="JKS", and if the key has its own password, add certificateKeyPassword. The old attributes directly on the connector (keystoreFile, keystorePass, keyAlias) are deprecated and do not belong in the configuration of newer Tomcat versions. Special characters in the password must be escaped in XML: & for &, " for quotes.
Typical error messages
Alias name [tomcat] does not identify a key entry: the alias holds only a certificate, no key. Usually the CA reply was imported under a different alias.keystore password was incorrect: wrong password or wrong type (PKCS12/JKS mixed up).Cannot recover key: with JKS the key has a different password andcertificateKeyPasswordis missing.Invalid keystore format: the file is not a keystore, e.g. a PEM file. For PEM there arecertificateFileandcertificateKeyFile, see Tomcat with Let's Encrypt.- Browser reports a missing chain: the CA reply only contained the server certificate and the intermediates were not in the keystore beforehand. Check with the SSL check.
All steps also work in the browser: the Java keystore helper creates key and CSR, matches the CA reply to the right key automatically and completes the chain. If the certificate comes as a PFX file, see the article Tomcat with a PFX file.
FAQ
JKS or PKCS#12?
PKCS#12. It has been the default since Java 9, and OpenSSL and Windows can read it too. JKS still works, but keytool warns every time.
Do I have to restart Tomcat after changing the certificate?
Yes, or reload the configuration via the manager (/manager/text/sslReload). Tomcat only reads the keystore at startup.
How do I get port 443 instead of 8443?
Tomcat should not run as root. The usual way is forwarding 443 to 8443 in the firewall or a reverse proxy in front.