How To Install Apache Tomcat on Ubuntu 18.04 with Ansible

0
128

What is Apache Tomcat?

Apache Tomcat is a free open source HTTP server designed to server Java web pages. Today, we are going to Install Apache Tomcat on our Ubuntu 18.04 LTS. Because standard way of Installing Tomcat on a Linux system is manual & time consuming. As a result we will use another way which is more fast and automatic.

Environment Setup

You must have Ubuntu 16.04+ system with Systemd service manager. This Ansible installation won’t work for Upstart.

Step 1: Install Ansible

Run the following command to Install Ansible on your system because it is the main dependency.

sudo apt-get install -y  software-properties-common
sudo apt-add-repository --yes --update ppa:ansible/ansible
sudo apt-get update
sudo apt-get install -y ansible

Step 2: Create Ansible folders

Make a directory for our project.

mkdir -p ~/projects/ansible/roles
cd ~/projects/ansible/roles

Ansible YAML files will be located in this parent directory.

Step 3: Create Ansible playbook files

Now, create ansible roles & tasks as YAML files to orchestrate steps of the manual ordered processes.

cd ~/projects/ansible/roles
mkdir -p tomcat/{tasks,handlers,defaults,vars,templates}
  • tasks= contains task files
  • handlers= handler files
  • defaults= default lower priority variables for this role.
  • vars= variables associated with the role
  • templates= holds files for use with template resources

You should see the similar

sabi@Ubuntu:~/projects/ansible$ tree
.
└── roles
└── tomcat
├── defaults
├── handlers
├── tasks
├── templates
└── vars

Create Setup tasks

Do the tasks that will complete Tomcat environment.

nano tomcat/tasks/tomcat-setup-Debian.yml

Paste the below data into the file.

 name: Ensure the system can use the HTTPS transport for APT.
stat:
path: /usr/lib/apt/methods/https
register: apt_https_transport
name: Install APT HTTPS transport.
apt:
name: "apt-transport-https"
state: present
update_cache: yes
when: not apt_https_transport.stat.exists
name: Install basic packages
package:
name: ['vim','aptitude','bash-completion','tmux','tree','htop','wget','unzip','curl','git']
state: present
update_cache: yes
name: Install Default Java (Debian/Ubuntu)
apt:
name: default-jdk
state: present
name: Add tomcat group
group:
name: tomcat
name: Add "tomcat" user
user:
name: tomcat
group: tomcat
home: /usr/share/tomcat
createhome: no
system: yes
name: Download Tomcat
get_url:
url: "{{ tomcat_archive_url }}"
dest: "{{ tomcat_archive_dest }}"
name: Create a tomcat directory
file:
path: /usr/share/tomcat
state: directory
owner: tomcat
group: tomcat
name: Extract tomcat archive
unarchive:
src: "{{ tomcat_archive_dest }}"
dest: /usr/share/tomcat
owner: tomcat
group: tomcat
remote_src: yes
extra_opts: "--strip-components=1"
creates: /usr/share/tomcat/bin
name: Copy tomcat service file
template:
src: templates/tomcat.service.j2
dest: /etc/systemd/system/tomcat.service
when: ansible_service_mgr == "systemd"
name: Start and enable tomcat
service:
daemon_reload: yes
name: tomcat
state: started
enabled: yes
when: ansible_service_mgr == "systemd"

Create vars file

nano defaults/main.yml
...
tomcat_ver: 9.0.21
tomcat_archive_url: https://archive.apache.org/dist/tomcat/tomcat-9/v{{ tomcat_ver }}/bin/apache-tomcat-{{ tomcat_ver }}.tar.gz
tomcat_archive_dest: /tmp/apache-tomcat-{{ tomcat_ver }}.tar.gz

Check the latest releases page for latest Tomcat.

Then add OS specific variables.

$ nano tomcat/vars/Debian.yml
---
JAVA_HOME: /usr/lib/jvm/default-java
$ vim tomcat/vars/RedHat.yml
JAVA_HOME: /usr/lib/jvm/jre

Create Service systemd templates

Now, add Tomcat systemd service as a template.

cd templates
nano templates/tomcat.service.j2
[Unit]
Description=Tomcat
After=syslog.target network.target
[Service]
Type=forking
User=tomcat
Group=tomcat
Environment=JAVA_HOME={{ JAVA_HOME }}
Environment='JAVA_OPTS=-Djava.awt.headless=true'
Environment=CATALINA_HOME=/usr/share/tomcat
Environment=CATALINA_BASE=/usr/share/tomcat
Environment=CATALINA_PID=/usr/share/tomcat/temp/tomcat.pid
ExecStart=/usr/share/tomcat/bin/catalina.sh start
ExecStop=/usr/share/tomcat/bin/catalina.sh stop
[Install]
WantedBy=multi-user.target

Add the same data to file & save it.

Add Handler

nano handlers/main.yml
##Add the data into file
name: restart tomcat
service:
name: tomcat
state: restarted

Define Playbook execution file

Playbook file defines roles to be executed & on which instances.

cd ~/ansible/
nano tomcat-setup.yml
 name: Tomcat deployment playbook hosts: app-servers become: yes become_method: sudo remote_user: vagrant roles: tomcat 

Replace app-servers with the server IP address or hostname or server group defined in the inventory file. This can be a custom file such as hosts in the project directory, or global inventory file -/etc/ansible/hosts.

sudo nano /etc/ansible/hosts
cat /etc/ansible/hosts

See the example

See the final structure of folder.

Step 4: Execute Playbook

Now it’s time to execute our Playbook.

cd ~/projects/ansible
ansible-playbook setup-tomcat.yml
PLAY [Tomcat deployment playbook] *
TASK [Gathering Facts]
ok: [server1]
ok: [server2]
TASK [tomcat : include_tasks] *
included: /home/jmutai/projects/ansible/roles/tomcat/tasks/tomcat-RedHat.yml for server1
included: /home/jmutai/projects/ansible/roles/tomcat/tasks/tomcat-Debian.yml for server2
TASK [tomcat : Set Server timezone] *
changed: [server1]
TASK [tomcat : Add EPEL repository to CentOS] ***
changed: [server1]
TASK [tomcat : Install basic packages] **
changed: [server1]
TASK [tomcat : Install Java 8 CentOS] ***
changed: [server1]
TASK [tomcat : Add tomcat group] **
changed: [server1]
TASK [tomcat : Add "tomcat" user] *
changed: [server1]
TASK [tomcat : Download Tomcat] ***
changed: [server1]
TASK [tomcat : Create a tomcat directory] *
changed: [server1]
TASK [tomcat : Extract tomcat archive] **
changed: [server1]
TASK [tomcat : Copy tomcat service file]
changed: [server1]
TASK [tomcat : Start and enable tomcat] *
changed: [server1]
TASK [tomcat : Start and enable firewalld]
changed: [server1]
TASK [tomcat : Open tomcat port on the firewall]
changed: [server1]
TASK [tomcat : Set Server timezone] *
changed: [server2]
TASK [tomcat : Ensure the system can use the HTTPS transport for APT.]
ok: [server2]
TASK [tomcat : Install APT HTTPS transport.] ****
skipping: [server2]
TASK [tomcat : Install basic packages] **
[WARNING]: Could not find aptitude. Using apt-get instead
changed: [server2]
TASK [tomcat : Install Default Java (Debian/Ubuntu)] **
changed: [server2]
TASK [tomcat : Add tomcat group] **
changed: [server2]
TASK [tomcat : Add "tomcat" user] *
changed: [server2]
TASK [tomcat : Download Tomcat] ***
changed: [server2]
TASK [tomcat : Create a tomcat directory] *
changed: [server2]
TASK [tomcat : Extract tomcat archive] **
changed: [server2]
TASK [tomcat : Copy tomcat service file]
changed: [server2]
TASK [tomcat : Start and enable tomcat] *
changed: [server2]
PLAY RECAP **
server1 : ok=14 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
server2 : ok=13 changed=5 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0

Test your Installation & configuration by visiting URL on port 8080

LEAVE A REPLY

Please enter your comment!
Please enter your name here