Installing Jenkins behind NGINX Reverse Proxy

by admin

In this post I’ll look at installing Jenkins on a Centos 7 box, and set it up behind an NGINX reverse proxy server. To start we need to grab the Jenkins repo, before we can download the Jenkins package:

[root@Jenkins01 ~]# wget -O /etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo

The downloaded repo file should look something like this:

[root@Jenkins01 ~]# cat /etc/yum.repos.d/jenkins.repo
[jenkins]
name=Jenkins
baseurl=http://pkg.jenkins.io/redhat
gpgcheck=1

Now we can run Yum update, then install the Jenkins package:

[root@Jenkins01 ~]# yum update
[root@Jenkins01 ~]# yum install jenkins

We also need to install a Java package:

[root@Jenkins01 ~]# yum install java-1.8.0-openjdk

Once that’s done, we can start the Jenkins service:

[root@Jenkins01 ~]# systemctl start jenkins
[root@Jenkins01 ~]# systemctl status jenkins
● jenkins.service - LSB: Jenkins Automation Server
   Loaded: loaded (/etc/rc.d/init.d/jenkins; bad; vendor preset: disabled)
   Active: active (running) since Wed 2018-01-24 09:00:09 UTC; 11s ago
     Docs: man:systemd-sysv-generator(8)
  Process: 18121 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/jenkins.service
           └─18140 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=true -DJENKINS_HOME=/var/lib/jenkins -jar /usr...


Jan 24 09:00:08 Jenkins01 systemd[1]: Starting LSB: Jenkins Automation Server...
Jan 24 09:00:08 Jenkins01 runuser[18126]: pam_unix(runuser:session): session opened for user jenkins by (uid=0)
Jan 24 09:00:09 Jenkins01 jenkins[18121]: Starting Jenkins [  OK  ]
Jan 24 09:00:09 Jenkins01 systemd[1]: Started LSB: Jenkins Automation Server.
[root@Jenkins01 ~]# systemctl enable jenkins
jenkins.service is not a native service, redirecting to /sbin/chkconfig.
Executing /sbin/chkconfig jenkins on

By default, Jenkins runs on port 8080. Rather than make that accessible, I will be using NGINX as a reverse proxy, to direct traffic sent to port 80, to port 8080. First NGINX needs to be installed:

[root@Jenkins01 ~]# yum install nginx

Once installed, the /etc/nginx/nginx.conf file needs to be edited to set up NGINX as a reverse proxy. The following line needs to be added:

proxy_pass  http://127.0.0.1:8080

This should be placed in the location section of the server configuration:

nginx-reverse-proxy

Once done, save the file, then start the NGINX service. We will need to return to the NGinX conf file later to make some further changes.

[root@Jenkins01 ~]# systemctl start nginx
[root@Jenkins01 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Wed 2018-01-24 09:29:43 UTC; 8s ago
  Process: 2794 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 2791 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 2790 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 2797 (nginx)
   CGroup: /system.slice/nginx.service
           ├─2797 nginx: master process /usr/sbin/nginx
           └─2798 nginx: worker process

At this point, we should be able to connect to the Jenkins installation using a browser, connecting to port 80:

unlock-jenkins-page

As a security measure, we have to ‘unlock’ the installation by retrieving the password from /var/lib/jenkins/secrets/initialAdminPassword. Grab the password, paste it in then click Continue. Next, we’re asked to select which plugins to install:

customize-jenkins-installation

For now, I’ve just selected the first option, which is to install the suggested plugins. Once they have installed you will be prompted to create the first admin user. Enter the necessary details, then click Save and Finish. You’ll then be taken to the Jenkins dashboard.

jenkins-dashboard

Earlier, I mentioned that we’d need to make some further changes to the nginx.conf file. After clicking the Manage Jenkins link, there will be an error message stating that the reverse proxy setup is broken:

jenkins-reverse-proxy-broken

We need to add some further configuration to the nginx.conf file to fix this issue. Clicking the More Info button takes you to a wiki page where the full config required is displayed. We need the information from the Running Jenkins behind Nginx page – there’s an example nginx configuration there. Update the nginx.conf file as required, then restart the nginx service:

# systemctl restart nginx

The error message on the Jenkins dashboard should now have disappeared. At this point we have a working Jenkins installation, accessible via port 80, which will form the basis for some future posts where I have a more detailed look at Jenkins.


Keep up to date with new posts on Buildvirtual.net - Follow us on Twitter:
Be Sociable, Share!

Leave a Comment

*

Previous post:

Next post: