Linux下SonarQube+Runner+Scanner离线安装
(一)、创建sonar数据库
#创建sonar数据库 mysql> create user 'sonar'@'%' identified by 'yzt12345'; Query OK, 0 rows affected (0.00 sec) mysql>create database sonarqube; Query OK, 1 row affected (0.00 sec) mysql>grant all privileges on sonarqube.* TO 'sonar'@'%' identified by 'yzt12345'; Query OK, 0 rows affected, 1 warning (0.00 sec) mysql>flush privileges; Query OK, 0 rows affected (0.00 sec) |
(二)、安装SonarQube
第一步:下载
sonarqube下载地址: http://www.sonarqube.org/downloads/
sonar-runner下载地址: http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
sonar-scanner下载扫描器地址: https://docs.sonarqube.org/display/SCAN/Analyzing+with+SonarQube+Scanner
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.6.zip wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-3.3.0.1492-linux.zip unzip sonarqube-6.7.6.zip unzip sonar-runner-dist-2.4.zip unzip sonar-scanner-cli-3.3.0.1492-linux.zip mv sonarqube-6.7.6 /usr/local/sonarqube mv sonar-runner-2.4 /usr/local/sonar-runner mv sonar-scanner-3.3.0.1492-linux /usr/local/sonar-scanner |
第二步:配置环境变量
[root@qsh local]# vim /etc/profile #-------------------------------------------------------------------- export SONAR_HOME=/usr/local/sonarqube export SONAR_RUNNER_HOME=/usr/local/sonar-runner export SONAR_SCANNER_HOME=/usr/local/sonar-scanner PATH=$PATH:$SONAR_HOME/bin:$SONAR_RUNNER_HOME/bin:$SONAR_SCANNER_HOME/bin [root@qsh local]# source /etc/profile |
第三步:配置sonar.properties
[root@Nginx-Jenkins-Server sonarqube]# cd /usr/local/sonarqube/conf/ [root@Nginx-Jenkins-Server conf]# vim sonar.properties #修改连接数据库用户密码 sonar.jdbc.username=sonar sonar.jdbc.password=yzt12345 #取消注释,修改数据库地址 sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false #取消下面两行注释 sonar.web.host=192.168.65.132 sonar.web.port=9000 |
sonarqube启动参数,修改配置: /usr/local/sonarqube/conf/wrapper.conf
ES启动参数,修改配置:/usr/local/sonarqube/elasticsearch/config/jvm.options
第四步:配置SonarQube Runner
[root@qsh conf]# cd /usr/local/sonar-runner/conf/ [root@qsh conf]# vim sonar-runner.properties #----- Default SonarQube server sonar.host.url=http://192.168.65.132:9000 #----- PostgreSQL #sonar.jdbc.url=jdbc:postgresql://localhost/sonar #----- MySQL sonar.jdbc.url=jdbc:mysql://127.0.0.1:3306/sonarqube?useUnicode=true&characterEncoding=utf8 #----- Oracle #sonar.jdbc.url=jdbc:oracle:thin:@localhost/XE #----- Microsoft SQLServer #sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/sonar;SelectMethod=Cursor #----- Global database settings sonar.jdbc.username=sonar sonar.jdbc.password=yzt12345 #----- Default source code encoding sonar.sourceEncoding=UTF-8 #----- Security (when 'sonar.forceAuthentication' is set to 'true') sonar.login=admin sonar.password=admin |
[root@qsh sonar-runner]# cd /usr/local/sonar-runner [root@qsh sonar-runner]# mkdir /data [root@qsh sonar-runner]# mkdir /data/sonar-runner [root@qsh sonar-runner]# vim sonar-project.properties #------------------------------------------------- #Required metadata #对于较大的工程项目,经常出现内存不够的问题,需要利用环境变量设置虚拟机的内存 SONAR_RUNNER_OPTS=-Xmx512m-XX:MaxPermSize=128m sonar.projectKey=my:project sonar.projectName=yaok sonar.projectVersion=1.0 #Paths to source directories. #Paths are relative to the sonar-project.properties file. Replace "\" by "/" on Windows. #Do not put the "sonar-project.properties" file in the same directory with the source code. #(i.e. never set the "sonar.sources" property to ".") sonar.sources=/data/sonar-runner #The value of the property must be the key of the language. sonar.language=java #Encoding of the source code sonar.sourceEncoding=UTF-8 #Additional parameters sonar.my.property=value |
第五步:安装后启动SonarQube
用root无法启动lSonarQube,需要另外新建普通用户来启动
[root@qsh ~]# useradd esadmin [root@qsh ~]# chown -R esadmin.esadmin /usr/local/sonarqube [root@qsh ~]# vim /usr/local/sonarqube/elasticsearch/config/elasticsearch.yml |
//开启以下两行
//切换用户,启动
[root@qsh ~]# su - esadmin [esadmin@qsh ~]$ cd /usr/local/sonarqube/ [esadmin@qsh sonarqube]$ ./bin/linux-x86-64/sonar.sh start Starting SonarQube... Started SonarQube. |
//检查是否正常启动
[esadmin@qsh sonarqube]$ ps -aux |grep sonar |
//启动日志查看
#sonarqube
tail -f /usr/local/sonarqube/logs/sonar.log |
#Es
tail -f /usr/local/sonarqube/logs/es.log |
附:启动的时候报错
1,错误:max file descriptors [4096] for elasticsearch process is too low, increase to at least
解决方法:
[root@qsh ]#vim /etc/security/limits.conf #在文件最后添加下面四行 * soft nofile 65536 * hard nofile 65536 * soft nproc 4096 * hard npro 4096 #查看文件限制命令 [esadmin@qsh sonarqube]$ ulimit -Hn 655350 |
2,错误,max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
解决方法:
切换到root用户修改配置sysctl.conf [root@qsh]#vim /etc/sysctl.conf #添加下面配置: vm.max_map_count=655360 并执行命令: [root@qsh]# sysctl -p |
第六步:安装后启动Sonar-Runner
[root@qsh sonar-runner]$ cd /usr/local/sonar-runner [root@qsh sonar-runner]$ ./bin/sonar-runner -e -X |
如果能够看到下面的输出信息,证明你的SonarQube Runner安装并配置正确啦。
问题:sonarQube环境搭建过程中报错:ERROR web[][o.a.c.h.Http11NioProtocol] Failed to initialize end point associated with ProtocolHandler [“http-nio-192.168.X.X-9000”]
java.net.BindException: Cannot assign requested address
解决:使用127.0.0.1,启动没问题,说明IP不对,使用命令ip addr 查询实际IP,即可成功启动。
如果是报已经被绑定,说明是进程没有被真正关掉
使用netstat -ano|findstr 9000命令查看,查到PID后,再kill
第七步:使用浏览器就可以直接登陆
http://192.168.65.132:9000/about
登录:admin/admin
//安装中文语言包
选择语言包,点击安装,安装好后重启,重新进入就是汉化版了
插件安装位置: /usr/local/sonarqube/extensions/plugins/
第八步:配置SonarQube Scanner代码扫描工具
[root@qsh sonar-scanner]# cd /usr/local/sonar-scanner/conf/ [root@Nginx-Jenkins-Server conf]# vim sonar-scanner.properties #Configure here general information about the environment, such as SonarQube server connection details for example #No information about specific project should appear here #----- Default SonarQube server #sonar.host.url=http://localhost:9000 sonar.host.url=http://192.168.65.132:9000
#----- Default source code encoding #sonar.sourceEncoding=UTF-8 sonar.sourceEncoding=UTF-8 |
# 拉取sonar示例代码
[root@qsh ]# mkdir /data/sonar-examples [root@qsh ]# cd /data/sonar-examples [root@qsh sonar-examples]# git clone https://github.com/SonarSource/sonar-scanning-examples.git |
#在项目的根目录中创建配置文件: sonar-project.properties
[root@qsh sonar-examples]# cd sonar-scanning-examples/sonarqube-scanner-maven/ [root@qsh sonarqube-scanner-maven]# vim sonar-project.properties #------------------------------------------------------------------ # must be unique in a given SonarQube instance sonar.projectKey=devops-demo # this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1. sonar.projectName=devops-demo sonar.projectVersion=1.0 # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. # This property is optional if sonar.modules is set. sonar.sources=. sonar.java.binaries=. # Encoding of the source code. Default is default system encoding sonar.sourceEncoding=UTF-8 |
#代码扫描测试
[root@qsh sonarqube-scanner-maven]# sonar-scanner |
//使用浏览器打开 http://192.168.65.132:9000/projects ,查看扫描结果
注释:手工测试时,可以直接把自己的程序包源码文件夹直接拷贝到sonar-scanning-examples/sonarqube-scanner-maven/下,然后,直接执行
[root@qsh sonarqube-scanner-maven]# sonar-scanner |