Jenkins Installation and Configuration on Ubuntu


Jenkins is a well-known CI environment. This technical note introduces how to install, config and run Jenkins based on Ubuntu System. All operations are validated on Ubuntu 16.04. For other Ubuntu release, please do related change if necessary.

Step 0: Install Java Environment

sudo apt install default-jre

Step 1: Install Jenkins

Firstly, add key and source list to system apt. Run below commands

wget -q -O - | apt-key add -
echo deb binary/ > /etc/apt/sources.list.d/jenkins.list
sudo apt-get update

Run below command to install Jenkins.

sudo apt-get install jenkins

Step 2: Config Jenkins

By default, Jenkins is running on port 8080. Access link http://[SERVER_IP]:8080 with your browser, and filled out user name, password and install plugins according to the page instructions.

Step 3: Add job

Now, Jenkins is running. You can add jobs according to your requirement.

TIPS: All parameters of a Jenkins Job should be in upper case. Those parameters can be passed to shell script in format of $PARA.

Step 4: Change Jenkins port to 80(optional)

Usually it will be convenient to access Jenkins via port (such as via a domain name). There are 2 ways.

The 1st method is implemented via configuration file of Jenkins, i.e. parameter HTTP_PORT in file /etc/default/jenkins. Restart Jenkins service to make the change work (sudo service jenkins restart)

Another way is redirect all traffic of port 80 to 8080 via iptables.

Following is the Iptables rule.

iptables -A INPUT -i eth0 -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -i eth0 -p tcp --dport 8080 -j ACCEPT
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 8080

Reload iptables after above changes.

sudo service iptables save
sudo service iptables restart

Step 5: Apache reserve proxy(optional)

It is easy to config Apache as a reserve proxy, and access Jenkins with a domain. After configuration, you can sethttpListenAddress= to forbidden external access, all traffic will be routed via Apache reserve proxy and safety improvment

Firstly, enable proxy modules of Apache.

a2enmod proxy
a2enmod proxy_http

Config virtual Host,and restart Apache (sudo service apache2 restart)

<VirtualHost *:443>
  ServerName jenkins.domain
  ServerAlias www.jenkins.domain

  SSLEngine On
  SSLCertificateFile    /etc/apache2/ssl/crt/jenkins.domain.crt
  SSLCertificateKeyFile /etc/apache2/ssl/key/jenkins.domain.key

  ProxyRequests     Off
  ProxyPass         /
  ProxyPassReverse  /
    Order allow,deny
    Allow from all
  ProxyPreserveHost on

About SSL configuration, please refer to this article.

Step 6: Install more plugins(Optinal)

Jenkins provides lots of useful plugins, you can install according to your requirements.