Install Jenkins, Nginx On Ubuntu
Step 1 — Installing Jenkins
The version of Jenkins included with the default Ubuntu packages is often behind the latest available version from the project itself. To ensure you have the latest fixes and features, use the project-maintained packages to install Jenkins.
First, add the repository key to the system:
wget -q -O — https://pkg.jenkins.io/debian-stable/jenkins.io.key | sudo apt-key add -
Next, let’s append the Debian package repository address to the server’s sources.list
sudo sh -c ‘echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list’
After both commands have been entered, we’ll run an update so that apt will use the new repository.
sudo apt update
Finally, we’ll install Jenkins and its dependencies.
sudo apt install jenkins
Step 2 — Starting Jenkins
Let’s start Jenkins by using systemctl:
sudo systemctl start jenkins
Since systemctl doesn’t display status output, we’ll use the status command to verify that Jenkins started successfully:
sudo systemctl status jenkins
If everything went well, the beginning of the status output shows that the service is active and configured to start at boot:
Output
● jenkins.service — LSB: Start Jenkins at boot time
Loaded: loaded (/etc/init.d/jenkins; generated)
Active: active (exited) since Fri 2020–06–05 21:21:46 UTC; 45s ago
Docs: man:systemd-sysv-generator(8)
Tasks: 0 (limit: 1137)
CGroup: /system.slice/jenkins.service
Step 3 — Setting Up Jenkins
To set up your installation, visit Jenkins on its default port, 8080, using your server domain name or IP address: http://your_server_ip_or_domain:8080
You should receive the Unlock Jenkins screen, which displays the location of the initial password:
In the terminal window, use the cat command to display the password:
sudo cat /var/lib/jenkins/secrets/initialAdminPassword
Copy the 32-character alphanumeric password from the terminal and paste it into the Administrator password field, then click Continue.
The next screen presents the option of installing suggested plugins or selecting specific plugins:
We’ll click the Install suggested plugins option, which will immediately begin the installation process.
When the installation is complete, you’ll be prompted to set up the first administrative user. It’s possible to skip this step and continue as admin using the initial password we used above, but we’ll take a moment to create the user.
You’ll receive an Instance Configuration page that will ask you to confirm the preferred URL for your Jenkins instance. Confirm either the domain name for your server or your server’s IP address:
After confirming the appropriate information, click Save and Finish. You’ll receive a confirmation page confirming that “Jenkins is Ready!”:
Click Start using Jenkins to visit the main Jenkins dashboard:
At this point, you have completed a successful installation of Jenkins.
Step 4 — Installing Docker
First, update your existing list of packages:
sudo apt update
Next, install a few prerequisite packages which let apt use packages over HTTPS:
sudo apt install apt-transport-https ca-certificates curl software-properties-common
Then add the GPG key for the official Docker repository to your system:
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
Add the Docker repository to APT sources:
sudo add-apt-repository “deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable”
Next, update the package database with the Docker packages from the newly added repo:
sudo apt update
Make sure you are about to install from the Docker repo instead of the default Ubuntu repo:
apt-cache policy docker-ce
Finally, install Docker:
sudo apt install docker-ce
Docker should now be installed, the daemon started, and the process enabled to start on boot. Check that it’s running:
sudo systemctl status docker
The output should be similar to the following, showing that the service is active and running:
Output
● docker.service — Docker Application Container Engine
Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2020–05–19 17:00:41 UTC; 17s ago
TriggeredBy: ● docker.socket
Docs: https://docs.docker.com
Main PID: 24321 (dockerd)
Tasks: 8
Memory: 46.4M
CGroup: /system.slice/docker.service
└─24321 /usr/bin/dockerd -H fd:// — containerd=/run/containerd/containerd.sock
If you want to avoid typing sudo whenever you run the docker command, add your username to the docker group:
sudo usermod -a -G docker jenkins
Step 5 — Installing Nginx
sudo apt update
sudo apt install nginx
We can check with the systemd init system to make sure the service is running by typing:
systemctl status nginx
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 2020–04–20 16:08:19 UTC; 3 days ago
Docs: man:nginx(8)
Main PID: 2369 (nginx)
Tasks: 2 (limit: 1153)
Memory: 3.5M
CGroup: /system.slice/nginx.service
├─2369 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
└─2380 nginx: worker process
To redirect requests from port 80 to port 8080
Open default nginx configuration file
sudo vi /etc/nginx/sites-enabled/default
Add this to the top of the above file
upstream jenkins {
server 127.0.0.1:8080;
}
Add the following to the default server block
location / {
proxy_pass http://jenkins/;
proxy_redirect off;
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
add_header Access-Control-Allow-Origin “*” always;
}
Restart nginx
sudo systemctl restart nginx
Step 6 — Mount new disks for Jenkins and Docker
Create two Disks of 50 GB each for Jenkins and Docker in the virtual machine.
Connect to the Linux VM to mount the new disk.
To partition, format, and mount your new disk so your Linux VM can use it, SSH into your VM.
ssh azureuser@<public_ip>
Once connected to your VM, you need to find the disk. In this example, we are using lsblk to list the disks.
lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i “sd”
The output is similar to the following example:
sda 0:0:0:0. 30G
├─sda1 29.9G /
├─sda14 4M
└─sda15 106M /boot/efi
sdb 1:0:1:0 14G
└─sdb1 14G /mnt
sdc 3:0:0:0 50G
sdd 3:0:0:1 50G
In this example, the disk that I added is sdc and sdd.
Since we are attaching a new disk, we need to partition the disk.
The parted utility can be used to partition and format a data disk.
The following example uses parted on /dev/sdc and /dev/sdd, which is where the first data disk will typically be on most VMs. We are also formatting it using the XFS filesystem.
sudo parted /dev/sdc — script mklabel gpt mkpart xfspart xfs 0% 100%
sudo mkfs.xfs /dev/sdc1
sudo partprobe /dev/sdc1sudo parted /dev/sdd — script mklabel gpt mkpart xfspart xfs 0% 100%
sudo mkfs.xfs /dev/sdd1
sudo partprobe /dev/sdd1
Use the partprobe utility to make sure the kernel is aware of the new partition and filesystem. Failure to use partprobe can cause the blkid or lslbk commands to not return the UUID for the new filesystem immediately.
Mount the disk
Create a directory to mount the file system using mkdir.
sudo mkdir /docker
sudo mkdir /jenkins
Use mount to then mount the filesystem.
sudo mount /dev/sdc1 /docker
sudo mount /dev/sdd1 /jenkins
To ensure that the drive is remounted automatically after a reboot, it must be added to the /etc/fstab file. It is also highly recommended that the UUID (Universally Unique Identifier) is used in /etc/fstab to refer to the drive rather than just the device name (such as, /dev/sdc1). If the OS detects a disk error during boot, using the UUID avoids the incorrect disk being mounted to a given location. Remaining data disks would then be assigned those same device IDs. To find the UUID of the new drive, use the blkid utility:
sudo blkid
The output looks similar to the following example:
/dev/sda1: UUID=”b6b12996–3110–493e-9dd4–445695ff15f3" TYPE=”ext4" PARTUUID=”63a5366f-01"
/dev/sdb1: LABEL=”cloudimg-rootfs” UUID=”37a35aa8-cfae-400b-b3cf-d498145b528e” TYPE=”ext4" PARTUUID=”acefaae4-afed-42dd-96de-70518d1feb1a”
/dev/sdb15: LABEL=”UEFI” UUID=”9A97–7176" TYPE=”vfat” PARTUUID=”31d16809–30f8–498b-8f13–735b5f66ac0c”
/dev/sdc1: UUID=”306c37bf-4d07–4691–9371–5fb2fbfb78d1" TYPE=”xfs” PARTLABEL=”xfspart” PARTUUID=”c90d5679–159a-4ca0–8645–5ea9ac2fadd0"
/dev/sdd1: UUID=”18272942-bd3c-4da6-bfe8–54d6bde5dbe1" TYPE=”xfs” PARTLABEL=”xfspart” PARTUUID=”98e1a26a-7d0e-4f47-a434–0e0fb2f5935e”
/dev/sdb14: PARTUUID=”0786fc9d-ca31–4369-b78f-f47b53503fde”
Next, open the /etc/fstab file in a text editor as follows:
sudo nano /etc/fstab
In this example, use the UUID value for the /dev/sdc1 device that was created in the previous steps, and the mountpoint of /docker. Add the following line to the end of the /etc/fstab file:
UUID=306c37bf-4d07–4691–9371–5fb2fbfb78d1 /docker xfs defaults,nofail 1 2
UUID=18272942-bd3c-4da6-bfe8–54d6bde5dbe1 /jenkins xfs defaults,nofail 1 2
Verify the disk
You can now use lsblk again to see the disk and the mountpoint.
lsblk -o NAME,HCTL,SIZE,MOUNTPOINT | grep -i “sd”