Tomcat Install SSL Certificate

29/12/2020
Chưa phân loại
Tomcat is a web server that serves web applications written in Java. Tomcat is a project of Apache Foundation and it is completely open source. You can use Tomcat for free. There are many web servers for running Java web applications such as Glassfish, JBoss etc. Tomcat is one of the most popular Java web server among them.

Your Java web application hosted on Tomcat server uses HTTP protocol by default. The communication between the client and the web server is not encrypted when HTTP protocol is used. So, it’s not secure.

These days, almost all the browsers will warn you when you try to visit HTTP links. It will even show you a red lock in the URL bar. If you want that green lock to appear in the URL bar when someone visits your Java web application hosted on Tomcat web server, you have to enable HTTPS on Tomcat. HTTPS is just the secured version of the HTTP protocol. When HTTPS is enabled, the communication between the client and the web server is encrypted.

An SSL (Secure Socket Layer) certificate is used to verify the ownership of the web server the client is connecting to using the HTTPS protocol. You normally buy an SSL certificate from a Certificate Authority or CA such as Verisign, DigiCert etc and install it on your web server (in this case the Tomcat web server).  You can also generate an SSL certificate yourself and install it on your Tomcat web server. Such certificates are called Self-Signed certificates.

In this article, I will show you how to generate your own SSL certificate or Self-Signed certificate and how to install it on Tomcat web server. Let’s get started.

Generating a Self-Signed SSL Certificate:

Before you can install a SSL certificate on your Tomcat web server, you have to generate a Java KeyStore file or JKS file. When you install Apache Tomcat web server, all the required tools to generate a JKS keystore file is also installed. You can use the keytool utility to generate a JKS file on Linux.

I like keeping all the key files I generate in a single directory. So I am going to create a new directory (let’s call it keys) in the /etc directory.

To do that, run the following command:

$ sudo mkdir /etc/keys

Now, navigate to the /etc/keys directory with the following command:

$ cd /etc/keys

Now, generate a JKS certificate file (with the same name as your domain name) with the following command:

$ sudo keytool -genkey -alias tomcat -keyalg RSA -keystore
tomcat.linuxhint.com.jks

NOTE: Here, tomcat is the alias of the Java KeyStore file. You can change it to anything you want. Also, tomcat.linuxhint.com.jks is the name of the output JKS certificate file.

Now type in a password for your Java KeyStore file and press <Enter>.

Re-type the same password and press <Enter>.

Now, type in the Fully Qualified Domain Name (FQDN) of your Tomcat server and press <Enter>. I am just going to put tomcat.linuxhint.com here.

NOTE: If you want to use wildcard domain names, you may do so here. For example, you can type in *.yourdomain.com; In that case, this certificate will be valid for site1.yourdomain.com, site2.yourdomain.com and so on.

Now, type in your Organizational Unit (OU) and press <Enter>.

Now, type in the name of your Company or Organization here and press <Enter>.

Now, type in the name of the City of your Company or Organization and press <Enter>.

Now, type in the name of the State the of City you typed in earlier and press <Enter>.

Now type in the two letter country code of your country and press <Enter>.

Finally, type in yes to confirm that all the information is correct and press <Enter>.

Your JKS certificate file should be generated.

As you can see, the JKS key file (in my case tomcat.linuxhint.com.jks) is generated in the /etc/keys directory.

In the next section of this article, I will show you how to install the JKS self-signed certificate on your Tomcat web server.

Installing Self-Signed SSL Certificate on Tomcat Web Server:

Now that you have a self-signed SSL certificate, you can install it on your Apache Tomcat web server and enable HTTPS protocol very easily. To do that, you have to modify the server.xml file of Apache Tomcat web server.  On Ubuntu 18.04 LTS, the server.xml file is in the path /etc/tomcat8/server.xml

Now, edit the server.xml file with the following command:

$ sudo nano /etc/tomcat8/server.xml

Now, find the line as marked in the screenshot below.

Inside the <Service name=”Catalina”> container, add the following lines.

<Connector port="HTTPS_PORT" maxThreads="150"

scheme="https" secure="true" SSLEnabled="true"

keystoreFile="PATH_TO_YOUR_JKS_FILE" keystorePass="YOUR_KEYSTORE_PASSWORD"

clientAuth="false" keyAlias="YOUR_KEY_ALIAS" sslProtocol="TLS"/>

NOTE: Make sure you change HTTPS_PORT, PATH_TO_YOUR_JKS_FILE and YOUR_KEYSTORE_PASSWORD, YOUR_KEY_ALIAS according to your need.

Finally, the server.xml file should look something like this as marked in the screenshot below. Save the file by pressing <Ctrl> + x and then press y followed by <Enter>.

Now, restart Tomcat service with the following command:

$ sudo systemctl restart tomcat8

Now check whether the Tomcat service is running with the following command:

$ sudo systemctl status tomcat8

As you can see, the Tomcat service is running. It means, the configuration was successful.

Now open up a web browser and try to access your website hosted on the Tomcat web server. You should see the following warning. Click on Advanced.

It just means, your SSL certificate is self-signed. If you buy your SSL certificate from a Certificate Authority (CA), then you won’t see this. Now, click on Add Exception…

Now, click on Confirm Security Exception.

As you can see, it works. The green lock icon appears as well.

So, that’s how you install SSL certificate on your Tomcat web server. Thanks for reading this article.

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

How to install Qmmp 1.1.9 – Winamp Like Music Player on Linux

Qmmp 1.1.9 recently released, is a audio media player that was written with the help of the Qt library hence making it...
28/12/2020

[10 phút ][Ansible] [Cơ bản] [Phần 1] Ansile dùng để làm gì?

Làm hạ tầng giống như làm lập trình (nôm na của câu “Infrastructure As Code”) mà trong các...
30/12/2020

GUI Automation using Pyautogui, Python

PYAUTOGUI is an automation module provided by Python for controlling keyboard and mouse functions via program. This module...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Tìm Hiểu Về Thuê Proxy US – Lợi Ích và Cách Sử Dụng Hiệu Quả
11/12/2024

Mua Proxy V6 Nuôi Facebook Spam Hiệu Quả Tại Onetcomvn
03/06/2024

Hướng dẫn cách sử dụng ProxyDroid để duyệt web ẩn danh
03/06/2024

Mua proxy Onet uy tín tại Onet.com.vn
03/06/2024

Thuê mua IPv4 giá rẻ, tốc độ nhanh, uy tín #1
28/05/2024