Apache Tomcat is a free and open-source HTTP server designed to serve Java web pages. Tomcat is an implementation of the Java Servlet, JavaServer Pages, Java Expression Language, and Java WebSocket technologies. It is widely deployed and powers various mission-critical web applications around the world.
The standard way of installing Tomcat on a Linux system such as Ubuntu/CentOS/Debian is manual and time-consuming. This guide will discuss a better way, which is automated and can be reproduced easily.
I wrote an Ansible role to simplify the process. The link to the Github project is shared below.
Environment Setup
I assume you have a CentOS 7+, Ubuntu 16.04+ system with Systemd service manager. This Ansible installation won’t work for Upstart or Sysvinit.
Step 1: Install Ansible
The main dependency on your Workstation is Ansible. Install Ansible on your Linux system using the commands shared below.
###### CentOS ######
sudo yum -y install epel-release && sudo yum -y install ansible
###### Fedora ######
sudo dnf -y install ansible
###### Ubuntu / Linux Mint ######
sudo apt -y update
sudo apt -y install software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt update
sudo apt -y install ansible
###### Debian ######
sudo apt -y update
sudo apt -y software-properties-common
echo "deb http://ppa.launchpad.net/ansible/ansible/ubuntu trusty main" | sudo tee /etc/apt/sources.list.d/ansible.list
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 93C4A3FD7BB9C367
sudo apt update
sudo apt install ansible
###### Arch/Manjaro ######
$ sudo pacman -S ansible
###### macOS ######
sudo easy_install pip
sudo pip install ansible
Confirm ansible installation:
$ ansible --version
ansible 2.9.23
config file = /etc/ansible/ansible.cfg
configured module search path = ['/home/rocky/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/lib/python3.6/site-packages/ansible
executable location = /usr/bin/ansible
python version = 3.6.8 (default, May 19 2021, 03:00:47) [GCC 8.4.1 20200928 (Red Hat 8.4.1-1)]
Step 2: Clone Ansible role from Github
The tomcat ansible role is available publicly on Github for use. Clone it to your workstation.
git clone https://github.com/jmutai/tomcat-ansible.git
cd tomcat-ansible
- Update your inventory, e.g:
$ vim hosts
[tomcat-nodes]
192.168.20.55
- Update variables in playbook file – Set Tomcat version, remote user and Tomcat UI access credentials
$ vim tomcat-setup.yml
---
- name: Tomcat deployment playbook
hosts: tomcat-nodes # Inventory hosts group / server to act on
become: yes # If to escalate privilege
become_method: sudo # Set become method
remote_user: root # Update username for remote server
vars:
tomcat_ver: 9.0.52 # Tomcat version to install
ui_manager_user: manager # User who can access the UI manager section only
ui_manager_pass: Str0ngManagerP@ssw3rd # UI manager user password
ui_admin_username: admin # User who can access bpth manager and admin UI sections
ui_admin_pass: Str0ngAdminP@ssw3rd # UI admin password
roles:
- tomcat
Check Tomcat version on the releases page.
When using non root remote user, become_method is necessary.
become: yes
become_method: sudo
Step 3: Install Apache Tomcat 9 With Ansible
Once all values are updated, you can then run the playbook against your nodes.
Playbook executed as root user – with ssh key:
$ ansible-playbook -i hosts tomcat-setup.yml
Playbook executed as root user – with password:
$ ansible-playbook -i hosts tomcat-setup.yml --ask-pass
Playbook executed as sudo user – with password:
$ ansible-playbook -i hosts tomcat-setup.yml --ask-pass --ask-become-pass
Playbook executed as sudo user – with ssh key and sudo password:
$ ansible-playbook -i hosts tomcat-setup.yml --ask-become-pass
Playbook executed as sudo user – with ssh key and passwordless sudo:
$ ansible-playbook -i hosts tomcat-setup.yml --ask-become-pass
A successful installation output will show output similar to below.
PLAY [Tomcat deployment playbook] **********************************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************************************************
Enter passphrase for key '/var/home/jkmutai/.ssh/id_rsa':
ok: [ubuntu01]
TASK [tomcat : Add the OS specific variables] **********************************************************************************************************
ok: [ubuntu01] => (item=/tmp/tomcat-ansible/tmp2/tomcat-ansible/roles/tomcat/vars/Debian.yml)
TASK [tomcat : include_tasks] **************************************************************************************************************************
included: /tmp/tomcat-ansible/tmp2/tomcat-ansible/roles/tomcat/tasks/tomcat-setup-Debian.yml for ubuntu01
TASK [tomcat : Ensure the system can use the HTTPS transport for APT.] *********************************************************************************
ok: [ubuntu01]
TASK [tomcat : Install APT HTTPS transport.] ***********************************************************************************************************
skipping: [ubuntu01]
TASK [tomcat : Install basic packages] *****************************************************************************************************************
[WARNING]: Updating cache and auto-installing missing dependency: python-apt
changed: [ubuntu01]
TASK [tomcat : Install Default Java (Debian/Ubuntu)] ***************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Add tomcat group] ***********************************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Add "tomcat" user] **********************************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Download Tomcat] ************************************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Create a tomcat directory] **************************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Extract tomcat archive] *****************************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Copy tomcat service file] ***************************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Start and enable tomcat] ****************************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Set UI access credentials] **************************************************************************************************************
changed: [ubuntu01]
TASK [tomcat : Allow access to Manager and Host Manager apps from any IP] ******************************************************************************
changed: [ubuntu01] => (item=/usr/share/tomcat/webapps/host-manager/META-INF/context.xml)
changed: [ubuntu01] => (item=/usr/share/tomcat/webapps/manager/META-INF/context.xml)
RUNNING HANDLER [tomcat : restart tomcat] **************************************************************************************************************
changed: [ubuntu01]
PLAY RECAP *********************************************************************************************************************************************
ubuntu01 : ok=16 changed=12 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Step 4: Test Tomcat Installation
Visit the server URL on port 8080. to test tomcat installation and configuration.
- Tomcat web application manager dashboard:
http://<domain_or_IP_address>:8080/manager/html
- Tomcat virtual host manager dashboard:
http://<domain_or_IP_address>:8080/host-manager/html
You can also access the web application manager and host manager by clicking the UI buttons:
Authentication is required when accessing both sections.
Server Status page:
Web application manager page:
Enjoy your Java web apps management with Tomcat.
Related articles: