How to Install Jenkins Self Hosted on Ubuntu 20.04

How to Install Jenkins Self Hosted on Ubuntu 20.04. In this post we will explain what Jenkins server is with its features and then we will proceed to installations guide. 

What is Jenkins Server?

Jenkins is a free, open-source automation tool written in Java that helps in the building, testing, deploying and monitoring of software projects. It is compatible with operating systems like Windows, macOS and integrates various deployment technologies on cloud or can use a Docker container in it. The tool supports 1500+ plugins and other features that aid in the Continuous Integration and continuous delivery (CI/CD) process.

Most companies prefer Jenkins over other DevOps tools as it helps accelerate the software development process and delivers quick results. It is a flexible, easy to install and has configuration automation tool that helps developers and operational teams to collaborate and enable smooth functioning. Developers can track repeated tasks, discover and fix errors at at an early stage so it is easy to debug.

It is flexible in creating the jobs and supports different types of source code repositories like SVN or Git. Jenkins server runs an active and thriving community for customer queries and other support services.

In this post, we will show you how to install Jenkins Self Hosted on Ubuntu 20.04.

Getting Started with Jenkins Server

Before starting, you will need to install the Java and other required dependencies to your server. You can install all of them by running the following command:

				
					apt-get install apt-transport-https gnupg2 curl wget software-properties-common ca-certificates -y
apt-get install default-jdk -y
				
			

Once all the packages are installed, run the following command to verify the Java version:

				
					java --version
				
			

You will get the Java version in the following output:

				
					openjdk 11.0.13 2021-10-19
OpenJDK Runtime Environment (build 11.0.13+8-Ubuntu-0ubuntu1.20.04)
OpenJDK 64-Bit Server VM (build 11.0.13+8-Ubuntu-0ubuntu1.20.04, mixed mode, sharing)
				
			

Install Jenkins on Ubuntu 20.04

By default, Jenkins is not available in the Ubuntu 20.04 default repository. So you will need to add the Jenkins repository to the APT.

First, download and add the Jenkins GPG key with the following command:

				
					curl -fsSL https://pkg.jenkins.io/debian/jenkins.io.key | tee  /usr/share/keyrings/jenkins-keyring.asc
				
			

Next, add the Jenkins repository with the following command:

				
					echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] https://pkg.jenkins.io/debian binary/ | tee /etc/apt/sources.list.d/jenkins.list 
				
			

Once the Repository is added, update the repository and install the Jenkins with the following command:

				
					apt-get update -y
apt-get install jenkins -y
				
			

After installing Jenkins, you can verify the status of the Jenkins service using the following command:

				
					systemctl status jenkins
				
			

You will get the following output:

				
					● jenkins.service - LSB: Start Jenkins at boot time
     Loaded: loaded (/etc/init.d/jenkins; generated)
     Active: active (running) since Thu 2022-02-03 11:43:25 UTC; 26s ago
       Docs: man:systemd-sysv-generator(8)
      Tasks: 40 (limit: 2353)
     Memory: 325.3M
     CGroup: /system.slice/jenkins.service
             └─14726 /usr/bin/java -Djava.awt.headless=true -jar /usr/share/jenkins/jenkins.war --webroot=/var/cache/jenkins/war --httpPort=8>

Feb 03 11:43:24 ubuntu2004 systemd[1]: Starting LSB: Start Jenkins at boot time...
Feb 03 11:43:24 ubuntu2004 jenkins[14695]:  * Starting Jenkins Automation Server jenkins
Feb 03 11:43:24 ubuntu2004 jenkins[14695]: Correct java version found
Feb 03 11:43:25 ubuntu2004 systemd[1]: Started LSB: Start Jenkins at boot time.

				
			

By default, Jenkins listens on port 8080. You can check it using the following command:

				
					ss -antpl | grep 8080
				
			

You will get the following output:

				
					LISTEN    0         50                       *:8080                   *:*        users:(("java",pid=14726,fd=116))
				
			

Configure Nginx as a Reverse Proxy for Jenkins

Next part of the guide of how to install Jenkins Self Hosted on Ubuntu 20.04 we recommend  configuring Nginx as a reverse proxy for Jenkins. So you can access it through the port 80.

First, install the Nginx package by running the following command:

				
					apt-get install nginx -y
				
			

Once the Nginx package is installed, create an Nginx virtual host configuration file using the following command:

				
					nano /etc/nginx/conf.d/jenkins.conf
				
			

Add the following lines:

				
					upstream jenkins {
  keepalive 32; # keepalive connections
  server 127.0.0.1:8080; # jenkins ip and port
}

# Required for Jenkins websocket agents
map $http_upgrade $connection_upgrade {
  default upgrade;
  '' close;
}

server {
  listen          80;       # Listen on port 80 for IPv4 requests

  server_name     jenkins.example.com;  # replace 'jenkins.example.com' with your server domain name

  # this is the jenkins web root directory
  # (mentioned in the /etc/default/jenkins file)
  root            /var/run/jenkins/war/;

  access_log      /var/log/nginx/jenkins.access.log;
  error_log       /var/log/nginx/jenkins.error.log;

  # pass through headers from Jenkins that Nginx considers invalid
  ignore_invalid_headers off;

  location ~ "^/static/[0-9a-fA-F]{8}\/(.*)$" {
    # rewrite all static files into requests to the root
    # E.g /static/12345678/css/something.css will become /css/something.css
    rewrite "^/static/[0-9a-fA-F]{8}\/(.*)" /$1 last;
  }

  location /userContent {
    # have nginx handle all the static requests to userContent folder
    # note : This is the $JENKINS_HOME dir
    root /var/lib/jenkins/;
    if (!-f $request_filename){
      # this file does not exist, might be a directory or a /**view** url
      rewrite (.*) /$1 last;
      break;
    }
    sendfile on;
  }

  location / {
      sendfile off;
      proxy_pass         http://jenkins;
      proxy_redirect     default;
      proxy_http_version 1.1;

      # Required for Jenkins websocket agents
      proxy_set_header   Connection        $connection_upgrade;
      proxy_set_header   Upgrade           $http_upgrade;

      proxy_set_header   Host              $host;
      proxy_set_header   X-Real-IP         $remote_addr;
      proxy_set_header   X-Forwarded-For   $proxy_add_x_forwarded_for;
      proxy_set_header   X-Forwarded-Proto $scheme;
      proxy_max_temp_file_size 0;

      #this is the maximum upload size
      client_max_body_size       10m;
      client_body_buffer_size    128k;

      proxy_connect_timeout      90;
      proxy_send_timeout         90;
      proxy_read_timeout         90;
      proxy_buffering            off;
      proxy_request_buffering    off; # Required for HTTP CLI commands
      proxy_set_header Connection ""; # Clear for keepalive
  }

}

				
			

Save and close the file then verify the Nginx for any configuration error using the following command:

				
					nginx -t
				
			

If everything is fine, you will get the following output:

				
					nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

				
			

Finally, restart the Nginx service to apply the changes:

				
					systemctl restart nginx
				
			

You can also check the status of the Nginx using the following command:

				
					systemctl status nginx
				
			

You will get the following output:

				
					● nginx.service - A high performance web server and a reverse proxy server
     Loaded: loaded (/lib/systemd/system/nginx.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2022-02-12 06:27:47 UTC; 4s ago
       Docs: man:nginx(8)
    Process: 7373 ExecStartPre=/usr/sbin/nginx -t -q -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
    Process: 7374 ExecStart=/usr/sbin/nginx -g daemon on; master_process on; (code=exited, status=0/SUCCESS)
   Main PID: 7375 (nginx)
      Tasks: 2 (limit: 2341)
     Memory: 2.5M
        CPU: 45ms
     CGroup: /system.slice/nginx.service
             ├─7375 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
             └─7376 nginx: worker process

Feb 12 06:27:47 ubuntu2004 systemd[1]: Starting A high performance web server and a reverse proxy server...
Feb 12 06:27:47 ubuntu2004 systemd[1]: nginx.service: Failed to parse PID from file /run/nginx.pid: Invalid argument
Feb 12 06:27:47 ubuntu2004 systemd[1]: Started A high performance web server and a reverse proxy server.
				
			

In order to Jenkins work with Nginx you need to make Jenkins to listen on localhost. You can do it by editing the Jenkins configuration file:

				
					nano /etc/default/jenkins
				
			

Find the following line:

				
					JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT"
				
			

Replaced it with the following line:

				
					JENKINS_ARGS="--webroot=/var/cache/$NAME/war --httpPort=$HTTP_PORT --httpListenAddress=127.0.0.1"
				
			

Save and close the file then restart the Jenkins service to apply the changes:

				
					systemctl restart jenkins
				
			

Access Jenkins Web Interface

At this point of our guide of how to install Jenkins Self Hosted on Ubuntu 20.04, the Nginx is installed and configured as a reverse proxy for Jenkins. You can now access the Jenkins web interface using the URL http://jenkins.example.com. You should see the Jenkins initial password screen:

Now, open your terminal and run the following command to fetch the Jenkins password:

				
					cat /var/lib/jenkins/secrets/initialAdminPassword
				
			

You will get the Jenkins password in the following output:

				
					148309c551cf4718a3cb051b80e25452
				
			

Copy and paste the above password to the web installation screen and click on the Continue button. You should see the Customize Jenkins screen:

Click on the Install suggested plugins. You should see the Jenkins admin user creation screen:

Provide your Jenkins admin username, password and click on the Save and Continue button. You should see the Jenkins Instance Configuration screen:

Provide your Jenkins URL and click on the Save and Finish button. You should see the following screen:

Now, click on the Start using Jenkins. You should see the Jenkins dashboard in the following screen:

How to Install Jenkins Self Hosted on Ubuntu 20.04 Conclusion

In the above post, we explained how to install Jenkins on Ubuntu 20.04. We also explained how to configure Nginx as a reverse proxy for Jenkins. You can now start creating your first project using the Jenkins dashboard.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x