Install Jenkins

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.

---

To follow the tutorial you should need ubuntu 16.04.1 x64 macine with 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:WindowsSystem32driversetchosts in Windows or /etc/hosts in Linux:

 127.0.0.1       localhost
 100.101.50.51   my-jenkins.com

Step 1 - 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:~#

Step 2 - 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

Step 3 - Update the system

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

Step 4 - Install apache2

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
 ...

Step 5 - Enable proxy modules

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

Step 6 - add jenkins site

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>