How to Install Jenkins Automation Server with Apache on Ubuntu 18.04
This tutorial exists for these OS versions
On this page
Jenkins is an automation server forked from the Hudson project. Jenkins is a server-based application running in a Java servlet container, it has support for many SCM (Source Control Management) software systems including Git, SVN, and Mercurial. Jenkins provides hundreds of plugins to automate your project. Jenkins created by Kohsuke Kawaguchi, first released in 2011 under MIT License, and it's a free software.
In this tutorial, I will show you how to install the latest stable Jenkins version on Ubuntu Server 18.04 LTS (Bionic Beaver). We will run Jenkins on our own domain name, and we will install and configure Jenkins to run under the Apache web server reverse proxy.
- Ubuntu 18.04
- Root privileges
What we will do?
- Install Java
- Install Jenkins
- Install and Configure Apache2 as a Reverse Proxy for Jenkins
- Configure UFW Firewall
- Configure Jenkins
- Jenkins Security
Step 1 - Install Java
Jenkins is a Java-based application, so we need to install Java OpenJDK on the server. In this step, we will install Java 8 from a PPA repository which we will add first.
Install software-properties-common packages, then add the java OpenJDK PPA repository.
sudo apt install software-properties-common apt-transport-https -y
sudo add-apt-repository ppa:openjdk-r/ppa -y
Now install the Java 8 using apt command.
sudo apt install openjdk-8-jdk -y
When the installation is complete, check the java version installed on the system.
And you will get the Java OpenJDK 1.8 is now installed on the Ubuntu 18.04 system.
- If you have multiple java version on your system, change the default java version using the command below.
sudo update-alternatives --config java
Step 2 - Install Jenkins
Jenkins provides an Ubuntu repository for the installation packages and we will install Jenkins from this repository.
Add Jenkins key and repository to the system with the command below.
wget -q -O - https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
echo 'deb https://pkg.jenkins.io/debian-stable binary/' | tee -a /etc/apt/sources.list
Now update the repository and install Jenkins.
sudo apt update
sudo apt install jenkins -y
When the installation is complete, start the Jenkins service and add it to the boot time.
systemctl start jenkins
systemctl enable jenkins
Jenkins is now up and running on Ubuntu 18.04 server, running on the default port '8080'. Check it using netstat as below.
And you will get the result as below.
Step 3 - Install and Configure Apache2 as a Reverse proxy for Jenkins
In this tutorial we will run Jenkins behind an Apache web server, we will configure apache as the reverse proxy for Jenkins.
First we will install Apache and enable some require modules, and then we will create the virtual host file with domain name jenkins.hakase-labs.io for Jenkins. Please use your own domain name here and replace it in all config files wherever it appears.
Install apache2 web server from Ubuntu repository.
sudo apt install apache2 -y
When the installation is complete, enable the proxy and proxy_http modules so we can configure apache as frontend server/reverse proxy for Jenkins.
Next, create a new virtual host file for Jenkins in the sites-available directory.
Paste virtual host configuration below.
<Virtualhost *:80> ServerName jenkins.hakase-labs.io 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://jenkins.hakase-labs.io/ </Virtualhost>
Save and exit, then activate the Jenkins virtual host with the a2ensite command.
Restart Apache and Jenkins services.
systemctl restart apache2
systemctl restart jenkins
The apache2 installation and configuration as a reverse proxy for Jenkins has been completed.
Step 4 - Configure UFW Firewall
Before enabling the UFW firewall on the Ubuntu server, we need to add the basic services port such as SSH, HTTP, and HTTPS.
Add SSH, HTTP, and HTTPS services to the ufw firewall.
ufw allow ssh
ufw allow http
ufw allow https
Now start and enable the ufw firewall.
type 'y' and press Enter.
The UFW firewall is now enabled, and the HTTP port has been added.
Step 5 - Configure Jenkins
Jenkins is running on the domain name 'http://jenkins.hakase-labs.io'. Open your web browser and type in the URL.
You will get the screen that requests you to enter the initial admin password. A password has been generated by Jenkins already, so we just need to show and copy the results to the password box.
Show initial admin password Jenkins with the cat command.
Paste the results to the screen and click 'Continue'.
Now we should install some plugins in Jenkins to get a good foundation for later use. Choose 'Install Suggested Plugins', click on it.
Jenkins plugins installations in progress.
After the plugins installation is complete, we need to create a new admin password. Type in your admin username, password, email etc. and click on 'Save and Continue'.
For the instance configuration, type the Jenkins domain name 'http://jenkins.hakase-labs.io' and click 'Save and Finish' button.
Now click the 'Start using Jenkins' button.
And you will be redirected to the Jenkins admin dashboard.
Jenkins installation and Configuration has been completed successfully
Step 6 - Jenkins Security
From the Jenkins admin dashboard, we need to configure the standard security settings for Jenkins, click on 'Manage Jenkins' and then 'Configure Global Security'.
Jenkins provides several authorization methods in the 'Access Control' section. We will be using the 'Matrix-based Security', so we can control all user privileges.
Add the 'hakase' user at the box 'User/Group' and click add.
Give the 'hakase' user all privileges by checking all options, and click 'Save' button.
You will be redirected to the dashboard, and if there is login option, just type your admin user and password.
Step 7 - Testing
In this section, we want to create a simple job for the Jenkins server. We will create a simple job for testing Jenkins and to find out the server load with the top command.
From the Jenkins admin dashboard, click 'Create New Job'.
Type the job name. We'll use 'Checking System' here, select 'Freestyle Project' and click 'OK'.
Go to the 'Build' tab. On the 'Add build step', select the option 'Execute shell'.
Type in the command below into the box.
top -b -n 1 | head -n 5
Now you are on the job page of the job 'Project checking system'. Click 'Build Now' to execute the job 'checking system'.
After the job has been executed, you will see the 'Build History', click on the first job to see the results.
Here are the results from the job executed by Jenkins.
Jenkins automation tool installation and configuration with Apache2 as a reverse proxy on Ubuntu 18.04 has completed successfully.