How to Install Jenkins on a CentOS 7 Cloud Server?

19-02-2024 02:19:03

Jenkins is an open-source continuous integration tool, widely used for project management and automated deployment. This guide explains the process of installing Jenkins on a CentOS 7 cloud server. To facilitate access management, Nginx will also be installed to serve as a reverse proxy server for Jenkins.

Installing Java

One of the best practices in Linux system administration is to keep the system up to date. We start by installing the EPEL repository, then update the system to the latest state and reboot.

sudo yum install epel-release
sudo yum update
sudo reboot

Before installing Jenkins, it is necessary to install the Java Virtual Machine on the system. Here, we use the OpenJDK Runtime Environment version 1.8.0 as an example.

sudo yum install java-1.8.0-openjdk.x86_64

After the installation, check the Java version to confirm the successful installation.

java -version

The return result will display the recently installed OpenJDK Runtime Environment.

openjdk version "1.8.0_91"
OpenJDK Runtime Environment (build 1.8.0_91-b14)
OpenJDK 64-Bit Server VM (build 25.91-b14, mixed mode)

To allow Java applications to locate the Java Virtual Machine, we need to set two environment variables: JAVA_HOME and JRE_HOME.

sudo cp /etc/profile /etc/profile_backup
echo 'export JAVA_HOME=/usr/lib/jvm/jre-1.8.0-openjdk' | sudo tee -a /etc/profile
echo 'export JRE_HOME=/usr/lib/jvm/jre' | sudo tee -a /etc/profile
source /etc/profile

Display these two environment variables.

echo $JAVA_HOME
echo $JRE_HOME

Installing Jenkins

Install the latest version of Jenkins using the official YUM repository.

cd ~ 
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install jenkins

Start the Jenkins service and set it to follow system startup.

sudo systemctl start jenkins.service
sudo systemctl enable jenkins.service

Access to Jenkins requires opening port 8080, so it is necessary to enable this port on the firewall.

sudo firewall-cmd --zone=public --permanent --add-port=8080/tcp
sudo firewall-cmd --reload

Next, we access http://IPaddress:8080 in a browser to check if the Jenkins installation and configuration were successful.

Installing Nginx (Optional)

To facilitate user access, we can set up Nginx as a reverse proxy server for Jenkins, allowing users to access Jenkins without entering the 8080 port number.

Install Nginx using YUM.

sudo yum install nginx

Modify the Nginx configuration file.

sudo vi /etc/nginx/nginx.conf

Find the following two lines of configuration.

location / {
}

Insert the following code into the curly braces {}.

location / {
    proxy_pass http://127.0.0.1:8080;
    proxy_redirect off;
    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;
}

Save the configuration file and start the Nginx service.

sudo systemctl start nginx.service
sudo systemctl enable nginx.service

Open port 80 on the firewall.

sudo firewall-cmd --zone=public --permanent --add-service=http
sudo firewall-cmd --reload

Finally, we directly access the IP address http://IPaddress in a browser to check if the Nginx installation and configuration were successful.