Install Jenkins on EC2 (Amazon Linux 2 AMI)

1) EC2 environment setup

First, let's set up the EC2 environment where we can install Jenkins. All the steps are mentioned below if you do not know how to create an EC2 you can follow the same steps mentioned below in the video.

The thing you have to keep in mind is that Jenkins communicates via port 8080 so we have opened that port in the security group accordingly.

1.1) Accessing the EC2 environment setup

Now, let's access our EC2 via command line using the user as "ec2-user" and the key pair(the key pair changes according to your decision while creating the EC2) that we selected while creating the EC2. Please follow along with the below-mentioned video.

2) Installing Java-1.8 on our EC2

Now let's install java version 1.8 on the server, the commands are mentioned below.

# yum install java-1.8*

once the installation is completed we can check the installed java version using the below command.

# java -version

~Output~
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (build 1.8.0_272-b10)
OpenJDK 64-Bit Server VM (build 25.272-b10, mixed mode)

The same steps are mentioned below video you can follow along with the video.

2.1) Find Home path for the java that we have installed

We need to find the exact path for our Java installation, in order to find that we can use the below command in our server.

# find /usr/lib/jvm/java-1.8* | head -n 3

~Output~

/usr/lib/jvm/java-1.8.0
/usr/lib/jvm/java-1.8.0-openjdk
/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.amzn2.0.1.x86_64   <~ this is the path

The path is where the jre files of java are available

2.2) Configuring Home path for the java in .bash_profile

Now we have to mention the java path in the bash profile of our root user. For this let's go to the home directory of the root user and edit the file named .bash_profile

The steps and commands are mentioned below

let's change our working directory to the home directory of our current user(root) and edit the file named .bash_profile and add the java path accordingly.

 
]# vi ~/.bash_profile

# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.amzn2.0.1.x86_64
PATH=$PATH:$HOME/bin:$JAVA_HOME

export PATH

We have to edit the file .bash_profile and add the exact lines mentioned in purple color in the above snippet. Once the file is saved we need to enter the command "source ~/.bash_profile" to make the .bash_profile reload and get updated with the new details.

Now let's check the java home via command line if everything worked we will get the java path as mentioned below as output.

]# echo $JAVA_HOME

~Output~

/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.272.b10-1.amzn2.0.1.x86_64

All the above steps are demonstrated in the below video.

3) Installing Jenkins

We have configured java in the server properly, now let's install Jenkins and for this let's visit the official website "https://www.jenkins.io/" for the installation instructions. For your reference, the commands used are mentioned below (i have used commands for "CentOS/Fedora/Red Hat" since we are using Amazon Linux 2 AMI as OS in our EC2)

]# sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
]# sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key

Once the above commands are entered we can use the yum command to install Jenkins 

]# yum install jenkins