Deploying Jenkins on Linux with Systemd
Environment Preparation
Jenkins requires a Java Development Kit (JDK) and a build tool like Maven to function correctly. The following steps outline the installation of these dependencies on a Linux system.
Installing OpenJDK 11
Modern versions of Jenkins require Java 11 or later. It is recommended to set up a dedicated directory for the Java installation.
mkdir -p /usr/lib/jvm
cd /usr/lib/jvm
wget https://repo.huaweicloud.com/openjdk/11.0.1/openjdk-11.0.1_linux-x64_bin.tar.gz
tar -xzf openjdk-11.0.1_linux-x64_bin.tar.gz
mv jdk-11.0.1 java-11
rm openjdk-11.0.1_linux-x64_bin.tar.gzConfigure the system environment variables by creating a profile script.
cat <<EOF > /etc/profile.d/jdk.sh
export JAVA_HOME=/usr/lib/jvm/java-11
export PATH=\$PATH:\$JAVA_HOME/bin
EOF
source /etc/profile.d/jdk.sh
java -versionInstalling Apache Maven
Install Maven using the package manager and configure the mirror repository for faster dependency resolution.
yum install -y mavenEdit the Maven settings file located at /etc/maven/settings.xml to include the Aliyun mirror within the <mirrors> section.
<mirror>
<id>aliyun-maven</id>
<mirrorOf>central</mirrorOf>
<name>Aliyun Maven Mirror</name>
<url>https://maven.aliyun.com/repository/public</url>
</mirror>Jenkins Server Installation
Add the official Jenkins repository to the system's package manager list.
wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo --no-check-certificate
rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key
yum install -y jenkinsService Configuration
Rather than using a manual startup script, managing Jenkins via systemd is recommended for stability and automatic startup capabilities. Locate the jenkins.war file to verify its path.
find / -name "jenkins.war"
# Typically located at /usr/share/java/jenkins.warCreate a systemd service unit file at /etc/systemd/system/jenkins.service. This configuration utilizes the installed JDK 11 and sets a custom HTTP port.
[Unit]
Description=Jenkins Continuous Integration Server
After=network.target
[Service]
Type=simple
User=root
Environment="JENKINS_HOME=/var/lib/jenkins"
WorkingDirectory=/var/lib/jenkins
ExecStart=/usr/lib/jvm/java-11/bin/java -jar /usr/share/java/jenkins.war --httpPort=8091
Restart=always
[Install]
WantedBy=multi-user.targetReload the systemd daemon, enable the service to start on boot, and start the service.
systemctl daemon-reload
systemctl enable jenkins
systemctl start jenkinsVerify the service status and access the web interface at http://<server-ip>:8091.
Troubleshooting Plugin Installation
During the initial setup, the plugin installation wizard may encounter SSL certificate validation errors (PKIX path building failed). To resolve this, manually install the skip-certificate-check plugin.
Navigate to the Jenkins plugins directory and download the plugin manually.
mkdir -p /root/.jenkins/plugins
cd /root/.jenkins/plugins
wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/plugins/skip-certificate-check/latest/skip-certificate-check.hpiRestart the Jenkins service to load the plugin. After the restart, allow a few minutes for the initialization process to complete before attempting to install other plugins such as Git.
systemctl restart jenkins