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?

Brilliant Guide to Check all possible ways to view Disk usage on Ubuntu Machine

Monitoring of application or system disk utilization has always remained a top most and crucial responsibility of any IT engineer. In the IT world with various software’s , automation and tools it is very important to keep a track of disk utilization regularly.

Having said that, In this tutorial we will show you best commands and tools to work with your disk utilization. Please follow me along to read and see these commands and their usage.

Table of content

  1. Check Disk Space using disk free or disk filesystems command ( df )
  2. Check Disk Space using disk usage command ( du )
  3. Check Disk Usage using ls command
  4. Check Disk Usage using pydf command
  5. Check Disk Usage using Ncdu command( Ncurses Disk Usage )
  6. Check Disk Usage using duc command
  7. conclusion

Check Disk Space using disk free or disk filesystems command (df)

It stands for disk free. This command provides us information about the available space and used space on a file system. There are multiple parameters which can be passed along with this utility to provide additional outputs. Lets look at some of the commands from this utility.

  • To see all disk space available on all the mounted file systems on ubuntu machine.
df
  • To see all disk space available on all the mounted file systems on ubuntu machine in human readable format.
    • You will notice a difference in this command output and a previous. The difference is instead of 1k-blocks you will see size which is human readable.
df -h
  • To check the disk usage along with type of filesystem
df -T
  • To check disk usage of particular Filesystem
df /dev/xvda1
  • To check disk usage of multiple directories.
df -h  /opt /var /etc /lib
  • To check only Percent of used disk space
df -h --output=source,pcent
  • To check data usage based on filesystem wise
df -h -t ext4

Check Disk Space using disk usage command ( du )

du command provides disk usage information. This command provides file and directories space utilization. Lets see some of the example .

  • To check disk usage of directory
du /lib # Here we are taking lib directory
  • To check disk usage of directory with different block size type .
    • M for MB
    • G for GB
    • T for TB
du -BM /var
  • To check disk usage according to the size
    • Here s represents summarize
    • Here k represents size in KB , you can use M, G or T and so on
    • Here sort represents sort
    • Here n represents in numerical order
    • Here r represents in reverse order
du -sk /opt/* | sort -nr

Check Disk Usage using ls command

ls command is used of listing of files but also provides information about disk utilized by directories and files. Lets see some of these command.

  • To list the files in human readable format.
ls -lh
  • To list the file in descending order of size of files.
ls -ls

Check Disk Usage using pydf command

pydf is a python based command-line tool which is used to display disk usage with different colors. Lets dive into command now.

  • To check the disk usage with pydf
pydf -h 

Check Disk Usage using Ncdu command (Ncurses Disk Usage)

Ncdu is a disk utility for Unix systems. This command is text-based user interface under the [n]curses programming library.Let us see a command from Ncdu

ncdu

Check Disk Usage using duc command

Duc is a  command line utility which queries the disk usage database and also create, maintain and the database.

  • Before we run a command using duc be sure to install duc package.
sudo apt install duc
  • duc is successfully installed , now lets now run a command
duc index /usr
  • To list the disk usage using duc command with user interface
duc ui /usr

Conclusion

There are various ways to identify and view disk usage in Linux or ubuntu operating system. In this tutorial we learnt and showed best commands and disk utilities to work with . Now are you are ready to troubleshoot disk usage issues or work with your files or application and identify the disk utilization.

Hope this tutorial gave you in depth understanding and best commands to work with disk usage . Hoping you never face any disk issues in your organization. Please share if you like.

How to Install Terraform on an Ubuntu machine

Many automation tools and scripts are available in the market, but one of the most widely used and easy to use is Terraform, also known as Infrastructure as a code tool.

In this tutorial, you’ll install Terraform on Ubuntu 20.04. You’ll then work with Terraform to create a service on Amazon Managed Service. So let’s get started.

Join 28 other followers

Table of Content

  1. What is Terraform?
  2. Prerequisites
  3. How to Install Terraform on Ubuntu 20.04 LTS
  4. Terraform files and Terraform directory structure
  5. Terraform ec2 instance example (terraform aws ec2)
  6. Conclusion

What is Terraform?

Terraform is a tool for building, versioning, and changing the infrastructure. Terraform is Written in GO Language, and the syntax language of configuration files is HashiCorp configuration language(HCL) which is much easier than yaml or json.

Terraform is used with various cloud providers such as Amazon AWS, Oracle, Microsoft Azure, Google Cloud, etc.

Prerequisites

  • Ubuntu machine preferably 18.04 version + , if you don’t have any machine you can create a ec2 instance on AWS account. Recommended to have 4GB RAM and at least 5GB of drive space
  • Ubuntu machine should have IAM role attached with AWS EC2 instance creation permissions or admin rights.

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install Terraform on Ubuntu 20.04 LTS

Now that you have a basic idea about terraform let’s kick off this tutorial by first installing terraform on Ubuntu 20.04 machine.

  • First log in to the Ubuntu machine sing your favorite SSH client such as putty.
  • Next, update the existing system packages on the ubuntu machine by running below command.
sudo apt update
  • Now, download the latest version of Terraform in the opt directory. You can install Terraform on any directory but it is recommended to use opt directory for software installations.
wget https://releases.hashicorp.com/terraform/0.14.8/terraform_0.14.8_linux_amd64.zip
  • Further install zip package which you will need in next step to unzip Terraform zip file.
sudo apt-get install zip -y
  • Now, unzip the Terraform download zip file from the unzip command.
unzip terraform*.zip
  • Next, move the Terraform executable file to executable directory such as /usr/local/bin so that you can run Terraform from any directory of your Ubuntu machine.
sudo mv terraform /usr/local/bin
  • Finally, verify the Terraform installation by running terraform command or terraform version command.
terraform  # To check if terraform is installed 

terraform -version # To check the terraform version </mark> 
Checking the Terraform installation
Checking the Terraform installation
  • This confirms that terraform has been successfully installed on ubuntu 18.04 machine.
Checking the Terraform installation by running terraform version command
Checking the Terraform installation by running terraform version command

Terraform files and Terraform directory structure

Now that you have Terraform installed. Let’s now dive into Terraform files and Terraform directory structure that will help you write the Terraform configuration files later in this tutorial.

Terraform code, that is, Terraform configuration files, are written in a tree-like structure to ease the overall understanding of code with .tf format or .tf.json or .tfvars format. These configuration files are placed inside the Terraform modules.

Terraform modules are on the top level in the hierarchy where configuration files reside. Terraform modules can further call another child to terraform modules from local directories or anywhere in disk or Terraform Registry.

Terraform contains mainly five files as main.tf , vars.tf , providers.tf , output.tf and terraform.tfvars.

  1. main.tf – Terraform main.tf file contains the main code where you define which resources you need to build, update or manage.
  2. vars.tf – Terraform vars.tf file contains the input variables which are customizable and defined inside the main.tf configuration file.
  3. output.tf : The Terraform output.tf file is the file where you declare what output paraeters you wish to fetch after Terraform has been executed that is after terraform apply command.
  4. .terraform: This directory contains cached provider , modules plugins and also contains the last known backend configuration. This is managed by terraform and created after you run terraform init command.
  5. terraform.tfvars files contains the values which are required to be passed for variables that are refered in main.tf and actually decalred in vars.tf file.
  6. providers.tf – The povider.tf is the most important file whrere you define your terraform providers such as terraform aws provider, terraform azure provider etc to authenticate with the cloud provider.

Terraform ec2 instance example (terraform aws ec2)

Let’s wrap up this ultimate guide with a basic Terraform ec2 instance example or terraform aws ec2.

  • Assuming you already have Terraform installed on your machine.
  • First create a folder terraform-demo in opt directory. This folder will contain all the configuraion file that Terraform needs to build ec2.
mkdir /opt/terraform-demo
cd /opt/terraform-demo
  • Now create main.tf file under terraform-demo folder and copy/paste the content below.
resource "aws_instance" "my-machine" {          # This is Resource block where we define what we need to create

  ami = var.ami                                 # ami is required as we need ami in order to create an instance
  instance_type = var.instance_type             # Similarly we need instance_type
}

  • Create one more file named vars.tf file under terraform-demo folder and copy/paste the content below. The vars.tf file contains the variables that you referred in main.tf file.
variable "ami" {                       # We are declaring the variable ami here which we used in main.tf
  type = string      
}

variable "instance_type" {             # We are declaring the variable instance_type here which we used in main.tf
  type = string 
}
  • Create one more file output.tf file under terraform-demo folder and paste the content below. This file will allow Terraform to display he output after running terraform apply command.
output "ec2_arn" {
  value = aws_instance.my-machine.arn    
}  
  • Create provider.tf file under terraform-demo folder and paste the content below.
provider "aws" {     # Defining the Provider Amazon  as we need to run this on AWS  
  region = "us-east-2"
}
  • Create terraform.tfvars file under terraform-demo folder and paste the content below. This file contains the value of Terraform vaiables declared in vars.tf file.
ami = "ami-013f17f36f8b1fefb" 
instance_type = "t2.micro"
  • Now, run tree command which will provide you the folder structure. You should see something like below.
tree command
tree command
  • Now your files and code are ready for execution. Initialize the terraform using the terraform init command.
terraform init
Initializing the terraform using the terraform init command.
Initializing the terraform using the terraform init command.
  • Terraform initialized successfully , now its time to run the plan command which provides you the details of the deployment. Run terraform plan command to confirm if correct resources is going to provisioned or deleted.
terraform plan
Running the terraform plan command
Running the terraform plan command
Running the terraform plan command
Running the terraform plan command
  • After verification, now its time to actually deploy the code using terraform apply command.
terraform apply
Running the terraform apply command
Running the terraform apply command
Output of the terraform apply command
The output of the terraform apply command

Great Job; terraform commands were executed successfully. Now you should have the AWS EC2 instance launched in AWS Cloud.

It generally takes a minute or so to launch an instance, and yes, you can see that the instance is successfully launched now in the us-east-2 region as expected.

Join 28 other followers

Conclusion

In this tutorial, you learned What is terraform, how to Install Terraform on the Ubuntu machine and launch an ec2 instance on an AWS account using terraform. Keep Terraforming !!

Now that you have the AWS EC2 instance launched, what are you planning to deploy on the newly created AWS EC2?

How to Install Docker ubuntu step by step

If you want to deploy multiple applications in an isolated environment, consider using Docker and containers where applications have their own container.

In this tutorial, you’ll install the Docker ubuntu machine. You’ll then work with docker containers and docker images and push an image to a Docker Repository. So let’s get started.

Let’s dive in.

Join 28 other followers

Table of Content

  1. What is Docker?
  2. Prerequisites
  3. How to Install Docker on Ubuntu 18.04 LTS
  4. How to run Docker command using Non-Root user (run docker commands without sudo)
  5. Working with docker command ( docker pull command, docker ps, docker image)
  6. Tag Docker image using docker tag and push docker image to docker hub
  7. Conclusion

What is Docker?

Docker is an open-source tool for developing, shipping, and running applications. It has the ability to run applications in a loosely isolated environment using containers. Docker is an application that helps manage containers in a very smooth and effective way. In containers, you can isolate your applications. Docker is quite similar to a virtual machine, but it is lightweight and easily ported.

Containers are light weighted as they are independent of hypervisors load and configuration. They directly connect with machines, i.e., the host’s kernel.

What is Docker?
What is Docker?

Prerequisites

  • Ubuntu machine preferably 18.04 version +, if you don’t have any machine you can create an ec2 instance on AWS account
  • Recommended to have 4GB RAM and at least 5GB of drive space.
  • An account on Docker Hub if you wish to create your own images and push them to Docker Hub.

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install Docker on Ubuntu 18.04 LTS

Let’s kick off this tutorial by installing docker on ubuntu. Installing docker on the ubuntu machine is a straightforward task. The Docker installation package is available in the official Ubuntu repository may not be the latest version.

This tutorial will install Docker from the official Docker repository to ensure you get the latest version. To do that, add a new package source, add the GPG key from Docker to ensure the downloads are valid, and then install the package.

  • Login to your Ubuntu machine using your favorite SSH client.
  • First, update your existing list of packages by running the below command
sudo apt update
  • Install the below prerequisites software so that apt uses packages over the https protocol. The apt transport software allows your machine to connect with external repositories over HTTPS or HTTP over TLS.
sudo apt install apt-transport-https ca-certificates curl software-properties-common

  • Next, add the GPG key for the official Docker repository to your system. This key builds the trust of your machine with the docker official repository.
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

  • Now, add the Docker repository to APT sources so that you can install the docker installation package.
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"

  • Next, update the package database with the Docker packages from the newly added repo using the following command.
sudo apt update
  • Finally, Install the docker on the ubuntu machine using the apt install command.
sudo apt install docker-ce

  • Docker should be started by now; verify the status and version of docker by using service docker status the command.
Checking the docker service using service docker status command
Checking the docker service using the service docker status command

How to run Docker command using Non-Root user (run docker commands without sudo)

The docker command can only be run by the root user or by a user in the docker group, automatically created during Docker’s installation process. If you attempt to run the docker command without prefixing it with sudo or without being in the docker group, it will give you a permission denied error message.

  • To enable users other than root or to run docker commands without sudo, first create the docker group using the groupadd docker command.
groupadd docker
  • Next, add the users in the docker group that you created earlier and can run Docker commands.
sudo usermod -aG docker jenkins   # To add Jenkins user to docker group
sudo usermod -aG docker user1     # To add user1 to docker group
sudo usermod -aG docker ubuntu    # To add ubuntu user  to docker group
  • Restart the docker service by service docker restart command.
service docker restart 
  • Check the docker version by running the below command.
docker version

Your docker commands should now run succesfully now without giving any permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock like this, if you continue to get this message make sure to logout and login back on your machine.

Verifying the docker version using docker version command
Verifying the docker version using the docker version command

Working with docker command ( docker pull command, docker ps, docker image)

The most important part of docker is to run several docker commands such as docker pull which fetches docker images that contain your code, the environment to run it, and several parameters. Docker images are stored on the Docker repository known as the docker hub, though you can also store them on your local machine.

Docker architecture
Docker architecture
  • Use the docker pull command to pull the docker image on your ubuntu machine.
docker pull ubuntu
Pulling an image from docker repository using docker pull command on ubuntu machine
Pulling an image from docker repository using docker pull command on ubuntu machine
  • Now check the downloaded image on your ubuntu machine and run the docker images command.
docker images
Checking all docker images in the system
Checking all docker images in the system
  • Now, run the first docker container by using the below command.
docker run -it ubuntu # Here i,t provides you interactive shell access and ubuntu is image name
Running docker container using docker run command
Running docker container using docker run command.
  • To check all container details ( Exited, running, etc.), run the docker ps -a command.
 docker ps -a
Checking details of all container details
Checking details of all container details

Tag Docker image using docker tag and push docker image to docker hub

Now, let’s learn how to push the docker image to the docker hub. You need a docker hub account to push the docker image to the docker hub.

Assuming you are still logged into the Ubuntu machine using the SSH client.

  • Login to docker hub with your credentials and then login.
docker login -u docker-registry-user
Login to docker hub using docker login -u command
Login to docker hub using docker login -u command
  • Before you push your docker image to the docker hub, it’s highly recommended to tag your image with your docker hub username using the docker tag command. The syntax of the docker tag command is docker tag image.
docker tag ubuntu:latest  <dockerhub-username>/ubuntu:latest
  • After successful login in docker hub, now push your docker image using the following command.
docker push <dockerhub-username>/ubuntu:latest
Running docker push command
Running docker push command.

As you can see below, the docker image is successfully pushed into the docker hub that you created on the ubuntu machine.

successfully pushed docker image into docker hub
successfully pushed docker image into docker hub

Join 28 other followers

Conclusion

In this tutorial, you learned how to install Docker, learned several docker commands such as docker images, docker tag, docker pull, and how to push docker image to docker hub.

Docker is a great way to host your applications separately and efficiently. So which applications are you planning to run in docker?