SonarQube 是一個用於代碼質量管理的開源平台,用於管理源代碼的質量。 通過插件形式,可以支持包括 java, C#, C/C++, PL/SQL, Cobol, JavaScrip, Groovy 等等二十幾種編程語言的代碼質量管理與檢測。Sonar可以從以下七個維度檢測代碼質量,而作為開發人員至少需要處理前5種代碼質量問題。
運行環境
系統版本:CentOS Linux release 7.6.1810 (Core)
軟件版本:sonarqube-8.2
硬件要求:最低2核4GB
安裝過程
1、配置系統環境
1.1、關閉防火牆和SeLinux
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i 's/SELINUX=.*/SELINUX=disabled/g' /etc/sysconfig/selinux
1.2、設置系統可並發打開的文件數量為65535
[root@localhost ~]# vim /etc/security/limits.conf
root soft nofile 65536
root hard nofile 65536
* soft nofile 65536
* hard nofile 65536
[root@localhost ~]# ulimit -n 65536
1.3、設置mmap計數為262144
Elasticsearch默認使用mmapfs來存儲索引,mmap計數可能由於系統限制值太低了,可能會導致ES出現內存不足的異常,我們需要將其設置為更大。
[root@localhost ~]# vim /etc/sysctl.conf
vm.max_map_count=262144
[root@localhost ~]# sysctl -p
1.4、關閉SWAP
[root@localhost ~]# swapoff -a
2、安裝PostgreSQL
2.1、安裝YUM-PostgreSQL存儲庫
YUM-PostgreSQL存儲庫由PostgreSQL官方提供。
[root@localhost ~]# yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
2.2、安裝PostgreSQL12
[root@localhost ~]# yum -y install postgresql12 postgresql12-server
2.3、初始化數據庫
[root@localhost ~]# /usr/pgsql-12/bin/postgresql-12-setup initdb
2.4、修改配置,監聽所有網卡地址
這樣其他主機也可以通過主網卡訪問到PostgreSQL數據庫,默認情況下如果不修改,則PostgreSQL只允許本地訪問。
[root@localhost ~]# vi /var/lib/pgsql/12/data/postgresql.conf
listen_addresses = '*'
port = 5432
2.5、添加信任網段,允許其他主機訪問
[root@localhost ~]# vi /var/lib/pgsql/12/data/pg_hba.conf
# 添加以下內容到文件尾部。
# TYPE DATABASE USER ADDRESS METHOD
host all all 0.0.0.0/0 md5
# 身份驗證方法(METHOD):
# - md5 密碼經過MD5加密后登陸到數據庫,一般采用選擇這種方式。
# - password 使用明文密碼登陸到數據庫。
# - trust 信任該主機,無需密碼即可登陸到數據庫。
# - ident 通過讀取"pg_ident.conf"文件里面具有系統用戶=數據庫用戶的映射關系,可以使用系統用戶登陸到
# 數據庫。
2.6、啟動服務
[root@localhost ~]# systemctl enable postgresql-12
[root@localhost ~]# systemctl start postgresql-12
[root@localhost ~]# systemctl status postgresql-12
● postgresql-12.service - PostgreSQL 12 database server
Loaded: loaded (/usr/lib/systemd/system/postgresql-12.service; enabled; vendor preset: disabled)
Active: active (running) since Thu 2020-03-05 08:22:38 EST; 5s ago
Docs: https://www.postgresql.org/docs/12/static/
[root@localhost ~]# netstat -lnupt |grep postmaster
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN 35219/postmaster
tcp6 0 0 :::5432 :::* LISTEN 35219/postmaster
2.7、配置環境變量
配置環境變量,使“psql”客戶端命令可以再全局使用。
[root@localhost ~]# vi /etc/profile
# PostgreSQL
export POSTGRESQL_BIN="/usr/pgsql-12/bin/"
export PATH=$PATH:$POSTGRESQL_BIN
[root@localhost ~]# source /etc/profile
2.8、查看數據庫版本
切換操作用戶“postgres”,“postgres”用戶是PostgreSQL的超級用戶。
[root@localhost ~]# sudo -i -u postgres
-bash-4.2$ psql
psql (12.2)
Type "help" for help.
postgres=# SELECT version();
version
-------------------------------------------------------------------------------------------------------
PostgreSQL 12.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit
(1 row)
2.9、查看數據庫列表
postgres=# select pg_database.datname from pg_database;
datname
-----------
postgres
template1
template0
2.10、修改"postgres"用戶密碼
默認情況下"postgres"用戶沒有密碼,我們需要給超級管理員一個密碼。
postgres=# \password
Enter new password: XXX
Enter it again: XXX
postgres=# exit
-bash-4.2$ exit
3、創建一個數據庫"sonarqube"
postgres=# exit
-bash-4.2$ createdb sonarqube
4、創建一個數據庫用戶"sonarqube"並授權
sonarqube=# CREATE USER sonarqube WITH PASSWORD 'abc-123';
sonarqube=# GRANT all ON all tables in SCHEMA PUBLIC to sonarqube;
5、連接數據庫"sonarqube"測試
sonarqube=# exit
-bash-4.2$ exit
logout
[root@localhost ~]# PGPASSWORD='abc-123' psql --host='172.16.254.129' --port='5432' --username='sonarqube' sonarqube
sonarqube=>
6、安裝依賴
[root@localhost ~]# yum -y install java-11-openjdk
7、安裝Sonarqube
7.1、下載Sonarqube
[root@localhost ~]# wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.2.0.32929.zip
7.2、解壓包到指定目錄
[root@localhost ~]# unzip sonarqube-8.2.0.32929.zip -d /opt
7.3、修改Sonarqube配置
[root@localhost ~]# vi /opt/sonarqube-8.2.0.32929/conf/sonar.properties
sonar.jdbc.username=sonarqube
sonar.jdbc.password=abc-123
sonar.jdbc.url=jdbc:postgresql://172.16.254.129/sonarqube
sonar.web.host=0.0.0.0
sonar.web.port=9000
7.4、創建運行用戶並授權程序目錄
Sonarqube不允許使用root用戶運行,所以我們需要創建一個運行用戶。
[root@localhost ~]# useradd sonarqube
[root@localhost ~]# chown -R sonarqube.sonarqube /opt/sonarqube-8.2.0.32929/
7.5、啟動Sonarqube服務
可以先使用“sonar.sh console”命令測試運行下,若有問題可以通過錯誤信息進行排錯。
[root@localhost ~]# sudo -u sonarqube /opt/sonarqube-8.2.0.32929/bin/linux-x86-64/sonar.sh console
jvm 1 | 2020.03.10 05:42:14 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up
jvm 1 | 2020.03.10 05:42:14 INFO app[][o.s.a.SchedulerImpl] SonarQube is up
[root@localhost ~]# sudo -u sonarqube /opt/sonarqube-8.2.0.32929/bin/linux-x86-64/sonar.sh start
[root@localhost ~]# netstat -lnupt |grep 9000
3tcp6 0 0 :::9000 :::* LISTEN 19957/java
8、使用瀏覽器訪問到Sonarqube服務端WEB管理控制台
在瀏覽器中輸入“http://Server_IP:Port”。
然后點擊登陸,默認管理員賬號:admin,密碼:admin。


9、設置Sonarqube管理控制台為簡體中文
點擊菜單欄”Administration“--->"Marketplace",然后再搜素框輸入"Chinese",然后選擇中文插件,安裝它,重啟Sonarqube服務即可。



