Installing Jenkins on Rocky Linux 9
Installing Jenkins on Rocky Linux 9
在 Rocky Linux 9 上安装 Jenkins
Jenkins is a popular open-source automation server for continuous integration and continuous deployment (CI/CD), letting teams build, test, and deploy software with a large ecosystem of plugins. This guide installs Jenkins on Rocky Linux 9, secures the web interface behind Nginx with a Let’s Encrypt SSL certificate, tunes system limits for heavier workloads, and sets up automated backups. By the end, you’ll have Jenkins running securely over HTTPS with a test job verified through both the dashboard and the Jenkins CLI. Before you begin, you need access to a Rocky Linux 9 instance as a non-root sudo user, and a domain A record pointing to the instance’s IP address (for example, example.com) configured with your DNS provider.
Jenkins 是一款流行的开源自动化服务器,用于持续集成和持续部署 (CI/CD),它拥有庞大的插件生态系统,让团队能够构建、测试和部署软件。本指南将介绍如何在 Rocky Linux 9 上安装 Jenkins,通过 Nginx 和 Let’s Encrypt SSL 证书保护 Web 界面,针对繁重的工作负载调整系统限制,并设置自动备份。完成本指南后,你将拥有一个通过 HTTPS 安全运行的 Jenkins,并可以通过仪表板和 Jenkins CLI 验证测试任务。在开始之前,你需要以非 root sudo 用户身份访问 Rocky Linux 9 实例,并确保你的 DNS 提供商已配置指向该实例 IP 地址的域名 A 记录(例如 example.com)。
1. Install Java
1. 安装 Java
Jenkins requires the Java Development Kit (JDK) to run. Jenkins 需要 Java 开发工具包 (JDK) 才能运行。
-
Update the package index:
$ sudo dnf update -y -
更新软件包索引:
$ sudo dnf update -y -
Install OpenJDK 21 (or the latest version from the OpenJDK releases page):
$ sudo dnf install -y java-21-openjdk -
安装 OpenJDK 21(或从 OpenJDK 发布页面获取最新版本):
$ sudo dnf install -y java-21-openjdk -
Check for and switch away from an older default Java version:
$ sudo alternatives --config java -
检查并切换掉旧的默认 Java 版本:
$ sudo alternatives --config java
If multiple Java versions are installed, you’ll see output like: 如果安装了多个 Java 版本,你将看到如下输出:
There are two programs which provide 'java'.
Selection Command
-----------------------------------------------
*+ 1 java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.442.b06-2.el9.x86_64/jre/bin/java)
2 java-21-openjdk.x86_64 (/usr/lib/jvm/java-21-openjdk-21.0.6.0.7-1.el9.x86_64/bin/java)
Enter to keep the current selection[+], or type selection number:
Type 2 to select OpenJDK 21. 输入 2 以选择 OpenJDK 21。
- Verify the installation:
$ java -version - 验证安装:
$ java -version
2. Install Jenkins
2. 安装 Jenkins
-
Add the latest stable Jenkins repository to your DNF sources:
$ sudo wget https://pkg.jenkins.io/redhat-stable/jenkins.repo -O /etc/yum.repos.d/jenkins.repo -
将最新的稳定版 Jenkins 仓库添加到你的 DNF 源中:
$ sudo wget https://pkg.jenkins.io/redhat-stable/jenkins.repo -O /etc/yum.repos.d/jenkins.repo -
Import the GPG key for your version:
$ sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key -
导入对应版本的 GPG 密钥:
$ sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io-2023.key -
Install Jenkins:
$ sudo dnf install -y jenkins -
安装 Jenkins:
$ sudo dnf install -y jenkins -
Start the Jenkins service:
$ sudo systemctl start jenkins -
启动 Jenkins 服务:
$ sudo systemctl start jenkins -
Enable Jenkins to start on boot:
$ sudo systemctl enable jenkins -
设置 Jenkins 开机自启:
$ sudo systemctl enable jenkins -
Check its status:
$ sudo systemctl status jenkins -
检查其状态:
$ sudo systemctl status jenkins
3. Access the Jenkins Web Interface
3. 访问 Jenkins Web 界面
-
Allow the default Jenkins port 8080 through the firewall:
$ sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp -
通过防火墙放行 Jenkins 默认端口 8080:
$ sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp -
Reload the firewall:
$ sudo firewall-cmd --reload -
重载防火墙:
$ sudo firewall-cmd --reload -
Get the initial administrator password:
$ sudo cat /var/lib/jenkins/secrets/initialAdminPasswordCopy the output. -
获取初始管理员密码:
$ sudo cat /var/lib/jenkins/secrets/initialAdminPassword复制输出内容。 -
Open Jenkins in a browser using your domain name on port 8080:
http://example.com:8080 -
在浏览器中使用你的域名和 8080 端口打开 Jenkins:
http://example.com:8080 -
Paste the password into the Administrator Password field and click Continue.
-
将密码粘贴到“管理员密码”字段中,然后点击“继续”。
-
Select Install suggested Plugins to install the recommended Jenkins plugins.
-
选择 Install suggested Plugins(安装建议插件)以安装推荐的 Jenkins 插件。
-
Fill in the details for your first Admin User and click Save and Continue.
-
填写你的第一个管理员用户详细信息,然后点击“保存并继续”。
-
Confirm the Jenkins URL, which should match your domain name, then click Save and Finish.
-
确认 Jenkins URL(应与你的域名匹配),然后点击“保存并完成”。
-
Click Start using Jenkins to open the Jenkins dashboard.
-
点击 Start using Jenkins(开始使用 Jenkins)以打开 Jenkins 仪表板。
4. Secure Jenkins with Let’s Encrypt SSL Certificates
4. 使用 Let’s Encrypt SSL 证书保护 Jenkins
-
Install the Nginx web server:
$ sudo dnf install nginx -y -
安装 Nginx Web 服务器:
$ sudo dnf install nginx -y -
Install Certbot and its Nginx plugin:
$ sudo dnf install -y certbot python3-certbot-nginx -
安装 Certbot 及其 Nginx 插件:
$ sudo dnf install -y certbot python3-certbot-nginx -
Allow HTTP traffic:
$ sudo firewall-cmd --permanent --add-service=http -
允许 HTTP 流量:
$ sudo firewall-cmd --permanent --add-service=http -
Allow HTTPS traffic:
$ sudo firewall-cmd --permanent --add-service=https -
允许 HTTPS 流量:
$ sudo firewall-cmd --permanent --add-service=https -
Reload the firewall:
$ sudo firewall-cmd --reload -
重载防火墙:
$ sudo firewall-cmd --reload -
Request an SSL certificate:
$ sudo certbot certonly --standalone -d example.comCertbot will prompt you in sequence: for an email address; to accept its terms of service; and, optionally, whether to share your email with the Electronic Frontier Foundation (you can decline with N). Your domain now has a valid certificate for TLS encryption. -
申请 SSL 证书:
$ sudo certbot certonly --standalone -d example.comCertbot 将依次提示你:输入电子邮件地址;接受其服务条款;以及是否选择与电子前沿基金会 (EFF) 分享你的电子邮件(你可以输入 N 拒绝)。现在你的域名已拥有用于 TLS 加密的有效证书。 -
Enable and start Nginx:
$ sudo systemctl enable --now nginx -
启用并启动 Nginx:
$ sudo systemctl enable --now nginx -
Allow Jenkins to communicate over the network via SELinux:
$ sudo setsebool -P httpd_can_network_connect 1 -
通过 SELinux 允许 Jenkins 进行网络通信:
$ sudo setsebool -P httpd_can_network_connect 1 -
Open the Nginx configuration file:
$ sudo nano /etc/nginx/conf.d/jenkins.confAdd the following configuration: -
打开 Nginx 配置文件:
$ sudo nano /etc/nginx/conf.d/jenkins.conf添加以下配置:
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
...