In my last project, I needed to use Jenkins. Jenkins is an automation engine which support continuous integration and automated testing. In this article we will see how can we install it on ubuntu droplet in digitalocean.


(1) Create a new droplet which is based on ubuntu 16.04.1 x64 image.

To follow the tutorial you should need the credentials (password or private key) of the root user in the machine and its domain name. In this tutorial, the domain of the host which serve Jenkins is my-jenkins.com so to follow this tutorial replace this value to your domain name.

If your machine does not have a domain, you can add a new mappings of IP address to my-jenkins.com in hosts file in your local machine. This file is usually located in C:\Windows\System32\drivers\etc\hosts in Windows or /etc/hosts in Linux:

127.0.0.1       localhost
100.101.50.51   my-jenkins.com

(2) Connect via ssh to the machine.

$ ssh my-jenkins.com
login as: root
Authenticating with public key "rsa-key_2016-08-01-jenkins"
Welcome to Ubuntu 16.04.1 LTS (GNU/Linux 4.4.0-34-generic x86_64)
....
root@jenkins:~#

(3) Add jenkins package repositories as sources of apt-get

root@jenkins:~# curl --silent http://pkg.jenkins-ci.org/debian/jenkins-ci.org.key | sudo apt-key add -
OK
root@jenkins:~# echo "deb http://pkg.jenkins-ci.org/debian binary/" >> /etc/apt/sources.list.d/jenkins.list
root@jenkins:~# sudo apt-get update
...
Fetched 825 kB in 1s (785 kB/s)
Reading package lists... Done

(4) Update the system

root@jenkins:~# sudo apt-get update
...
Fetched 825 kB in 1s (785 kB/s)
Reading package lists... Done

(5) We will use apache2 to serve jenkins via proxy_http apache module. First, Install apache2 and jenkins.

root@jenkins:~# sudo apt-get install jenkins apache2
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
...
Suggested packages:
...
The following NEW packages will be installed:
...
Do you want to continue? [Y/n] Y
...

(6) Enable proxy and proxy_http apache modules. This is straightforward task since apache2 package offers the a2enmod and a2dismod commands (a2enmod allows to enable apache module, while a2dismod allows to disable apache module).

root@jenkins:~# sudo a2enmod proxy
Enabling module proxy.
To activate the new configuration, you need to run:
    service apache2 restart
root@jenkins:~# sudo a2enmod proxy_http
Enabling module proxy_http.
To activate the new configuration, you need to run:
    service apache2 restart

(7) Create a new jenkins.conf file at /etc/apache2/sites-available/. In this file we will define a new a site (virtual host) to our Jenkins web application. We will use nano text editor, but you can use any text editor you are familiar with:

root@jenkins:~# nano /etc/apache2/sites-available/jenkins.conf

Now, add the following content to the file. Replace the my-jenkins.com with your domain name.

<VirtualHost *:80>
    ServerName        my-jenkins.com
    ProxyRequests     Off
    ProxyPreserveHost On
    ProxyPass         / http://localhost:8080/
    <Proxy *>
        Order deny,allow
        Allow from all
    </Proxy>
</VirtualHost>

(8) Enable Jenkins web application. It is straightforward task as apache2 package offers the a2ensite and a2dissite commands (a2ensite allows to enable a site, while a2dissite allows to disable a site)

root@jenkins:~# a2ensite jenkins
Enabling site jenkins.
To activate the new configuration, you need to run:
    service apache2 reload

(9) Now, we reload apache to reflect the changes we made

sudo service apache2 reload

(10) Now, we open http://my-jenkins.com in our browser and the following page should appear:

Unlock Jenkins page - The first page after installing jenkins package in ubuntu

Now, you should fill the initial password from /var/lib/jenkins/secrets/initialAdminPassword and press Continue:

root@jenkins:~# cat /var/lib/jenkins/secrets/initialAdminPassword
81345...

(11) The Customize Jenkins should appear. In this page, you can select which plugins to install in order to extend Jenkins with additional fetuares.

Customize Jenkins Page - Install Plugins

You can choose Install Suggested Plugins or Select plugins to install. When done, Jenkins will start to install plugins:

Install Jenkins Plugins Status Page


(12) The Create First Admin User page should appear

Install Jenkins Plugins Status Page

Fill the details and click in Save and Finish.


Congratulations, Jenkins is ready to use! Now, you can build,deploy and automate your project.

Jenkins is ready to use