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 53 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 53 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?

Advertisement

Ultimate Jenkins tutorial for DevOps Engineers

Jenkins is an open source automated CI/CD tool where CI stands for continuous integration and CD stands for Continuous delivery. Jenkins has its own built-in Java servlet container server which is Jetty. Jenkins can also be run in different servlet containers such as Apache tomcat or glassfish.

  • Jenkins is used to perform smooth and quick deployment. It can be deployed to local machine or on premises data center or any cloud.
  • Jenkins takes your code any sort of code such as python, java or go or JS etc. and compiles it using different compiler such as MAVEN one of the most used compiler and then builds your code in war or Zip format and sometimes as a docker Image. Finally once everything is built properly it deploy as an when required . It integrates very well with lots of third party tools.

JAVA_HOME and PATH are variables to enable your operating system to find required Java programs and utilities.

JAVA_HOME: JAVA_HOME is an (OS) environment variable that can optionally be set after either the (JDK) or (JRE) is installed. The JAVA_HOME environment variable points to the file system location where the JDK or JRE was installed. This variable should be configured on all OS’s that have a Java installation, including Windows, Ubuntu, Linux, Mac, and Android. 

The JAVA_HOME environment variable is not actually used by the locally installed Java runtime. Instead, other programs installed on a desktop computer that requires a Java runtime will query the OS for the JAVA_HOME variable to find out where the runtime is installed. After the location of the JDK or JRE installation is found, those programs can initiate Java-based processes, start Java virtual machines and use command-line utilities such as the Java archive utility or the Java compiler, both of which are packaged inside the Java installation’s \bin directory.

  • JAVA_HOME if you installed the JDK (Java Development Kit)
    or
  • JRE_HOME if you installed the JRE (Java Runtime Environment) 

PATH: Set the PATH environment variable if you want to be able to conveniently run the executables (javac.exejava.exejavadoc.exe, and so on) from any directory without having to type the full path of the command. If you do not set the PATH variable, you need to specify the full path to the executable every time you run it, such as:

C:\Java\jdk1.8.0\bin\javac Myprogram.java
# The following is an example of a PATH environment variable:

C:\Java\jdk1.7.0\bin;C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem

Installing Jenkins using msi installer on Windows Machine

MSI is an installer file that installs your program on the executing system. Setup.exe is an application (executable file) that has MSI file(s) as one of the resources. The MSI is the file extension of MSI files. They are Windows installers. An MSI file is a compressed package of installer files. It consists of all the information pertaining to adding, modifying, storing, or removing the respective software.  MSI file includes data, instructions, processes, and add-ons that are necessary for the application to work normally.

EXE is short for Executable. This is any kind of binary file that can be executed. All windows programs are exe files. Prior to MSI files, all installers were EXE files. The exe is a file extension of an executable file. An executable file executes a set of instructions or a code when opening it. An executable file is compiled from source code to binary code. It can be directly executed by the Windows OS. These files are understandable by the machine, and they can be directly executed by the operating system

MSI is a file extension of windows installer which is a software component of Microsoft Windows used for the installation, maintenance, and removal of software. Whereas, exe is a file extension of an executable file that performs indicated tasks according to the encoded instructions. 

  1. Navigate to https://www.jenkins.io/download/ and select windows option and your download of Jenkins msi will begin.
  1. Once downloaded click on the jenkins.msi
  1. Continue the Jenkins setup.
  1. Select the Port 8080 and click on Test Port and then Hit Next.
  1. Provide the admin password from the provided Path mentioned in RED color.
  1. Further install the plugins required for jenkins.
  1. Next,it will prompt for First admin user. Please fill the required information and keep it safe with you , as you will use this to login.
  1. Now Jenkins URL configuration screen will appear , keep it as it is for now.
  1. Click on Save and Finish.
  1. Now your Jenkins is ready , click on Start using Jenkins. Soon, you will see Jenkins Dashboard. You can create New Jobs by clicking on New Item.

Installing Jenkins using jenkins exe on Windows Machine

  1. Similarly now install jenkins.war from jenkins URL and click on Generic Java package(.war).
  2. Next run the command as below.
java -jar jenkins.war -http=8181
  1. Next, copy the Jenkins password from the log output and paste it in the as you did earlier in windows msi section point (5) and follow rest of the points.

Installing jenkins on Apache Tomcat server on Windows Machine

  1. Install the Apache Tomcat on windows machine from https://tomcat.apache.org/download-90.cgi and click on tomcat installer as per your system. This tutorial is performed on 64 bit windows machine.
  1. Next, unzip the tomcat installation folder and copy the jenkin.war file in the webapps folder.
  1. Next, go inside the bin folder and run the tomcat by clicking on the startup batch script.
  1. Finally you will notice that Apache Tomcat has started and Jenkins as well.
  1. Now, navigate to localhost:8080 URL and you should see tomcat page as shown below.
  1. Further, navigate to localhost:8080/jenkins to redirect to Jenkins Page.

Configuring the Jenkins UI

  1. First click on Manage Jenkins and then navigate to Configure system.
  1. Next, add the system message and save it which should display this message on Jenkins everytime as below.
  1. To configure the name of the Jobs add the name Pattern as below.
  1. Next, try creating a a new Jenkins Job with random name then it will not allow you and display the error message.

Managing User’s and Permission’s in Jenkins UI

  • Go to Manage Jenkins and Navigate to Manage users in the Jenkins UI.
  • Then Create three users as shown below admin, dev, qa.
  • Next, Navigate to Manage Jenkins and choose Configure Global Security.
  • Next select Project-based Matrix Authorization Strategy and define the permissions for all users as you want.

Role Based Stratergy

  • In Previous section you noticed that adding all users and grnating all permissions is little tough job. So, instead create a role and add users in it. To do that first step is to install the Plugin as shown below.
  • Next select Role based Stratergy as shown below and define the permissions for all users as you want.
  • Next, navigate to Manage Jenkins and then to Manage and Assign Jenkins and then click on Manage Roles.
  • Add 3 Global Roles named DEV Team, QA Team and admin.
  • Add 2 Items Roles developers and Testers with define patterns so that Jobs names are declared accordingly.
  • Next, Click on Assign Role
  • Assigning the roles as shown below.

Conclusion

In this tutorial you learnt how to install jenkins on windows through various ways , how to configure Jenkins Dashboard UI and how to manager users and Permissions.

The Ultimate Guide: Getting Started with Groovy and Groovy Scripts

Powerful, dynamic language, with static-typing and static compilation capabilities, for the Java platform aimed at improving developer productivity .Groovy syntax is simple and easy. It saves a lot of code and effort thus increasing the productivity of developer if he had to do the same thing in Java.

In this tutorial you will learn what is groovy and how to install Groovy on windows and Linux machine . Later you will see two examples which helps you kickstart for Writing Groovy Scripts.

Table of Content

  1. What is Groovy?
  2. Prerequisites
  3. How to Install Groovy on Windows Machine
  4. How to Install Groovy on Ubuntu Machine
  5. Groovy Syntax
  6. Groovy Examples
  7. Conclusion

What is Groovy?

Groovy is a Powerful static as well as dynamic language which is almost same as Java language with few differences. Groovy language is vastly used in Jenkins Pipelines. It integrates with Java libraries very well to deliver powerful enhancements and features including domain specific language authoring and scripting capabilities.

Basic Features of Groovy

  • Groovy supports all Java libraries and it has its own libraries as well.
  • It has a simple similar syntax as that of Java but in more simpler form
  • It has both static and dynamic nature
  • It has great extensibility for the language and tooling.
  • Last but not the least it a free open source language which is being used by lot of developers.

Prerequisites

  • Ubuntu 18 Machine or Windows machine
  • Make sure to have Java 8 plus installed on machines. To check Java version run the following command.
java --version
On Ubuntu Machine
On Windows Machine

How to Install Groovy on ubuntu Machine

Installing Groovy on Ubuntu machine is pretty straightforward. Lets Install the Groovy on Ubuntu 18 machine.

  • First Update the ubuntu official repository by running the apt command.
sudo apt update
  • Now, download the groovy installation script by running the curl command.
curl -s get.sdkman.io | bash
  • Now Install the groovy using the sdk command

How to install Groovy on Windows machine

  • Now you will see windows installer package, once you click on it it will automatically download the file.
  • Now click on the the downloaded windows installer package and installation will begin.
  • Accept the license Agreement
  • Make sure you select Typical for Setup Type and click on Install
  • Now Groovy is successfully installed on windows machine. Open Groovy console from the Start menu & run a simple command to test.

Groovy Syntax

Shebang line

  • This allows script to run groovy scripts directly from command line provided you have groovy installed and groovy command is available on the PATH
#!/usr/bin/env groovy
println "Hello from the shebang line"

Strings

  • Strings are basically chain of characters. Groovy strings are written with single quotes ' or double quotes '' and even with triple quotes '''
'This is an example of single line'

"This is an example of double line"

def threequotes = '''
line1
line2
line3
'''

String interpolation

Groovy expressions can be interpolated and it is just like replacing a placeholder with its value. Placeholder in groovy are surrounded by ${} or $ . Also if you pass GString in any method where string is required then you should replace it by calling toString() on that method.

def name  = "automate"
def greet =  "Hello $name"

Groovy Examples

Lets now see two examples of Groovy

  1. JsonSlurper : JsonSlurper is a class that parses JSON text or reader content into Groovy data
    • creating instance of the JsonSlurper class
    • Using the parseText function of the JsonSlurper class to parse some JSON text
    • access the values in the JSON string via the key.
import groovy.json.JsonSlurper 

class Example {
   static void main(String[] args) {
      def jsonSlurper = new JsonSlurper() # creating instance of the JsonSlurper class
      def object = jsonSlurper.parseText('{ "name":  "John", "ID" : "1"}') 
 	
      println(object.name);
      println(object.ID);
   } 
}
  1. Catching Exceptions
    • Accessing an array with an index value which is greater than the size of the array
class Example {
   static void main(String[] args) {
      try {
         def arr = new int[3];
         arr[5] = 5;
      }catch(ArrayIndexOutOfBoundsException ex) {
         println("Catching the Array out of Bounds exception");
      }catch(Exception ex) {
         println("Catching the exception");
      }
		
      println("Let's move on after the exception");
   } 
}

Conclusion

This tutorial is pretty straightforward and to get you started with Groovy. In this tutorial you learnt what is groovy and how to install Groovy on windows and Linux machine . Later you learnt two examples which helps you kickstart for Writing Groovy Scripts.

Well , Groovy is used at various places such as Jenkins pipelines etc.What do you plan to code with Groovy next ?