在 Rocky Linux 上安装 SonarQube
本指南介绍如何在 Rocky Linux 9 服务器上安装和配置 SonarQube。SonarQube 是一款开源静态代码分析平台:可检测缺陷、代码坏味道和安全漏洞,支持多种编程语言,并可通过自动化代码审查保持高质量。SonarQube 基于 Java,可部署在各种 Linux 发行版(包括 Rocky Linux)或容器(Docker、Kubernetes)中。
本文描述的部署使用 OpenJDK 17、PostgreSQL 作为数据库、Apache 作为反向代理,以及 Let's Encrypt 提供 HTTPS。
订购服务器
要托管 SonarQube 实例,HostMyServers 提供多种方案:
- Performance VPS - 适合小型项目
- NVMe VPS - 性价比高
- 经济型专用服务器 - 适合多项目或重度分析
- 性能专用服务器 - 最佳性能
前提条件
- 以 root 或具有 sudo 权限的用户进行 SSH 访问
- Rocky Linux 9 64 位系统
- 至少 4 GB 内存(SonarQube 与 Elasticsearch 对内存要求较高)
- 一个 域名(例如
sonarqube.您的域名.com),并有一条指向服务器 IP 的 A 记录 - 端口 80(HTTP)和 443(HTTPS)可访问
配置要求
| 组件 | 最低 | 推荐 |
|---|---|---|
| 内存 | 4 GB | 8 GB |
| CPU | 2 核 | 4 核 |
| 存储 | 10 GB | 20 GB |
| 网络 | 100 Mbps | 1 Gbps |
SonarQube 在本地监听 9000 端口。Apache 反向代理到该端口;公网通过 80/443 的 HTTP/HTTPS 访问。
连接服务器
通过 SSH 连接到 Rocky Linux 服务器:
ssh 用户@服务器IP地址
系统更新
开始前先更新软件包:
sudo dnf update -y
安装 OpenJDK 17
SonarQube 需要 JRE/JDK。SonarQube 2025 LTA(长期支持)版本需要 Java 17 或 21。我们使用 OpenJDK 17。
sudo dnf install -y java-17-openjdk java-17-openjdk-devel
验证已安装版本:
java -version
应看到类似:openjdk version "17.x.x"。若当前为更旧版本,可用 sudo alternatives --config java 选择 Java 17。
创建 SonarQube 用户
SonarQube 使用 Elasticsearch 进行搜索与索引;必须以 非 root 用户运行。请创建专用系统用户:
sudo useradd --system sonarqube
SonarQube 的 PostgreSQL 数据库
SonarQube 支持多种数据库引擎。我们使用 PostgreSQL。
安装并初始化 PostgreSQL
sudo dnf install -y postgresql-server postgresql-contrib
sudo postgresql-setup --initdb
sudo systemctl enable postgresql
sudo systemctl start postgresql
创建 数据库和用户
以用户 postgres 连接 PostgreSQL:
sudo -i -u postgres psql
在 PostgreSQL 控制台中执行(将 您的密码 替换为强密码):
\password postgres
CREATE DATABASE sonardb;
CREATE USER sonaruser WITH ENCRYPTED PASSWORD '您的密码';
GRANT ALL PRIVILEGES ON DATABASE sonardb TO sonaruser;
\q
启用密码认证(pg_hba.conf)
编辑认证配置文件:
sudo nano /var/lib/pgsql/data/pg_hba.conf
对本地与 TCP 连接,对「local」使用 trust,对「host」(如 127.0.0.1/32 和 ::1/128)使用 md5。示例:
# TYPE DATABASE USER ADDRESS METHOD
local all all trust
host all all 127.0.0.1/32 md5
host all all ::1/128 md5
重启 PostgreSQL:
sudo systemctl restart postgresql
安装 SonarQube
SonarQube 不在默认 DNF 仓库中。请从官网下载压缩包。
下载并解压 SonarQube
请查看 SonarQube 下载页 获取最新 LTA 版本。以下以 25.x 为例(如需请调整 URL 和目录名):
sudo dnf install -y unzip
sudo mkdir -p /opt/sonarqube
cd /tmp
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-25.1.0.102122.zip
unzip sonarqube-25.1.0.102122.zip
sudo mv sonarqube-25.1.0.102122/* /opt/sonarqube
请将 sonarqube-25.1.0.102122 替换为您下载版本解压后的实际目录名。
安装 SonarScanner CLI(可选但推荐)
SonarScanner 可从命令行分析项目。从 SonarScanner CLI 下载最新版本。
sudo mkdir -p /opt/sonarscanner
cd /tmp
wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-7.0.2.4839-linux-x64.zip
unzip sonar-scanner-cli-7.0.2.4839-linux-x64.zip
sudo mv sonar-scanner-7.0.2.4839-linux-x64/* /opt/sonarscanner
配置 SonarQube 服务器 URL(用于本地分析):
sudo nano /opt/sonarscanner/conf/sonar-scanner.properties
添加或设置:sonar.host.url=http://127.0.0.1
使可执行文件在系统中可用:
sudo chmod +x /opt/sonarscanner/bin/sonar-scanner
sudo ln -sf /opt/sonarscanner/bin/sonar-scanner /usr/local/bin/sonar-scanner
验证:sonar-scanner -v
配置 SonarQube
sonar.properties 文件
编辑 SonarQube 主配置:
sudo nano /opt/sonarqube/conf/sonar.properties
在文件末尾添加(将 您的密码 替换为 PostgreSQL 用户 sonaruser 的密码):
sonar.jdbc.username=sonaruser
sonar.jdbc.password=您的密码
sonar.jdbc.url=jdbc:postgresql://localhost/sonardb
sonar.web.host=0.0.0.0
sonar.web.port=9000
sonar.web.javaOpts=-Xmx512m -Xms256m -XX:+HeapDumpOnOutOfMemoryError
sonar.search.javaOpts=-Xmx1g -Xms1g -XX:MaxDirectMemorySize=512m -XX:+HeapDumpOnOutOfMemoryError
sonar.path.data=data
sonar.path.temp=temp
| 参数 | 说明 |
|---|---|
sonar.jdbc.* | PostgreSQL 连接 |
sonar.web.host | 监听所有 接口 (0.0.0.0) |
sonar.web.port | Web 端口 (9000) |
sonar.web.javaOpts | Web 服务 JVM 参数 |
sonar.search.javaOpts | Elasticsearch(搜索引擎)JVM 参数 |
sonar.path.data / sonar.path.temp | 数据与临时目录 |
权限与系统限制
将 SonarQube 目录属主设为用户 sonarqube:
sudo chown -R sonarqube:sonarqube /opt/sonarqube
sudo chmod -R 775 /opt/sonarqube
SonarQube(Elasticsearch)需要更高的内存映射区域限制。在 /etc/sysctl.conf 中添加:
sudo nano /etc/sysctl.conf
在末尾添加:
vm.max_map_count=524288
fs.file-max=131072
然后执行:sudo sysctl -p
SonarQube 的 systemd 服务
SonarQube 在 /opt/sonarqube/bin/linux-x86-64/sonar.sh 提供启动脚本。为其创建 systemd 服务。
SELinux 上下文(Rocky Linux 9)
在 Rocky Linux 上 SELinux 已启用。允许脚本执行:
sudo chcon -t bin_t /opt/sonarqube/bin/linux-x86-64/sonar.sh
sudo restorecon -Rv /opt/sonarqube
服务文件
sudo nano /etc/systemd/system/sonarqube.service
内容:
[Unit]
Description=SonarQube service
After=syslog.target network.target
[Service]
Type=forking
User=sonarqube
Group=sonarqube
PermissionsStartOnly=true
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
Restart=always
RestartSec=10
LimitNOFILE=131072
LimitNPROC=8192
SuccessExitStatus=143
TimeoutStartSec=300
[Install]
WantedBy=multi-user.target
启用并启动服务:
sudo systemctl daemon-reload
sudo systemctl enable sonarqube
sudo systemctl start sonarqube
sudo systemctl status sonarqube
状态应为 active (running)。首次启动可能需要数分钟(Elasticsearch 与数据库初始化)。
Apache 反向代理与 HTTPS
SonarQube Web 界面在本地 9000 端口提供。要对外提供 HTTP/HTTPS,我们使用 Apache 作为反向代理。
安装 Apache
sudo dnf install -y httpd
sudo systemctl enable httpd
sudo systemctl start httpd
确认已加载 proxy 模块:sudo httpd -M | grep proxy(proxy_module、proxy_http_module、proxy_balancer_module)。如需,在 /etc/httpd/conf/httpd.conf 中取消对应行的注释。
SonarQube 虚拟主机
为您的域名创建配置文件(将 sonarqube.您的域名.com 替换为您的域名):
sudo nano /etc/httpd/conf.d/sonarqube.conf
内容:
<VirtualHost *:80>
ServerName sonarqube.您的域名.com
ServerAdmin admin@您的域名.com
ProxyPreserveHost On
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/
TransferLog /var/log/httpd/sonarqube_access.log
ErrorLog /var/log/httpd/sonarqube_error.log
</VirtualHost>
测试配置并重启 Apache:
sudo apachectl configtest
sudo systemctl restart httpd
允许 Apache 连接后端(SELinux)
使 Apache 能代理到 localhost:9000:
sudo setsebool -P httpd_can_network_connect 1
防火墙
开放 HTTP 与 HTTPS 端口:
sudo firewall-cmd --add-service=http --permanent
sudo firewall-cmd --add-service=https --permanent
sudo firewall-cmd --reload
使用 Certbot 的 TLS 证书(Let's Encrypt)
为 HTTPS 访问配置证书:
sudo dnf install -y certbot python3-certbot-apache
sudo certbot --apache -d sonarqube.您的域名.com -m 您的邮箱@示例.com --agree-tos
Certbot 会为 Apache 配置 HTTPS 并自动续期证书。
访问 SonarQube
- 在浏览器中打开:
https://sonarqube.您的域名.com - 使用默认账号登录:admin / admin
- SonarQube 会要求 修改管理员密码;请设置强密码。
- 用于分析(CI/CD 或命令行)时,可创建 用户 或 令牌:管理 → 安全 → 用户 → 创建用户,再为该账号生成令牌。
使用 SonarScanner 分析项目
要验证安装,可分析示例项目:
cd ~
mkdir -p projects && cd projects
git clone https://github.com/SonarSource/sonar-scanning-examples
cd sonar-scanning-examples/sonar-scanner
在 SonarQube 界面中生成令牌(管理 → 安全 → 用户 → 令牌),然后执行分析(将 您的令牌 替换为生成的令牌):
sonar-scanner -D sonar.token=您的令牌
分析完成后,在界面中查看项目:项目 → 已分析项目会显示指标(缺陷、漏洞、覆盖率、重复等)。
常用命令
| 命令 | 说明 |
|---|---|
sudo systemctl status sonarqube | SonarQube 服务状态 |
sudo systemctl restart sonarqube | 重启 SonarQube |
sudo systemctl restart httpd | 重启 Apache |
sonar-scanner -v | SonarScanner 版本 |
sonar-scanner -D sonar.token=TOKEN | 使用令牌运行分析 |
目录结构
| 路径 | 说明 |
|---|---|
/opt/sonarqube/ | SonarQube 安装(bin、conf、data、temp、extensions) |
/opt/sonarqube/conf/sonar.properties | 主配置 |
/opt/sonarscanner/ | SonarScanner CLI 安装 |
/etc/httpd/conf.d/sonarqube.conf | Apache 虚拟主机 |
故障排除
SonarQube 服务无法启动
- 查看日志:
sudo journalctl -u sonarqube.service -n 100 - 查看 SonarQube 日志:
tail -f /opt/sonarqube/logs/sonar.log与tail -f /opt/sonarqube/logs/web.log - 确认 PostgreSQL 已启动:
sudo systemctl status postgresql - 检查内核参数:
sysctl vm.max_map_count(应至少为 524288)
通过 Apache 访问出现 503 或无法打开页面
- 确认 SonarQube 在 9000 端口监听:
ss -tlnp | grep 9000 - 检查 SELinux:
getsebool httpd_can_network_connect(应为 "on") - 查看 Apache 日志:
sudo tail -f /var/log/httpd/sonarqube_error.log
Java 或内存
- 若
sonar.properties中的 JVM 参数对当前内存过小,可增大sonar.web.javaOpts和sonar.search.javaOpts(例如根据可用内存将 web 设为-Xmx1g,search 设为-Xmx2g)。