Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)

Jenkins is a powerful application that allows continuous integration and helps in the continuous delivery of projects, regardless of the platform you are working on. It is an open source tool that can handle any kind of build or continuous integration. You can integrate Jenkins with a number of testing and deployment technologies. In this post, we will explain how you can install Jenkins on an Amazon EC2 (Ubuntu Server 14.04 LTS) Instance.

Assuming that you have already completed the below three steps. If not then you can follow our below posts to complete these prerequisites.

  1. First, create a new Amazon EC2 Instance(Ubuntu Server 14.04 LTS) and setup Security Groups and open up ports 22, 80 and 443.  See Blog: Create a new Instance (Ubuntu 14.04) on AWS EC2 and setup Security Groups
  2. Then connect to your EC2 Instance via SSH. See Blog: Connect to AWS EC2 Instance via Putty/SSH
  3. For Jenkins first, you have Install Java on your Instance.  See Blog: Install OpenJDK 8 on Ubuntu 14.04 LTS

Step 1: Fixing Locales in Ubuntu 14.04 LTS on Amazon EC2

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)1

Step 2: The Installation

Now, we’re going to add the repository for the Jenkins package. So, we need to install the public key for the Jenkins repository to tell Ubuntu that this is a trusted repository:

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)2

Then, we need to tell Ubuntu where the package repository is. We just add the package repository to the list of repositories:

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)3

The best practice is to Update packages first:

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)4

Finally, we install Jenkins:

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)5

Step 3: Verify Installation

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)6

This verifies that Jenkins service has been installed is actually running on httpPort=8080.

The reason for that is that Jenkins package actually created a user called Jenkins on the system that allows Jenkins to be run as an unprivileged user.

We don’t want to run it as a root user for security reasons. Now the unprivileged user can’t start the server on privilege port like HTTP port 80 so, it runs on 8080, on the other hand, we don’t have to specify the port 8080 every time to connect to Jenkins server from a web browser.

So, for what we are going to do is to setup an apache server to send proxy requests from port 80 to our Jenkins server port 8080 for that we have to install and configure Apache web server.

Now, to install and configure Apache Web Server we need to follow the below steps.

Step 4: Installing and Configuring Apache

Install apache2 server

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)7

Next command is to enable module proxy

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)8

After the above, we need to run the next command to enable module proxy_http

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)9

Now configure Apache to proxy the request from port 80 to 8080 to Jenkins server for that you have to paste below configuration in the file. Don’t forget to change your EC2 hostname.

For open ‘jenkins.conf’ file type below command and paste above configuration *Don’t forget to change your EC2 hostname*

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)10

Step 5: Enabling jenkins.conf

Once the installation is complete we move on to the configuration of the files.We need to enable our new site that we’ve set up in Apache and reload the apache2 service.

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)11

Next command is to restart Apache service

Once, we run the above command you should be able to see the below message on the command interface.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)12

Step 6 : Unlock Jenkins

Access the Jenkins server by web browser then you will see below screen which means you have configured Jenkins properly.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)13

Then the next step is to unlock your Jenkins, for that we need to copy the administrator password from “/var/lib/jenkins/secrets/initialAdminPassword” file we could do that by executing the below command on your EC2 server to copy the password and unlock your Jenkins.

Then you will see the Jenkins dashboard.

Install Jenkins on Amazon EC2 (Ubuntu Server 14.04 LTS)14

Connect to AWS EC2 Instance via Putty/SSH

For connecting to your AWS EC2 instance, you can do it via Putty (for Windows user) or via SSH (for Linux/Mac user).

Assuming that you have created an AWS EC2 Instance and Key Pair (.pem file) is locally available for connection. If not then you can follow our below posts to complete these prerequisites.

First, find your EC2 instance Public DNS

Connect to AWS EC2 Instance via SSH_1

For Windows Users

Step1: Download Putty and putty gen from here

Step 2: Use putty gen to convert .PEM file to .PPK file. See Blog: How to convert .PEM file to .PPK file using putty gen

Step 3: Launch Putty and enter the host IP address and set the port to 22.

Connect to AWS EC2 Instance via SSH_2

Step 4: Navigate to Connection/SSH/Auth. Click “Browse” and select the .PPK file you exported from putty gen.

Connect to AWS EC2 Instance via SSH_3

For Mac/Linux users

Step 1: Use the chmod command to make sure your private key file isn’t publicly viewable. For example, if the name of your private key file is my-key-pair.pem, use the following command:

Step 2: Use the ssh command to connect to the instance. You’ll specify the private key (.pem) file and user_name@public_dns_name.
Example:- For Ubuntu, the username is ubuntu. For Fedora, the user name is either fedora or ec2-user.

You’ll see a response like the following.

Step 3: Enter yes and You’ll see a response like the following.

 

Create a new Instance (Ubuntu 14.04) on AWS EC2 and setup Security Groups

To create a new Instance (Ubuntu 14.04) on AWS EC2 and setup Security Groups is a very simple task and will take approx 5-10 minutes to complete. In this post, we will see how we can create a new Instance (Ubuntu 14.04) on AWS EC2.

Step 1: First login to your AWS console, select any region and click on EC2 link.

Create a new Instance (Ubuntu 14.04) on AWS EC2 1

Step 2: You will see the EC2 Dashboard screen. Now click on Launch Instance button.

Create a new Instance (Ubuntu 14.04) on AWS EC2 2

Step 3: Choose any Amazon Machine Image (AMI) which basically means the instance type you want to launch. In this post I have selected  “Ubuntu Server 14.04 LTS (HVM), SSD Volume Type” but you can select any Instance which you want to launch.

Create a new Instance (Ubuntu 14.04) on AWS EC2 3

Step 4: Next step is to choose an Instance type. In this post I have selected “t2.micro” you can select any Instance type as per your requirement and then click Next:Configure Instance Details button.

Create a new Instance (Ubuntu 14.04) on AWS EC2 4

Step 5: Configure Instance Details and click on Next: Add Storage button.

Create a new Instance (Ubuntu 14.04) on AWS EC2 5

Step 6: Configure Add Storage and then click on Next: Tag Instance button.

Create a new Instance (Ubuntu 14.04) on AWS EC2 6

Step 7: Tag Instance ( A tag consists of a case-sensitive key-value pair. For example, you could define a tag with key = Name and value = Webserver.) and click Next: Configure Security Group button.

Create a new Instance (Ubuntu 14.04) on AWS EC2 7

Step 8: Configure Security Group

A security group is a set of firewall rules that control the traffic for your instance. On this page, you can add rules to allow specific traffic to reach your instance.
For example, if you want to set up a web server and allow Internet traffic to reach your instance, add rules that allow unrestricted access to the HTTP and HTTPS ports.

So, I have added rule HTTP:80 and HTTPS:443. But you can configure Security Group according to your need and click on Review and Launch button.

Create a new Instance (Ubuntu 14.04) on AWS EC2 8

Step 9: Next step is to review your Instance details and then click on the Launch button.

Create a new Instance (Ubuntu 14.04) on AWS EC2 9

Step 10: Select an existing key pair or create a new key pair.

A key pair consists of a public key that AWS stores, and a private key file that you store. Together, they allow you to connect to your instance securely. For Windows AMIs, the private key file is required to obtain the password used to log into your instance. For Linux AMIs, the private key file allows you to securely SSH into your instance.

If you don’t have any key pairs. Please create a new key pair by selecting the Create a new key pair option above to continue.

Then you have to download the private key file (*.pem file) before you can continue. Store it in a secure and accessible location. You will not be able to download the file again after the instance has been created so please be careful and remember to download the same.

Create a new Instance (Ubuntu 14.04) on AWS EC2 10

After downloading the *.pem file click on Launch Instance button and wait for the instance to launch then you will see the below “Launch Status” screen that means your instance has been created successfully.

Create a new Instance (Ubuntu 14.04) on AWS EC2 11

How to Point a Domain to AWS EC2 Instance

In this post we will see how to point a domain to AWS EC2 instance
In the below example we have taken the Godaddy domain and an AWS EC2 instance.

Step -1 Register your custom domain on Godaddy2

Step -2 Open your EC2 instance on AWS

AWS Console

 

Step -3 Copy the Public IP XXX.XXX.XXX.XXX

Public IP
Step -4 Login to your Godaddy account and select the domain which you want to use on AWS and launch it.

Domain
Step -5 Go to DNS zone file and click on edit button and click on the add record
button and add A Record host and paste the IP address of the AWS EC2 and save the file

EC2 IP in Domain host

Step -6 Now open the AWS instance and go to Route 53 tab and click on the Create Hosted zone button

Route53
Step -7 Fill the Godaddy domain in the Domain Name field and save the zone
Fill godaddy domain in the domain name field and save the zone
Step -8 Click on the Checkbox against the created hosted zone and click on the Go to record Sets button

click on the Checkbox against the created hosted zone and click on the Go to record Sets button
Step -9 Click on the checkbox against the type NS and add the public DNS of ec2 instance and save the record set

Click on the checkbox against the type NS and add the public DNS of ec2 instance and save the record set
Step -10 Now click on the Create record set button and the CName from the drop down and fill the Elastick Beanstalk url like abc.elasticbeanstalk+.com and create the record set

Now click on the Create record set button and the CName from the drop down and fill the Elastick Beanstalk url like abc.elasticbeanstalk+.com and create the record set