DEV Community

Cover image for How to Install Jenkins on AWS EC2 and Deploy Changes to Apache Server
Buddhi Eashwarage
Buddhi Eashwarage

Posted on

How to Install Jenkins on AWS EC2 and Deploy Changes to Apache Server

Jenkins is a powerful open-source automation server that facilitates continuous integration and continuous deployment (CI/CD) in software development projects. In this article, we will guide you through the process of setting up Jenkins on an Amazon Web Services (AWS) Elastic Compute Cloud (EC2) instance and automating the deployment of changes to an Apache web server. This will enable you to streamline your development workflow and ensure smooth and efficient software deployment.

Prerequisites

Before we begin, make sure you have the following prerequisites in place:

  1. An AWS account with the necessary permissions to create EC2 instances and configure security groups.
  2. Familiarity with basic Linux commands and the AWS Management Console.

Step 1: Launch an EC2 Instance

  1. Log in to your AWS Management Console.
  2. Navigate to the EC2 dashboard.
  3. Click on "Launch Instance" to create a new EC2 instance.
  4. Select an Amazon Linux AMI or any other suitable Linux distribution of your choice.
  5. Choose an instance type based on your requirements. A t2.micro instance will suffice for this demonstration.
  6. Configure the instance details (e.g., network, subnet, IAM role).
  7. Add storage as needed for your Jenkins installation.
  8. Configure security groups to allow inbound traffic on port 8080 (Jenkins web interface) and port 22 (SSH access). Additionally, open port 80 if your Apache server is already running.

Step 2: Connect to Your EC2 Instance

  1. After the instance is launched, select it in the EC2 dashboard.
  2. Click on the "Connect" button to view connection options.
  3. Use SSH to connect to your EC2 instance using the provided Public DNS or IP address:
   ssh -i /path/to/your/key.pem ec2-user@your-ec2-public-dns
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Java

Jenkins requires Java to run. Amazon Linux usually comes with OpenJDK installed, but let's make sure we have Java installed:

  1. Update the package list:
   sudo yum update -y
Enter fullscreen mode Exit fullscreen mode
  1. Install Java:
   sudo yum install java-1.8.0-openjdk-devel -y
Enter fullscreen mode Exit fullscreen mode
  1. Check Java version:
   java -version
Enter fullscreen mode Exit fullscreen mode

Step 4: Install Jenkins

Now that Java is installed, let's proceed with installing Jenkins:

  1. Add the Jenkins repository to your system:
   sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
Enter fullscreen mode Exit fullscreen mode
  1. Import the Jenkins repository key:
   sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
Enter fullscreen mode Exit fullscreen mode
  1. Install Jenkins:
   sudo yum install jenkins -y
Enter fullscreen mode Exit fullscreen mode
  1. Start the Jenkins service:
   sudo service jenkins start
Enter fullscreen mode Exit fullscreen mode
  1. Enable Jenkins to start on system boot:
   sudo chkconfig jenkins on
Enter fullscreen mode Exit fullscreen mode

For the latest Jenkins installation instructions can be found through the Jenkins official installation guide

Step 5: Access Jenkins Web Interface

  1. Open a web browser and access Jenkins using your EC2 instance's public IP address or Public DNS on port 8080:
   http://your-ec2-public-ip:8080
Enter fullscreen mode Exit fullscreen mode
  1. You will be prompted to enter an initial administrator password. Retrieve it using the following command on your EC2 instance:
   sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Enter fullscreen mode Exit fullscreen mode
  1. Copy and paste the password into the Jenkins web interface.

  2. Follow the setup wizard to install suggested plugins and create an admin user.

Step 6: Install Required Plugins

To deploy changes to the Apache server, we will use the "Publish Over SSH" plugin. Here's how to install it:

  1. In the Jenkins web interface, click on "Jenkins" in the top-left corner to go back to the dashboard.
  2. Click on "Manage Jenkins" on the left-hand side.
  3. Select "Manage Plugins."
  4. Navigate to the "Available" tab and search for "Publish Over SSH."
  5. Check the box next to "Publish Over SSH" and click on "Install without restart."

Step 7: Configure Jenkins Credentials

Before we can deploy changes via SSH, Jenkins needs SSH credentials to access your Apache server:

  1. In the Jenkins web interface, click on "Jenkins" in the top-left corner to go back to the dashboard.
  2. Click on "Credentials" on the left-hand side.
  3. Under "Stores scoped to Jenkins," click on "Jenkins."
  4. Click on "Global credentials (unrestricted)."
  5. Click on "Add Credentials" on the left-hand side.
  6. Select "SSH Username with private key" from the "Kind" dropdown.
  7. Enter your SSH private key or provide the path to your private key file.
  8. Add a description and username for your credentials, and click "Save."

Step 8: Create a Jenkins Job for Deployment

Now, let's create a Jenkins job to automate the deployment of changes to the Apache server:

  1. In the Jenkins web interface, click on "New Item" on the left-hand side.
  2. Enter a name for your job (e.g., "Deploy to Apache Server") and select "Freestyle project."
  3. Click "OK" to create the job.
  4. Scroll down to the "Build" section and click on "Add build step."
  5. Select "Send files or execute commands over SSH" from the dropdown.
  6. Choose the SSH server you configured earlier.
  7. In the "Source files" field, enter the path to the files you want to deploy (e.g., ./dist/*).
  8. In the "Remove prefix" field, enter the relative path to the files from the Jenkins workspace, if applicable.
  9. In the "Remote directory" field, enter the path on the Apache server where you want to deploy the files (e.g., /var/www/html).
  10. Click "Save" to save the job configuration.

Step 9: Trigger the Deployment

Now that the job is set up, you can manually trigger the deployment:

  1. Go back to the Jenkins dashboard.
  2. Find your "Deploy to Apache Server" job and click on it.
  3. Click on "Build Now" to trigger the deployment.

Jenkins will now connect to your Apache server via SSH and deploy the changes specified in the job configuration.

Conclusion

Congratulations! You have successfully installed Jenkins on an AWS EC2 instance and set up a Jenkins job to automate the deployment of changes to your Apache web server. With Jenkins managing your CI/CD pipeline, you can enjoy faster and more reliable software deployments, enhancing your development workflow and overall productivity. Remember to monitor your Jenkins jobs regularly, and feel free to explore other Jenkins plugins and features to further enhance your automation process. Happy coding!

Top comments (0)