智能
助手
最大化  清空记录 停止  历史记录
翻译选中文本
选中一段文本后进行翻译
名词解释
选中一段文本后进行名词解释
知识图谱生成
通过图谱展示知识信息
登录用户在知识浏览页面可用
答案生成
AI自动回答一个问答功能中的问题
登录用户在问答浏览页面,且问题开放回答中可用
知识摘要
自动为当前知识生成摘要
知识浏览页面可用
知识问答
针对当前知识进行智能问答
知识浏览面可用
2025-11-23 00:27:10 版本 : 6. SonarQube服务-02-部署服务
作者: 文艺范儿 于 2025年11月17日 发布在分类 / DevOps / sonar 下,并于 2025年11月23日 编辑
 历史版本

备注 修改日期 修改人
修改标题 2025-11-23 00:27:10[当前版本] 文艺范儿
格式调整 2025-11-22 18:59:34 文艺范儿
创建版本 2025-11-17 22:35:38 文艺范儿

6.2 部署服务

1. 安装jdk21

jdk21在官网下载:https://www.oracle.com/cn/java/technologies/downloads/#java21

[root@203-sonar ~]# ll -h jdk-21_linux-x64_bin.rpm 
-rw-r--r-- 1 root root 188M 11月 17 2025 jdk-21_linux-x64_bin.rpm
[root@203-sonar ~]# rpm -ivh jdk-21_linux-x64_bin.rpm 
警告:jdk-21_linux-x64_bin.rpm: 头V3 RSA/SHA256 Signature, 密钥 ID 8d8b756f: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:jdk-21-2000:21.0.9-7             ################################# [100%]
[root@203-sonar ~]# java -version
java version "21.0.9" 2025-10-21 LTS
Java(TM) SE Runtime Environment (build 21.0.9+7-LTS-338)
Java HotSpot(TM) 64-Bit Server VM (build 21.0.9+7-LTS-338, mixed mode, sharing)

2. 安装postgresql 15数据库

1##yum添加官方仓库并安装
[root@203-sonar ~]# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
[root@203-sonar ~]# yum install -y postgresql15-server postgresql15-contrib
2##初始化数据库
[root@203-sonar ~]# /usr/pgsql-15/bin/postgresql-15-setup initdb
Initializing database ... OK
[root@203-sonar ~]# systemctl start postgresql-15
[root@203-sonar ~]# systemctl enable postgresql-15
3##创建数据库和用户
a##切换到postgres系统用户来操作数据库
[root@203-sonar ~]# sudo -i -u postgres
[postgres@203-sonar ~]$
b##进入PostgreSQL交互终端
[postgres@203-sonar ~]$ psql
psql (13.22)
输入 "help" 来获取帮助信息.

postgres=# 
c##再psql终端中,一次执行sql命令来创建数据库sonarqube和用户sonarqube,然后退出sql终端
postgres=# CREATE USER sonarqube WITH PASSWORD 'Password@123';
CREATE ROLE
postgres=# CREATE DATABASE sonarqube OWNER sonarqube;
CREATE DATABASE
postgres=# GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonarqube;
GRANT
postgres-# \q
##然后退出postgres用户
[postgres@203-sonar ~]$ exit
d##配置postgerSQL以允许连接
[root@203-sonar ~]# sudo vim /var/lib/pgsql/15/data/pg_hba.conf
...
host    all             all             127.0.0.1/32            scram-sha-256
...
##15版本默认不用改变配置
[root@203-sonar ~]# grep -v -E "^#|^$"  /var/lib/pgsql/15/data/pg_hba.conf
local   all             all                                     peer
host    all             all             127.0.0.1/32            scram-sha-256
host    all             all             ::1/128                 scram-sha-256
local   replication     all                                     peer
host    replication     all             127.0.0.1/32            scram-sha-256
host    replication     all             ::1/128                 scram-sha-256

[root@203-sonar ~]# systemctl restart postgresql-15   ##重启使配置生效

3. 安装和配置 SonarQube

官网下载地址:https://binaries.sonarsource.com/?prefix=Distribution/sonarqube/

1##下载并解压sonarqube
[root@203-sonar ~]# wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-25.9.0.112764.zip
[root@203-sonar ~]# unzip sonarqube-25.9.0.112764.zip -d /opt/
[root@203-sonar ~]# ln -s /opt/sonarqube-25.9.0.112764 /opt/sonarqube
2##创建专用系统用户(安全要求:sonarqube不允许使用root运行)
[root@203-sonar ~]# useradd -r -s /bin/false sonarqube
[root@203-sonar ~]# chown -R sonarqube:sonarqube /opt/sonarqube/  ##注意/opt/sonarqube/最后面的/; 
3##配置SonarQube配置文件#注意密码那里不能加引号
[root@203-sonar ~]# grep -vE '^#|^$' /opt/sonarqube/conf/sonar.properties
# 数据库配置
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
sonar.jdbc.username=sonarqube
sonar.jdbc.password=Password@123

# Web 服务器配置
sonar.web.host=0.0.0.0
sonar.web.port=9000

# JVM 配置(Java 21 优化)
sonar.web.javaOpts=-Xmx2g -Xms512m --enable-preview
sonar.ce.javaOpts=-Xmx2g -Xms512m --enable-preview
# 设置 Elasticsearch 的初始堆大小和最大堆大小相等
sonar.search.javaOpts=-Xmx1g -Xms1g -Djava.security.manager=allow --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED

# 其他优化配置
sonar.search.backup.path=/opt/sonarqube/data/es-backup
sonar.path.data=/opt/sonarqube/data
sonar.path.temp=/opt/sonarqube/temp
sonar.path.logs=/opt/sonarqube/logs

4##上传插件,这里插件是下载后打包的
[root@203-sonar extensions]# cd /opt/sonarqube/extensions/
[root@203-sonar extensions]# rm -rf plugins/
[root@203-sonar extensions]# ll -h sonarqube_25.9_plugins.tar.gz 
-rw-r--r-- 1 root root 277M 11月 17 01:01 sonarqube_25.9_plugins.tar.gz
[root@203-sonar extensions]# tar xf sonarqube_25.9_plugins.tar.gz 

5##配置系统服务
[root@203-sonar ~]# vim /etc/systemd/system/sonarqube.service
[root@203-sonar ~]# cat /etc/systemd/system/sonarqube.service
[Unit]
Description=SonarQube 25.9.0 service
After=syslog.target network.target postgresql-15.service

[Service]
Type=forking

ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
User=sonarqube
Group=sonarqube
Restart=always
LimitNOFILE=131072
LimitNPROC=8192

[Install]
WantedBy=multi-user.target
5##修改系统配置
[root@203-sonar ~]# vim /etc/sysctl.conf
...
vm.max_map_count = 262144
net.core.somaxconn = 16384
vm.swappiness = 1
...

[root@203-sonar ~]# sysctl -p
...
vm.max_map_count = 262144
net.core.somaxconn = 16384
vm.swappiness = 1
#设置文件描述符
[root@203-sonar ~]# echo 'sonarqube - nofile 65536' | sudo tee -a /etc/security/limits.conf
sonarqube - nofile 65536
[root@203-sonar ~]# echo 'sonarqube - nproc 8192' | sudo tee -a /etc/security/limits.conf
sonarqube - nproc 8192

4. 启动和验证

# 重新加载 systemd 配置
sudo systemctl daemon-reload
# 启用开机自启
sudo systemctl enable sonarqube
# 启动 SonarQube 服务
sudo systemctl start sonarqube
# 检查服务状态
sudo systemctl status sonarqube

打开浏览器,访问:http://你的服务器IP地址:9000

首次访问会看到登录页面:

  • 默认用户名: admin

  • 默认密码: admin

登录后系统会强制要求你修改密码。密码须通过验证 :Dong@12345678

5.配置远程访问token

1.访问sonarqube:http://你的服务器:9000

2.登录

3.点击 用户头像→ 我的账户 → 安全

4.生成令牌

粘贴图片

6. 配置客户端

客户端IP:10.0.0.201

a. 下载 SonarScanner

最新版本下载链接

# Linux 64-bit
[root@201-jenkins ~]# wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-7.2.0.5079-linux-x64.zip

b. 安装 SonarScanner(Linux)

1##解压
[root@201-jenkins ~]# unzip sonar-scanner-cli-7.2.0.5079-linux-x64.zip -d /opt/
[root@201-jenkins ~]# ln -s /opt/sonar-scanner-7.2.0.5079-linux-x64 /opt/sonar-scanner
2##设置环境变量并生效
[root@201-jenkins ~]# echo 'export SONAR_SCANNER_HOME=/opt/sonar-scanner' | sudo tee -a /etc/profile
[root@201-jenkins ~]# echo 'export PATH=$SONAR_SCANNER_HOME/bin:$PATH' | sudo tee -a /etc/profile
[root@201-jenkins ~]# source /etc/profile

c. 验证安装

[root@201-jenkins ~]# sonar-scanner --version
08:34:47.792 INFO  Scanner configuration file: /opt/sonar-scanner-7.2.0.5079-linux-x64/conf/sonar-scanner.properties
08:34:47.795 INFO  Project root configuration file: NONE
08:34:47.809 INFO  SonarScanner CLI 7.2.0.5079
08:34:47.810 INFO  Linux 3.10.0-1160.90.1.el7.x86_64 amd64
##显示SonarScanner 7.2.0.5079

d. 客户端测试将代码扫描到服务器进行代码检测

需要在sonar页面创建html项目,将game目录下的游戏代码推送到SonrQube测试

粘贴图片

粘贴图片

粘贴图片

粘贴图片

粘贴图片

粘贴图片

粘贴图片


然后再在客户端jenkins服务器操作

1##注意分析web项目需要先在客户端安装nodejs
[root@201-jenkins ~]# yum install -y nodejs npm
2##clone game代码进行测试
[root@201-jenkins game]# cd /root/game/
[root@201-jenkins game]# ll -h
总用量 48K
-rw-r--r-- 1 root root  28K 11月 17 08:47 bgm.mp3
drwxr-xr-x 2 root root   23 11月 17 08:47 css
drwxr-xr-x 2 root root   23 11月 17 08:47 images
-rw-r--r-- 1 root root 8.8K 11月 17 08:47 index.html
drwxr-xr-x 2 root root  213 11月 17 08:47 js
drwxr-xr-x 2 root root 4.0K 11月 17 08:47 roms
-rw-r--r-- 1 root root  811 11月 17 08:47 shuoming.html

[root@201-jenkins game]# sonar-scanner \
  -Dsonar.projectKey=html \
  -Dsonar.sources=. \
  -Dsonar.nodejs.executable=/usr/bin/node \
  -Dsonar.nodejs.disableAutoDownload=true \
  -Dsonar.javascript.file.suffixes=-1 \
  -Dsonar.typescript.file.suffixes=-1 \
  -Dsonar.host.url=http://10.0.0.203:9000 \
  -Dsonar.token=sqp_45f7bd377e3fa608e564098d0eb49c89b307b444
 ##运行完会显示以下内容
粘贴图片

参数分类总结

类别 参数 作用 解决什么问题

项目配置

projectKey=html

项目标识

在 SonarQube 中识别项目

源代码

sources=.

扫描当前目录

分析哪些文件

Node.js

nodejs.executable

使用系统 Node.js

避免版本冲突

Node.js

nodejs.disableAutoDownload=true

禁用自动下载

避免网络问题

语言分析

javascript.file.suffixes=-1

禁用 JS 分析

解决桥接器崩溃

语言分析

typescript.file.suffixes=-1

禁用 TS 分析

解决桥接器崩溃

服务器

host.url

服务器地址

指定结果发送位置

认证

login=

认证令牌

访问权限认证

再到sonar页面进行扫描结果查看

扫描完成后,访问:http://你的服务器:9000/dashboard?id=你的项目KEY

粘贴图片


e. 项目级配置(推荐)

在项目根目录创建 sonar-project.properties文件,此处暂不做介绍。

历史版本-目录  [回到顶端]
    文艺知识分享平台 -V 5.2.5 -wcp