Tomcat is lightweight and widely used web server based on implementation of
Java servlets ,
Java expression language. Tomcat provides pure java http web server environment in which java code runs. There are many applications which are hosted on tomcat as this is an open source which is a victory for system operators .
There are lots of features and benefits of tomcat web server and one of them is many tomcat applications can run on same server with different port allocated to them which we will discuss later in the course. In this tutorial you will learn complete knowledge on tomcat from basics to Advance level.
Table of Contents
- How to Install Java Version 11 on ubuntu 18.04 machine
- How to Install Tomcat 10 on ubuntu 18.04 machine
- How to Install Tomcat on several machine using Ansible Controller
- Files and Directories in Tomcat
How to Install Java Version 11 on ubuntu 18.04 machine
Before you install Tomcat , make sure you have Java already installed. If you don’t have java installed follow me.
- Install java by running the apt
installcommand. default-jdk is an open source java runtime.
# Here we are installing Java Version: Java SE 11 (LTS) sudo apt install default-jdk
- After command is executed successfully , verify java by running the below command.
java -version # To check the Installed Java Version
- To check the location of java binaries that got installed use which and whereis commands.
which java : # Provides the location of executable file whereis java # Provides location of all the files related to Java
- Run the below command below command which provides the installation path of java, also know as the JAVA_HOME environmental variable.
sudo update-java-alternatives -l
The Installation directory of Java is
/usr/lib/jvm/java-1.8.0-openjdk-amd64 which you will use later in the tutorial to as a tomcat service.
How to install Tomcat 10 on ubuntu 18.04 machine
Now, Java is successfully installed on our ubuntu 18.04 machine. Next install tomcat.
- Create a directory tomcat under opt using
cd /opt mkdir tomcat
2. Download the Binary distribution of Tomcat 10 using
curl -O https://mirrors.estointernet.in/apache/tomcat/tomcat-10/v10.0.4/bin/apache-tomcat-10.0.4.tar.gz
3. Extract the achieve of tomcat using
sudo tar xzvf apache-tomcat-10.0.4.tar.gz -C /opt/tomcat --strip-components=1
4. Generally avoid running applications from root user. As you are installing tomcat , lets name a user as tomcat that and assign it in tomcat group. Lets create it !
- Create a new group tomcat using
groupaddcommand and tomcat user with
useraddcommand and make it part of tomcat group .
- -s /bin/false is used so that nobody can login as this user and /opt/tomcat will be tomcats home directory.
sudo groupadd tomcat sudo useradd -s /bin/false -g tomcat -d /opt/tomcat tomcat
5. After creating tomcat user and group assign the tomcat directory permissions to tomcat user and group.
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
6. Now run and start the tomcat application but it is always recommended to run application using a service. Creating and enabling a service saves application to stop if system reboots mistakenly or by any means. Let’s create the tomcat service.
- Create a file called tomcat.service as below
sudo vi /etc/systemd/system/tomcat.service
- 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
7. Next, run below command to load the 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
Apache tomcat 10 is started and running now. Verify the tomcat by opening the default webpage by navigating to your browser and type <IP-address-of your-tomcat-server>:8080. Make sure to check your inbound rules and if port 8080 is open.
How to Install Tomcat on several machine using Ansible Controller
Ansible is a Automation Tool used for deploying applications and system 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.
Ansible is an agentless automation tool that by default manages machines over the SSH protocol. Once installed, Ansible does not add a database, and there will be no daemons to start or keep running.
Let us install tomcat using Ansible controller.
- SSH or login into your any Linux machine.
- Ansible must be already installed, in case it is not installed please refer: Install Ansible on Ubuntu Machine
- Enter your remote machine IP address inside /etc/ansible/hosts ( either one remote machine or define it as a group)
- Here in our case group of servers name is : webserver
- Create a file my_playbook3.yml and paste below code.
--- - 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
- Now run the playbook with following command
- Now check on remote machine if Apache Tomcat is installed successfully and started
- Also you can verify by running process command.
ps -ef | grep tomcat ps -aux | grep tomcat
Files and Directories in Tomcat
It is very important to understand the files and directories inside the tomcat installation folder. Lets see what are the main files which you should know and take care of.
- Firstly all the files and directories under <tomcat-installation-directory>/* should be owned by user tomcat and group ownership should also be given to tomcat.
- Your installation directory is represented by environmental variable $CATALINA_HOME
- When you have multiple instances of tomcat then you use $CATALINA_HOME
- <tomcat-installation-directory>/bin: This directory consists of startup and shutdown scripts. In case of unix machine you will see something like startup.sh and shutdown.sh scripts.
- <tomcat-installation-directory>/conf: This is very crucial directory where tomcat keeps all its configuration files.
- <tomcat-installation-directory>/logs: In case you get any errors or while troubleshooting you find any issues while running your tomcat or your application you can look at your safeguard ie. logs , tomcat creates its own logs under this directory.
- <tomcat-installation-directory>/webapps: This is the directory where you place your .war and run your applications. It is highly recommended to stop tomcat and then deploy your application inside this directory and then start tomcat.
In this tutorial we covered in depth of Apache Tomcat 10, How can you install Apache Tomcat 10 on ubuntu 18.0 version , How can you install Apache Tomcat 10 on ubuntu 18.0 version using Ansible controller and finally discussed about 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 , then Apache Tomcat is your friend.