Install and configure Jenkins on Ubuntu 16.04

Jenkins on Ubuntu 16.04

Introduction

Jenkins is an automation server written in Java, as a fork of the Hudson project. It helps to automate part of the software development process, with aids like continuous integration, but also by further empowering teams to implement the technical part of a Continuous Delivery. Jenkins supports many SCM (Source Control Management) software systems including Git, SVN, and Mercurial and provides hundreds of plugins to automate projects.
This tutorial explains how to install and use Jenkins on Ubuntu 16.04 using Apache as server.

Install Java

Jenkins requires Java, so, install OpenJDK7 on the server. First, install

python-software-properties

:

# apt install python-software-properties

Then you can add Java repository:

# add-apt-repository ppa:openjdk-r/ppa

Update Ubuntu repos and install OpenJDK:

# apt update
# apt install openjdk-7-jdk

Run

$ java -version

to verify that you have OpenJDK7 correctly installed.

Install Jenkins

Jenkins team provides a repository for Ubuntu. First, add it:

$ wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add -
# sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list'

Update repository and install Jenkins:

# apt update
# apt install jenkins

The installation process will perform the following tasks:

  • Setup Jenkins as a daemon launched on start
  • Create a Jenkins user to run this service
  • Direct console log output to the file
    /var/log/jenkins/jenkins.log
  • Populate /etc/default/jenkins with configuration parameters for the launch
  • Set Jenkins to listen on port 8080. Access this port with your browser to start configuration

Next, start Jenkins:

# systemctl start jenkins

Verify that it is running and listening on port 8080 with the following command:

# nestat -plntu | grep 8080

You will see a line like this:

tcp6       0      0 :::8080                 :::*                    LISTEN      122        54406       12370/java

Install and Configure Apache

Install Apache web server on your system:

# apt install apache2

Next, enable the

proxy

and

proxy_http modules

for configuring it as a front end server and reverse proxy for Jenkins. This is done by executing the following commands:

# a2enmod proxy
# a2enmod proxy_http

Now you must create a new Virtual Host file in the

/etc/apache2/sites-available/

directory.

# $EDITOR /etc/apache2/sites-available/jenkins.conf

Here, paste the following lines:

<Virtualhost *:80>
    ServerName        my.jenkins.id
    ProxyRequests     Off
    ProxyPreserveHost On
    AllowEncodedSlashes NoDecode
 
    <Proxy http://localhost:8080/*>
      Order deny,allow
      Allow from all
    </Proxy>
 
    ProxyPass         /  http://localhost:8080/ nocanon
    ProxyPassReverse  /  http://localhost:8080/
    ProxyPassReverse  /  http://my.jenkins.id/
</Virtualhost>

Save the file and activate the Jenkins Virtual Host with the

a2ensite

command.

# a2ensite jenkins

All these modifications will become effective after restarting Apache and Jenkins, so:

# systemctl restart apache2
# systemctl restart jenkins

Executing the following command, you can verify that Apache is using port 80 and Jenkins port 8080:

netstat -plntu | grep 80

Configure Jenkins

Open your browser, and go to URL

http://localhost:8080

You will see this page:
1
On your terminal, execute the following command:

# cat /var/lib/jenkins/secrets/initialAdminPassword

Copy the prompted password; in my case, this is:

f698b591187c40f298f24eea4847734a

Paste it on Jenkins configuration page, and click Continue
2
Click on Install suggested plugins. It will install various plugins automatically. This process will not take too long.
Next, we must create a new admin user:
3
Enter the required information, and then click on Save and finish. In the next page, click on Start using Jenkins and it will open the Jenkins Dashboard.

Configure security

Now that installation is finished, we need to configure Jenkins standard security settings.
On the Dashboard, click on Manage Jenkins -> Configure Global Security. You will see a page with different settings. Scroll and enable Matrix-based security. This will let you chose permissions for users. Add the user “admin”, and give him all permissions. To Anonymous, enable just the Read. Click on Save.

From now on, you can use your Jenkins based server for managing development jobs. Through the Dashboard you can add users, create new projects and configure them.