> For the complete documentation index, see [llms.txt](https://tkssharma-devops.gitbook.io/devops-training/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://tkssharma-devops.gitbook.io/devops-training/devops-01-continuous-integration/continuous-integration-and-continuous-delivery/ci-cd-using-jenkins-ci/jenkins-build-server-ec2.md).

# Jenkins Build on EC2

![](/files/-LjUZyL_bxKHNquTaEwe)

**Let's Setup the build environment by deploying Jenkins on EC2 as Build Server**

For our build environment we’ll launch an Amazon EC2 instance using the Amazon Linux 2 AMI, install and configure the required packages. Make sure that the security group you select for your instance allows traffic on ports TCP/22 and TCP/80 so you will be able to connect to you instance via SSH and access it via browser using its DNS.

**Install and configure Jenkins and required packages**

Connect to your EC2 instance using your private key:

```
$ ssh -i "path-to-key/key.pem" ec2-user@<instance-name>.<instance-region>.compute.amazonaws.com
```

First, let’s update the repositories and install Docker and Git:

```
$ sudo yum update -y
$ sudo yum install -y docker git
```

Add the Jenkins repository and install Jenkins from there:

```
$ sudo wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins.io/redhat/jenkins.repo
$ sudo rpm — import https://pkg.jenkins.io/redhat/jenkins.io.key
$ sudo yum install jenkins -y
$ sudo usermod -aG root jenkins # Add jenkins to the root group
```

To allow Jenkins to build our Docker images, we need to add the *jenkins* user to the *docker* group:

```
# Add Jenkins and current user to the Docker group
$ sudo usermod -aG docker $USER
$ sudo usermod -aG docker jenkins
```

Add the Jenkins and Docker services to start on boot and start both services:

```
# Add Jenkins and Docker to startup
$ sudo chkconfig jenkins on
$ sudo chkconfig docker on
$ sudo chkconfig --list
```

```
# Start Jenkins and Docker as a service
$ sudo service jenkins start
$ sudo service docker start
```

As Jenkins typically uses port TCP/8080, we’ll configure *iptables* routing rules to redirect the traffic:

```
$ sudo iptables -I INPUT 1 -p tcp --dport 8080 -j ACCEPT
$ sudo iptables -I INPUT 1 -p tcp --dport 80 -j ACCEPT
```

```
$ sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080
```

I’m not covering the usage of SSL certificates, to use HTTPS you need to include rules to redirect traffic for port 443:

```
$ sudo iptables -I INPUT 1 -p tcp --dport 8443 -j ACCEPT
$ sudo iptables -I INPUT 1 -p tcp --dport 443 -j ACCEPT
```

```
$ sudo iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 443 -j REDIRECT --to-port 8443
```

You can find more about SSL certificates on this article from [Paul Lessing](https://medium.com/@P_Lessing):[**Single-Page Apps on AWS, Part 1: Hosting a Website on S3**\
*How to set up S3, CloudFront and CloudFlare to host a static website on S3 without a dedicated server.*&#x6D;edium.com](https://medium.com/@P_Lessing/single-page-apps-on-aws-part-1-hosting-a-website-on-s3-3c9871f126)

At this point you should be able to see the Jenkins home page using the public DNS name of your EC2 instance(e.g. ec2–\<ec2-public-ip-address>.compute-1.amazonaws.com):Jenkins Setup — Administrator password

![](https://cdn-images-1.medium.com/max/1600/1*-tuSyBfeKPx8Ng8aC2YGXA.jpeg)

Get the password from the indicated file:

```
$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword
```

Install the recommended plugins and create an Admin account in the following steps.

At this point you should be able to login an see the following page:Jenkins home page

![](https://cdn-images-1.medium.com/max/1600/1*OrY8i6HDx1bVknc7Zkfr-g.png)

**Install Jenkins Plugins**

At the Jenkins home page on the left menu select *Manage Jenkins -> Manage Plugins* select the tab *Available* and search for the following plugins:

* [Blue Ocean](https://jenkins.io/projects/blueocean/) - New Jenkins UI
* [Pipeline AWS](https://plugins.jenkins.io/pipeline-aws) - AWS Integration
