Configuring SSL on Jenkins using Let's Encrypt and NGINX reverse proxy!
28 July 2016 on Let's-Encrypt and Jenkins. 5 minutes
Let’s Encrypt Certificate Authority (CA) provides free TLS/SSL certificates to enable encrypted HTTPS on web servers. This can be used to obtain a free SSL certificate, which can be installed manually on our Jenkins installation.
Here, We can configure free SSL certificate for a Jenkins installation on Ubuntu 16.04 and will configure to automatically renew SSL certificate.
###Installing Let’s Encrypt Client in the server
switch to root user and Clone the Let’s Encrypt Client repo from the github (Install git apt-get install git
)
git clone https://github.com/letsencrypt/letsencrypt /opt/letsencrypt
This will clone the repo to /opt/letsencrypt
folder.
###Installing NGINX server
If you haven’t installed NGINX on the system, you can install it with this command, We need this to setup reverse proxy for Jenkins.
apt-get install nginx
###Allowing Let’s Encrypt to Valdate the domain and server
Let’s Encrypt has many ways to obtain SSL certificates with the help of various plugins. Here we use the webroot plugin to get the certificate. This plugin will place a specific file in /.well-known directory in our configured server to validate the domain and server. So, We have to make sure the file is accessible by the Let’s Encrypt server for validation. For that we have to update the NGINX configuation. The default site configuaration file is located at /etc/nginx/sites-available/default
. We can edit that with vim/nano
vim /etc/nginx/sites-available/default
and add the below location block to the configuration, Don’t foget to update the server_name
name with your domain.
Now verify the configuration file and reload NGINX to update the configuration we changed.
sudo nginx -t && sudo nginx -s reload
###Creating a Template for the Let’s Encrypt
We have to create a template file with the values that Let’s Encrypt needed while generating the cerificate. Otherwise we have to provide these values as parameters for the Let’s Encrypt command line.
Create a file /etc/letsencrypt/configs/your-domain.conf
Don’t forget to replace your-domain
with your fully qualified domain name. Copy the following contents and set the correct values in the domains (fully qualified domain name), rsa‑key‑size and email fields.
###Requesting the Certificate
Now we can request the Let’s Encrypt to generate the certificate for our domain. Go to the folder where we cloned the Let’s Encrypt client
cd /opt/letsencrypt
Then Sent the request to generate the certiicates.
./letsencrypt-auto --config /etc/letsencrypt/configs/your-domain.conf certonly
You must agree to the Let’s Encrypt Subscribe Agreement. Select Agree for the following Question.
If everything fine, this will generate the two certificates files and stores in /etc/letsencrypt/live/your-domain
as fullchain.pem and privkey.pem.
###Installing Jenkins (Skip this if already installed)
Now we have to install Jenkins in the server. run the following commands to install Jenkins.
wget -q -O - https://jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
sudo sh -c 'echo deb http://pkg.jenkins-ci.org/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'
sudo apt-get update
sudo apt-get install jenkins
###Configuring Jenkins behind the NGINX reverse proxy
Update the NGINX site configuration file to work as a reverse proxy.
vim /etc/nginx/sites-available/default
Copy the following content and update accordingly. Don’t forget to update the domain names and certificat path with your correct values.
Verify the configuration file and restart NGINX to load the new certificates:
nginx -t && sudo nginx -s restart
###Configuring auto Renewal of Let’s Encrypt Certificates
Let’s Encrypt certificates are valid only for 3months (90 days), So we have to renew the cerificates whith in three months. We can configure this to auto renew using a cron job.
create an script that can be configured in monthly cron job. Copy the following and create a file /etc/cron.monthly/renew-ssl-your-domain.sh
Make sure the file has enough permission. Also, make sure the folder /var/log/letsencrypt/
exists to save the log.