How to Install Apache tomcat using Ansible.

If you are looking to install apache tomcat instances, consider using Ansible as a great way.

Ansible is an agentless automation tool that manages machines over the SSH protocol by default. Once installed, Ansible does not add a database, and there will be no daemons to start or keep running.

With Ansible, you can create an ansible playbook and use it to deploy dozens of tomcat in one go. In this tutorial, you will learn how to install apache tomcat using Ansible. Let’s get started.

Join 28 other followers

Table of Content

  1. Prerequisites
  2. Building tomcat Ansible-playbook on the Ansible Controller
  3. Running Ansible-playbook on the Ansible Controller
  4. Tomcat files and Tomcat directories on a remote node
  5. Conclusion

Prerequisites

This post will be a step-by-step tutorial. If you’d like to follow along, be sure you have:

  • An Ansible controller host. This tutorial will be using Ansible v2.9.18.
  • A remote Linux computer to test out the tomcat installation. This tutorial uses Ubuntu 20.04.3 LTS as the remote node.
  • An inventory file and one or more hosts are configured to run Ansible commands and playbooks. The remote Linux computer is called webserver, and this tutorial uses an inventory group called web.

Ensure your remote machine IP address is inside /etc/ansible/hosts ( either one remote machine or define it as a group)

Building tomcat Ansible-playbook on the Ansible Controller

Ansible is an automation tool used for deploying applications and systems easily; it could be Cloud, Services, orchestration, etc. Ansible uses YAML Language to build playbooks which are finally used to deploy or configure the required change. To deploy tomcat, let’s move ahead and create the ansible-playbook.

  • SSH or login into your any Linux machine.
  • Create a file named my_playbook3.yml inside /etc/ansible folder and paste below code.

The below playbook contains all the tasks to install tomcat on the remote node. The first task is to update your system packages by using the apt command, further creating tomcat user and group. The next task is to install java, install tomcat, and create necessary folders and permissions for the tomcat directory.

---
- name: Install Apache Tomcat10 using ansible
  hosts: webserver
  remote_user: ubuntu
  become: true
  tasks:
    - name: Update the System Packages
      apt:
        upgrade: yes
        update_cache: yes

    - name: Create a Tomcat User
      user:
        name: tomcat

    - name: Create a Tomcat Group
      group:
        name: tomcat

    - name: Install JAVA
      apt:
        name: default-jdk
        state: present


    - name: Create a Tomcat Directory
      file:
        path: /opt/tomcat10
        owner: tomcat
        group: tomcat
        mode: 755
        recurse: yes

    - name: download & unarchive tomcat10 
      unarchive:
        src: https://mirrors.estointernet.in/apache/tomcat/tomcat-10/v10.0.4/bin/apache-tomcat- 10.0.4.tar.gz
        dest: /opt/tomcat10
        remote_src: yes
        extra_opts: [--strip-components=1]

    - name: Change ownership of tomcat directory
      file:
        path: /opt/tomcat10
        owner: tomcat
        group: tomcat
        mode: "u+rwx,g+rx,o=rx"
        recurse: yes
        state: directory

    - name: Copy Tomcat service from local to remote
      copy:
        src: /etc/tomcat.service
        dest: /etc/systemd/system/
        mode: 0755

    - name: Start and Enable Tomcat 10 on sever
      systemd:
        name: tomcat
        state: started
        daemon_reload: true


Running Ansible-playbook on the Ansible Controller

Earlier in the previous section, you created the ansible-playbook, which is great, but it is not doing much unless you deploy it. To deploy the playbook using the ansible-playbook command.

Assuming you are logged into Ansible controller:

  • Now run the playbook using the below ansible-playbook command.
ansible-playbook my_playbook3.yml

As you can see below, all the tasks are successfully completed; if the status of TASK shows ok, that means the task was already completed; else, for changed status, Ansible performs the task on the remote node.

Running the ansible-playbook in the Ansible controller host
Running the ansible-playbook in the Ansible controller host
  • Next, verify remote machine if Apache Tomcat is installed successfully and started use the below command.
systemctl status tomcat 
service tomcat status
Verifying the tomcat service on the remote node
Verifying the tomcat service on the remote node
  • Also you can verify by running process command.
ps -ef | grep tomcat
ps -aux | grep tomcat
Checking the tomcat process
Checking the tomcat process
Checking the tomcat process
Checking the tomcat process

Join 28 other followers

Tomcat files and Tomcat directories on a remote node

Now that you have successfully installed the tomcat on the remote node and verified the tomcat service, it is equally important to check the tomcat files created and the purpose of each of them.

  • Firstly all the tomcat files and tomcat directories are stored under <tomcat-installation-directory>/*.

Your installation directory is represented by environmental variable  $CATALINA_HOME

  • The tomcat directory and files should be owned by user tomcat
  • The tomcat user should be member of tomcat group.
Verify all files of tomcat
Verify all files of tomcat
  • <tomcat-installation-directory>/bin: This directory consists of startup and shutdown scripts (startup.sh and shutdown.sh) to run or stop the tomcat directly without using the tomcat service configured.
Verify installation directory of tomcat
Verify installation directory of tomcat
  • <tomcat-installation-directory>/conf: This is very crucial directory where tomcat keeps all its configuration files.
Tomcat configuration directory
Verify Tomcat configuration directory
  • <tomcat-installation-directory>/logs: In case you get any errors while running your tomcat then you can look at your safeguard ie. logs , tomcat creates its own logs under this directory.
Tomcat logs directory
Verify Tomcat logs directory
  • <tomcat-installation-directory>/webapps: This is the directory where you place your code such as .war and run your applications. It is highly recommended to stop tomcat and then deploy your application inside this directory and then start tomcat.
Tomcat Code directory
Verify Tomcat Code directory

Join 28 other followers

Conclusion

In this tutorial, we covered in-depth how can you install Apache Tomcat 10 on the ubuntu 18.0 version using Ansible controller and finally discussed files and directories which are most important for any Apache tomcat admins and developers.

If you wish to run your application on lightweight and easily, Apache Tomcat is your friend.

How to Install Tomcat on Ubuntu Machine.

If you are looking to deploy your critical web applications on a web server, nothing could be better than Apache Tomcat.

Tomcat is a lightweight and widely used web server based on the implementation of Java servlets, JSP, and Java expression language. Tomcat provides a pure Java HTTP web server environment where java code runs. Many applications are hosted on tomcat as this is open-source, a victory for system operators.

In this tutorial, you will learn how to install Apache Tomcat 10.0 on an Ubuntu Linux machine.

Join 28 other followers

Table of Content

  1. Prerequisites
  2. How to Install Java 11 on ubuntu 18.04 machine
  3. How to Install Tomcat 10 on ubuntu 18.04 machine
  4. Tomcat files and Tomcat directories on a remote node
  5. Conclusion

Prerequisites

This post will be a step-by-step tutorial. To follow along, be sure you have the following:

Apache Tomcat is supported on all Windows, Linux, and macOS operating systems.

How to Install Java 11 on ubuntu 18.04 machine

As previously specified, Tomcat requires Java to be installed as tomcat implements Java-based technologies. If you don’t have java installed, let’s learn how to install Java Version 11 on the ubuntu 18.04 machine.

  • Connect to Ubuntu machine using your favorite SSH client.
  • Next, install java by running the apt install command. default-jdk is an open source java runtime which is most widely used.
# Installing Java Version: Java SE 11 (LTS)
sudo apt install default-jdk 
  • After apt install default-jdk command is executed successfully , verify if java has been installed successfully by running the below command.
java -version               # To check the Installed Java Version
Checking Java version using java -version command
Checking Java version using java -version command
  • To verify the Java you can also check the location of java binaries using which and whereis commands.
which java                  # Provides the location of executable file 
whereis java               # Provides location of all the files related to Java
Checking Java binaries
Checking Java binaries
  • Run the below command to check the installation path of java. The system should respond with the path where Java is installed
update-alternatives --list java

If you have multiples Java installed on your machine and if you want to switch from one Java to another version consider running update-alternatives --config java command.

Checking the installation path of java
Checking the installation path of java

How to install Tomcat 10 on ubuntu 18.04 machine

Now that you have Java installed successfully installed on the ubuntu 18.04 machine, which is great. Next, you need to install a tomcat. Installing Tomcat is a straightforward task; let’s checkout.

  • Create a folder named tomcat inside the opt directory mkdir command.
cd /opt
mkdir tomcat
  • Download the Binary distribution of Tomcat 10 using curl command as shown below..
curl -O https://mirrors.estointernet.in/apache/tomcat/tomcat-10/v10.0.4/bin/apache-tomcat-10.0.4.tar.gz
Download the Binary distribution of Tomcat 10
Download the Binary distribution of Tomcat 10
  • Extract the tomcat archieve that you just downloaded using tar command. After you execute the tar command you should see the tomcat folder in the opt directory.
 sudo tar xzvf apache-tomcat-10.0.4.tar.gz -C /opt/tomcat --strip-components=1
Extract the tomcat archive
Extract the tomcat archive
  • Now that you have tomcat installed but you should consider running tomcat as a tomcat user which should be the part of tomcat group.
  • Creating a new group tomcat using groupadd command.
sudo groupadd tomcat
  • Next, create a tomcat user using useradd command and make it part of tomcat group .
    • -s /bin/false denotes that nobody can login as this user
    • /opt/tomcat will be tomcat home directory.
sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat      
  • To run tomcat as tomcat user assign the tomcat user and tomcat group to the tomcat directory.
cd /opt/tomcat                     # Go to tomcat directory
sudo chgrp -R tomcat /opt/tomcat   # tomcat group given group ownership on /opt/tomcat
sudo chmod -R g+r conf             # Assign Read permission to tomcat group on conf
sudo chmod g+x conf                # Assign Execute permission to tomcat group on conf
sudo chown -R tomcat opt/tomcat    # Assign tomcat as owner of the directory 
  • Now you are ready to start the tomcat application but it is always recommended to run the application using a service because the service saves applications to stop if the system reboots mistakenly or by any means. Let’s create the tomcat service.
  • To create the tomcat service create a new file named tomcat.service as shown below.
sudo vi /etc/systemd/system/tomcat.service
  • Next, copy and paste the below code in tomcat.service
[Unit]
Description=Apache Tomcat 
After=network.target

[Service]
Type=forking

Environment=JAVA_HOME=/usr/lib/jvm/java-1.11.0-openjdk-amd64
Environment=CATALINA_PID=/opt/tomcat/temp/tomcat.pid
Environment=CATALINA_HOME=/opt/tomcat
Environment=CATALINA_BASE=/opt/tomcat

ExecStart=/opt/tomcat/bin/startup.sh
ExecStop=/opt/tomcat/bin/shutdown.sh

User=tomcat
Group=tomcat
UMask=0007
Restart=always

[Install]
WantedBy=multi-user.target
  • Next, run the below commands to load the tomcat.service file & then start and enable the service.
sudo systemctl daemon-reload    # This will load the file in systemd and it.
sudo systemctl start tomcat     # Start the tomcat service
sudo systemctl enable tomcat    # Enable the tomcat service
sudo systemctl status tomcat    # Enable the tomcat service
Checking the status of the tomcat service
Checking the status of the tomcat service

By now, Apache Tomcat 10 service should be started and running successfully. To verify the tomcat application, navigate to the default webpage on the browser and type <IP-address-of your-tomcat-server>:8080.

Make sure to check your inbound rules and if port 8080 is open.

verify the tomcat application,
verify the tomcat application

Join 28 other followers

Tomcat files and Tomcat directories on a remote node

Now that you have successfully installed the tomcat on the remote node and verified the tomcat service, it is equally important to check the tomcat files created and the purpose of each of them.

  • Firstly all the tomcat files and tomcat directories are stored under <tomcat-installation-directory>/*.

Your installation directory is represented by environmental variable  $CATALINA_HOME

  • The tomcat directory and files should be owned by user tomcat
  • The tomcat user should be member of tomcat group.
Verify all files of tomcat
Verify all files of tomcat
  • <tomcat-installation-directory>/bin: This directory consists of startup and shutdown scripts (startup.sh and shutdown.sh) to run or stop the tomcat directly without using the tomcat service configured.
Verify installation directory of tomcat
Verify installation directory of tomcat
  • <tomcat-installation-directory>/conf: This is very crucial directory where tomcat keeps all its configuration files.
Tomcat configuration directory
Verify Tomcat configuration directory
  • <tomcat-installation-directory>/logs: In case you get any errors while running your tomcat then you can look at your safeguard ie. logs , tomcat creates its own logs under this directory.
Tomcat logs directory
Verify Tomcat logs directory
  • <tomcat-installation-directory>/webapps: This is the directory where you place your code such as .war and run your applications. It is highly recommended to stop tomcat and then deploy your application inside this directory and then start tomcat.
Tomcat Code directory
Verify Tomcat Code directory

Conclusion

In this tutorial, you have learned how to install Apache Tomcat on an Ubuntu server. Deploying Java applications on Apache Tomcat is a quick and easy process!

Apache Tomcat is the most widely used open-source tool for Java developers to run their web applications. Now that you have installed Apache Tomcat, which Java application will you deploy and manage next?