Install and configure Jenkins with Nginx as a reverse proxy on CentOS 7

jenkinsIn this article we will install and configure Jenkins with Nginx as a reverse proxy on a CentOS 7 VPS.

Jenkins is a leading open source automation server built with Java that monitors executions of repeated jobs, such as building a software project or jobs run by cron. With Jenkins, organizations can accelerate the software development process through automation. It manages and controls development life-cycle processes of all kinds, including build, document, test, package, stage, deployment, static analysis and many more.

For Ubuntu use the tutorial below:
How to Install Jenkins on Ubuntu 16.04

Builds can be started by various means, including being triggered by commit in a version control system, scheduling via a cron-like mechanism, building when other builds have completed, and by requesting a specific build URL.

REQUIREMENTS

We will be using our SSD 1 Linux VPS hosting plan for this tutorial.

Log in to your server via SSH:

# ssh root@server_ip

Before starting, enter the below command to check whether you have the proper version of CentOS installed on your machine:

# cat /etc/redhat-release

It should give you the underneath output:

CentOS Linux release 7.2.1511 (Core)

UPDATE THE SYSTEM

Make sure your server is fully up to date:

# yum update

INSTALL JAVA AND NGINX

Your next step is to install Nginx along some needed dependencies and the nano text editor so you can edit some config files. Of course, you can use your favorite text editor.

Install the official Nginx repository for CentOS 7 and then install Nginx, nano etc… Use the below commands to do that:

# rpm -Uvh http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0.el7.ngx.noarch.rpm

# yum install nginx wget nano

Start Nginx and enable it to start on boot:

# systemctl start nginx

# systemctl enable nginx

Now create a host directive for the domain from which you will access Jenkins. Open a file, let’s say called ‘your_domain.conf’ in the ‘/etc/nginx/conf.d/’ directory:

# nano /etc/nginx/conf.d/your_domain.conf

Paste the following:

upstream jenkins {
    server 127.0.0.1:8080;
}

server {
    listen      80 default;
    server_name your_jenkins_site.com;

    access_log  /var/log/nginx/jenkins.access.log;
    error_log   /var/log/nginx/jenkins.error.log;

    proxy_buffers 16 64k;
    proxy_buffer_size 128k;

    location / {
        proxy_pass  http://jenkins;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;

        proxy_set_header    Host            $host;
        proxy_set_header    X-Real-IP       $remote_addr;
        proxy_set_header    X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto https;
    }

}

Replace the ‘your_jenkins_site.com’ value with your own domain, save and close the file.

Test if the Nginx configuration is OK:

# nginx -t

If everything is OK, restart Nginx for the changes to take effect:

# service nginx restart

Since Jenkins is built with Java, let’s install it with the yum package manager:

# yum install java

You can check the installed Java version:

# java -version

openjdk version "1.8.0_71"
OpenJDK Runtime Environment (build 1.8.0_71-b15)
OpenJDK 64-Bit Server VM (build 25.71-b15, mixed mode)

INSTALL JENKINS

Download the Jenkins repo and install Jenkins with the following commands:

# wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo

# rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key

# yum install jenkins

Start the Jenkins server and enable it to start on boot with:

# service jenkins start

# systemctl enable jenkins

Congratulations, you have successfully installed Jenkins on your CentOS 7 VPS. You can now open your favorite web browser and access Jenkins using the domain you configured in the Nginx conf file.

However, the installation is insecure and allows anyone on the network to launch processes on your behalf. Therefore enable authentication to discourage misuse. Go to Manage Jenkins (in the left menu).

Click ‘Setup Security’ on the page loaded. Enable security by checking the box next to ‘Enable security’.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

Configure Jenkins to use it’s own user database and disable sign ups:

Navigate to Matrix-based security under Authorization and check the box next to it. Then make sure that Anonymous has only the Read right under the View group to prevent Jenkins from crashing.

Click save at the bottom of the page. After that, you’ll see a sign up form.

Enter a username, password, name and email. Use a lower case username to avoid confusion because Jenkins is assuming a lower case username will be used. After signing up, you will be the administrator of this fresh Jenkins install.

Once everything is up and running, it is up to you to create and schedule your first job, install plugins etc…

Of course you don’t have to do any of this if you use one of our Hosted Jenkins  services, in which case you can simply ask our expert Linux admins to install Jenkins for you. They are available 24×7 and will take care of your request immediately.

PS. If you liked this post please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks.

4 thoughts on “Install and configure Jenkins with Nginx as a reverse proxy on CentOS 7”

  1. Thanks for this, excellent. One thing I didn’t get- what’s the purpose of installing php-fpm if Jenkins runs on Java?

    Reply
  2. Hi,

    Thanks for the post. I followed the instructions but got bad gateway.
    my configuraion is:
    1. nginx machine with ip 192.168.20.56
    2. jenkins machine with ip 192.168.40.96

    they both can reach each other by ip and by name. i changed the upstream ip to match the jenkins ip
    is there something else i need to do?

    thanks

    Reply
    • Hello,

      Make sure that Jenkins is up and running and accessible at 192.168.40.96:8080. Also, check the log files for errors related to your configuration.

      Thanks.

      Reply

Leave a Comment