habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Install Tomcat9 and Apache with Mod JK in Ubuntu 20.04 – Part 1

3 min read

Install-Tomcat9-and-Apache-with-Mod-JK-in-Ubuntu-20.04

Hi Dude, today i will make a post about install tomcat apache mod jk running in one node/server. In my post before a have already make article about tomcat9 in ubuntu 20.04. Okay let’s do it. Install Tomcat9 and Apache with Mod JK in Ubuntu 20.04.

NOTE: In this post, i will install Tomcat and apache with mod jk from ubuntu repository.

Disable Firewall

At first, i am disable firewall internal server, because live live production because the firewall is already handled by the router.

systemctl stop ufw
systemctl disable ufw

Set Hostname and /etc/hosts

First, Set with hostnamectl

root@tcserver:~# hostnamectl set-hostname tcserver.habibza.in

Second, hostname in /etc/hosts.

root@tcserver:~# echo "192.168.197.144 tcserver.habibza.in tcserver" >> /etc/hosts
root@tcserver:~# cat /etc/hosts
127.0.0.1 localhost
192.168.197.144 tcserver.habibza.in tcserver

And then install use apt.

root@tcserver:~# apt update
root@tcserver:~# apt-get install tomcat9 tomcat9-docs tomcat9-examples tomcat9-admin

After installation fisinshed, lets check java version.

root@tcserver:~# java -version

openjdk version "11.0.11" 2021-04-20
OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing)

after that go to /etc/tomcat9/tomcat-users.xml

<tomcat-users . . .>
    <user username="admin" password="password" roles="manager-gui,admin-gui"/>
</tomcat-users>

Open browser and type this url.

http://192.168.197.144:8080

So if wverything wis fine, will show tomcat default page.

See also  Securing Tomcat with SSL - Certificate Self Signed
install tomcat apache mod jk
tomcat-default-page with port 8080

Next, install Apache and Mod jk.

root@tcserver:~# apt install apache2 libapache2-mod-jk
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
  apache2-bin libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.2-0
Suggested packages:
  apache2-doc apache2-suexec-pristine | apache2-suexec-custom www-browser libapache-mod-jk-doc tomcat8 | tomcat9
The following NEW packages will be installed:
  apache2-bin libapache2-mod-jk libapr1 libaprutil1 libaprutil1-dbd-sqlite3 libaprutil1-ldap libjansson4 liblua5.2-0
0 upgraded, 8 newly installed, 0 to remove and 5 not upgraded.
Need to get 1,675 kB of archives.
After this operation, 6,674 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

Verify apache installed well with type in browser:

 http://192.168.197.144
install tomcat apache mod jk
apache default page

Enable JK Module

let’s check, whether the mod is loaded or not.

root@tcserver:~# systemctl restart apache2.service
root@tcserver:~# apache2ctl -M | grep jk
 <span style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">jk_module (shared)</span>

Look bold red jk_module (shared)which means Mod JK already active.

Configuring Tomcat communicate with Apache via Mod JK and Pointing Worker

We have to enable the AJP Connector in Tomcat, so that the Apache server can redirect requests to Tomcat. Lets edit Tomcat’s server.xml file.

root@tcserver:~# vim /etc/tomcat9/server.xml

Make sure that the AJP Connector below is active and not commented out.

<Connector protocol="AJP/1.3" port="8009" secretRequired="false" address="0.0.0.0" redirectPort="8443" />
See also  Alma Linux 8 - Postfix SMTP Server

Then set route of AJP. In this case, i am use worker name “worker1”.

<Engine name="Catalina" defaultHost="localhost" jvmRoute="worker1">

After that, restart service apache and tomcat

root@tcserver:~# systemctl restart apache2.service && systemctl restart tomcat9.service

After that, check port running.

root@tcserver:~# netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.53:53           0.0.0.0:*               LISTEN      820/systemd-resolve
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      892/sshd: /usr/sbin
tcp6       0      0 :::22                   :::*                    LISTEN      892/sshd: /usr/sbin
tcp6       0      0 ::1:8009                :::*                    LISTEN      21651/java
tcp6       0      0 :::8080                 :::*                    LISTEN      21651/java
tcp6       0      0 :::80                   :::*                    LISTEN      21582/apache2

See important port below:

  • 8009 : Port AJP Connector
  • 8080 : Port Tomcat
  • 80 : Port Apache
  • 22 : Default SSH port

Create workers.properties

This is important part. We need to configure the Apache HTTP Server to load and initialize the JK module.

Lets create the /etc/apache2/workers.properties file. This file defines a list of Tomcat ‘workers’ to which Apache can pass requests.  

root@tcserver:~# vim /etc/apache2/workers.properties

And paste the following into the file:

# Define 1 real worker using ajp13
worker.list=worker1

# Set properties for worker1 (ajp13)
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009

Next reference this file in the primary Apache configuration file apache2.conf:

root@tcserver:~# vim /etc/apache2/apache2.conf

We add these lines at the end:

JkWorkersFile /etc/apache2/workers.properties

# Where to put jk shared memory
JkShmFile /var/log/apache2/mod_jk.shm

# Where to put jk logs
JkLogFile /var/log/apache2/mod_jk.log

# Set the jk log level [debug/error/info]
JkLogLevel info

# Select the timestamp log format
JkLogStampFormat "[ %a %b %d %H:%M:%S %Y ]"

Setting which URLs to manage with Apache

The last, we have to setting which URL manage with apache. Mount Document Root with JkMount.

root@tcserver:~# vim /etc/apache2/sites-enabled/000-default.conf

Add the following line under the DocumentRoot entry. This makes it so that you can request JRS via the Apache web server.

JkMount /* worker1
#JkMount /examples/* worker1
  • JkMount /* worker1 : Mount everything in default document root tomcat in apache with worker1.
  • JkMount /examples/* worker1 : Mount only document root in folder examples in apache with worker1. In this case, i am not using it, it will be use in next deployment. So i give it comment.
See also  Tomcat Load Balancing with Apache HTTP and ModJK
install tomcat apache mod jk
apache conf jk mount

Restart service apache and tomcat.

root@tcserver:~# systemctl restart apache2.service
root@tcserver:~# systemctl restart tomcat9.service

Open in browser, http://ip-web-server without port 8080, it should look the same result as http://ip-web-server:8080.

install tomcat apache mod jk

That is simple post about install tomcat apache mod jk in ubuntu server. Hope this article helps. Please feel free to leave a comment if you have any questions and I’ll appreciate it.


Let's Buy Me Coffee.

Buy Me a Coffee at ko-fi.com

https://saweria.co/habibzain
https://ko-fi.com/habibzain
habibzain Just husband, father and enthusiastic men about System Administration. Love to write short article about it. Perhaps can help and be useful for others.

Easy Create Laravel Project with Composer

Requirement Laravel, a popular PHP framework, is renowned for its elegant syntax and robust features, making it a top choice for web developers. One...
habibzain
1 min read

Ansible Ubuntu 20.04

Introduction Ansible is an open-source automation tool that simplifies configuration management, application deployment, and task automation. It is designed to be simple, lightweight, and...
habibzain
1 min read

Virtualbox Change Storage Size

Changing the storage size of a virtual machine in VirtualBox involves a few steps. Here’s a general guide. Backup your Virtual Machine (VM) Before...
habibzain
35 sec read

Leave a Reply

Your email address will not be published. Required fields are marked *

Never miss good article from us, get weekly updates in your inbox