天天看點

CentOS 7 部署SonarQube 8.3版本及配置jenkins分析代碼

一、安裝SonarQube 8.3版本

​​官方文檔​​

​​下載下傳位址​​

1.準備工作

  • 準備一台CentOS 7伺服器
  • SonarQube 8.3版本隻支援Java 11 (​​下載下傳Java 11​​)
  • 因為8.0版本後不支援MySQL,是以需要安裝PostgreSQL

2.安裝服務端程式并建立sonar使用者

2.1下載下傳安裝包

wget -c https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.3.1.34397.zip
unzip sonarqube-8.3.1.34397.zip
# 一定用ZIP解壓縮原始檔案,否則會産生很多._XXX的隐藏檔案,使程式報錯
mv  sonarqube-8.3.1.34397  /opt/SonarQube/sonarqube-8.3.1.34397      

2.2建立相關使用者

# 建立使用者
groupadd sonar
useradd sonar -g sonar
passwd sonar

chown -R sonar.sonar /opt/SonarQube/jdk-11.0.9
chown -R sonar.sonar /opt/SonarQube/sonarqube-8.3.1.34397      

2.3配置SonarQube

# 修改sonar.properties配置檔案($SONARQUBE-HOME/conf/sonar.properties)
cd /opt/SonarQube/sonarqube-8.3.1.34397/conf
vi sonar.properties
sonar.web.host=0.0.0.0

sonar.web.port=9000

sonar.jdbc.username=sonar

sonar.jdbc.password=sonar

sonar.jdbc.url=jdbc:postgresql://192.168.1.206/sonarqube

sonar.jdbc.maxActive=60

sonar.jdbc.maxIdle=5

sonar.jdbc.minIdle=2

sonar.jdbc.maxWait=5000

sonar.jdbc.minEvictableIdleTimeMillis=600000

sonar.jdbc.timeBetweenEvictionRunsMillis=30000

sonar.jdbc.removeAbandoned=true

sonar.jdbc.removeAbandonedTimeout=60


# 系統安裝的是Java 8,是以需要單獨指定Java 11的路徑
vi wrapper.conf

wrapper.java.command=/opt/SonarQube/jdk-11.0.9/bin/java

# elasticsearch需要改
vi /etc/sysctl.conf

vm.swappiness=0
net.core.somaxconn=1024
vm.overcommit_memory=1
vm.zone_reclaim_mode=1
vm.min_free_kbytes=512000
vm.max_map_count=655360
fs.file-max=131072

sysctl -p

# sonar是啟動elasticsearch的使用者
vi /etc/security/limits.conf

sonar hard nofile 65536
sonar soft nofile 65536      

2.4設定SonarQube服務

vi /etc/systemd/system/sonarqube.service
[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=simple
User=sonar
Group=sonar
PermissionsStartOnly=true
ExecStart=/opt/SonarQube/sonarqube-8.3.1.34397/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/SonarQube/sonarqube-8.3.1.34397/bin/linux-x86-64/sonar.sh stop
StandardOutput=syslog
LimitNOFILE=65536
LimitNPROC=8192
TimeoutStartSec=5
Restart=always
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target      

或者

systemctl daemon-reload
systemctl enable sonarqube.service
systemctl start sonarqube.service      

3.安裝PostgreSQL 12.0并建立sonar資料庫

3.1安裝PostgreSQL 12.0資料庫

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

yum install postgresql12-server

# 安裝後的資料庫data目錄
cd /var/lib/pgsql/12/data

# 修改配置
vi pg_hba.conf
host    all             all             0.0.0.0/0            md5

vi postgresql.conf
listen_addresses = '*'

systemctl restart postgresql-12

# 用戶端程式目錄
cd /usr/pgsql-12/bin      

3.2建立sonar使用者和sonar資料庫

# 安裝的時候會自動建立postgres使用者密碼為空
su - postgres
bash-4.2$ psql
psql (12.5)
輸入 "help" 來擷取幫助資訊.

# 建立使用者
create user sonar with password 'sonar';

# 建立資料庫指定所屬者
create database sonarqube owner=sonar encoding='UTF8';

# 将資料庫sonarqube所有權限指派給sonar
grant all on database sonarqube to sonar;

# 修改管理者密碼(預設是随機密碼)
ALTER USER postgres WITH PASSWORD 'postgres';

# 退出
\q
-bash-4.2$ exit      

3.3初始化并啟動資料庫

/usr/pgsql-12/bin/postgresql-12-setup initdb
systemctl enable postgresql-12
systemctl start postgresql-12      

3.4用navicat或者其他遠端資料庫工具連接配接

出現下圖所示代表連接配接成功

4.檢視是否啟動成功

su -sonar
#使用systemctl指令啟動或者使用bin目錄下的.sh腳本啟動
systemctl start sonarqube.service
或
su -sonar -c "/opt/SonarQube/sonarqube-8.3.1.34397/bin/linux-x86-64/sonar.sh start"
#檢查受否有端口号及程序号
ps -ef | grep sonar
netstat -nltp | grep sonar