Sonarqube是什么
Sonar是一个用于代码质量管理的开源平台。通过插件机制,Sonar 可以集成不同的测试工具,代码分析工具,以及持续集成工具,比如Jenkins。通过不同的插件对这些结果进行再加工处理,通过量化的方式度量代码质量的变化,从而可以方便地对不同规模和种类的工程进行代码质量管理。同时 Sonar 还对大量的持续集成工具提供了接口支持,可以很方便地在持续集成中使用 Sonar。
准备环境
准备一台联网的机器(我以centos7为例)
由于新版本的Sonarqube不再支持MySQL我们用开源的postgresql数据库来进行安装部署
- 从官网下载sonarqube的安装包并上传到服务器
- 安装openjdk-11
sudo yum install java-11-openjdk -y
3. 安装数据库postgresql-11
由于12发布较晚我们采用稳定版本的11进行配置
使用yum进行安装postgresql-11-server
yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install postgresql11-server
/usr/pgsql-12/bin/postgresql-11-setup initdb
systemctl enable postgresql-11
systemctl start postgresql-11
4.配置postgresql数据库信息
登陆数据库配置用户
su - postgres
-bash-4.2$
登陆数据库
-bash-4.2$ psql -U postgres
创建用户和数据库并授权
create user sonarqube with password '000000';
create database sonarqube owner sonarqube;
grant all on database sonarqube to sonarqube;
create schema my_schema;
ALTER USER postgres with encrypted password '123456';
退出数据库
\q
开启数据库远程访问
vi /var/lib/pgsql-11/data/postgresql.conf
将 #listen_addresses = 'localhost'
改为 listen_addresses = '*'
vi /var/lib/pgsql-11/data/pg_hba.conf
在末尾添加
host all all 0.0.0.0/0 md5
重启数据库
systemctl restart postgresql-11
5.配置用户
由于sonarqube不能使用root用户进行启动,所以我们需要普通用户运行。
useradd sonar
将用户的软硬限制调大
vi /etc/security/limits.conf
追加
sonar soft nofile 65535
sonar hard nofile 65537
用户拥有的内存权限提高
vi /etc/sysctl.conf
追加
vm.max_map_count=262144
6.关闭防火墙以及selinux
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
部署sonarqube
解压sonar的安装包
unzip sonarqube-8.3.1.34397.zip -d /opt
配置sonar
vi /opt/sonarqube-8.3.1.34397/conf/sonar.properties
去除以下内容 # 并配置
sonar.jdbc.username=sonarqube
sonar.jdbc.password=000000
sonar.jdbc.url=jdbc:postgresql://192.168.200.136/sonarqube?currentSchema=my_schema
sonar.jdbc.url=jdbc:postgresql://192.168.200.136/sonarqube
sonar.web.host=0.0.0.0
sonar.web.port=9000
vi /opt/sonarqube-8.3.1.34397/
去除以下内容 # 并配置
network.host: 192.168.200.136(本机IP)
http.port: 9200
赋予普通用户sonar权限
chmod -R 777 /opt/sonarqube-8.3.1.34397
chown -R sonar:sonar /opt/sonarqube-8.3.1.34397
启动sonar程序
su - sonar
bash /opt/sonarqube-8.3.1.34397/bin/linux-x86-64/sonar.sh start
查看端口情况
netstat -ntpl
tcp 0 0 127.0.0.1:32000 0.0.0.0:* LISTEN 16302/java
tcp6 0 0 :::9000 :::* LISTEN 16426/java
tcp6 0 0 127.0.0.1:9001 :::* LISTEN 16330/java
tcp6 0 0 127.0.0.1:56694 :::* LISTEN 16524/java
打开浏览器访问http://192.168.200.136:9000登陆用户名和密码均为admin