sonar6.7.5的安装和配置
摘要
代码分析软件sonar的安装配置及升级过程
1、安装
- 下载
#选择长周期支持的版本6.7.5 wget -c
- 备份和停止原4.0
cp -r sonar/ sonar.4.0 service sonar stop
- 安装数据库
原来4.0使用的mysql已经不建议使用了,本次使用postgresql。
[root@xgit opt]# cat /etc/centos-release CentOS release 6.5 (Final) [root@xgit opt]# yum install Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.yun-idc.com * extras: mirrors.nwsuaf.edu.cn * updates: mirrors.aliyun.com Setting up Install Process pgdg-redhat96-9.6-3.noarch.rpm | 4.7 kB 00:00 Examining /var/tmp/yum-root-961sdj/pgdg-redhat96-9.6-3.noarch.rpm: pgdg-redhat96-9.6-3.noarch Marking /var/tmp/yum-root-961sdj/pgdg-redhat96-9.6-3.noarch.rpm to be installed Resolving Dependencies --> Running transaction check ---> Package pgdg-redhat96.noarch 0:9.6-3 will be installed --> Finished Dependency Resolution Dependencies Resolved =========================================================================================================== Package Arch Version Repository Size =========================================================================================================== Installing: pgdg-redhat96 noarch 9.6-3 /pgdg-redhat96-9.6-3.noarch 2.7 k Transaction Summary =========================================================================================================== Install 1 Package(s) Total size: 2.7 k Installed size: 2.7 k Is this ok [y/N]: y Downloading Packages: Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : pgdg-redhat96-9.6-3.noarch 1/1 Verifying : pgdg-redhat96-9.6-3.noarch 1/1 Installed: pgdg-redhat96.noarch 0:9.6-3 Complete! #安装客户端和服务端 [root@xgit opt]# yum install postgresql96 yum install postgresql96-server Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.yun-idc.com * extras: mirrors.nwsuaf.edu.cn * updates: mirrors.aliyun.com pgdg96 | 4.1 kB 00:00 pgdg96/primary_db | 207 kB 00:00 Setting up Install Process ^C[root@xgit opt]# yum install postgresql96 postgresql96-server -y Loaded plugins: fastestmirror, security Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * epel: mirrors.yun-idc.com * extras: mirrors.nwsuaf.edu.cn * updates: mirrors.aliyun.com Setting up Install Process Resolving Dependencies --> Running transaction check ---> Package postgresql96.x86_64 0:9.6.10-1PGDG.rhel6 will be installed --> Processing Dependency: postgresql96-libs(x86-64) = 9.6.10-1PGDG.rhel6 for package: postgresql96-9.6.10-1PGDG.rhel6.x86_64 ---> Package postgresql96-server.x86_64 0:9.6.10-1PGDG.rhel6 will be installed --> Running transaction check ---> Package postgresql96-libs.x86_64 0:9.6.10-1PGDG.rhel6 will be installed --> Finished Dependency Resolution Dependencies Resolved =========================================================================================================== Package Arch Version Repository Size =========================================================================================================== Installing: postgresql96 x86_64 9.6.10-1PGDG.rhel6 pgdg96 1.4 M postgresql96-server x86_64 9.6.10-1PGDG.rhel6 pgdg96 5.0 M Installing for dependencies: postgresql96-libs x86_64 9.6.10-1PGDG.rhel6 pgdg96 289 k Transaction Summary =========================================================================================================== Install 3 Package(s) Total download size: 6.8 M Installed size: 27 M Downloading Packages: (1/3): postgresql96-9.6.10-1PGDG.rhel6.x86_64.rpm | 1.4 MB 00:33 (2/3): postgresql96-libs-9.6.10-1PGDG.rhel6.x86_64.rpm | 289 kB 00:02 (3/3): postgresql96-server-9.6.10-1PGDG.rhel6.x86_64.rpm | 5.0 MB 01:24 ----------------------------------------------------------------------------------------------------------- Total 57 kB/s | 6.8 MB 02:01 Running rpm_check_debug Running Transaction Test Transaction Test Succeeded Running Transaction Installing : postgresql96-libs-9.6.10-1PGDG.rhel6.x86_64 1/3 Installing : postgresql96-9.6.10-1PGDG.rhel6.x86_64 2/3 Installing : postgresql96-server-9.6.10-1PGDG.rhel6.x86_64 3/3 Verifying : postgresql96-libs-9.6.10-1PGDG.rhel6.x86_64 1/3 Verifying : postgresql96-server-9.6.10-1PGDG.rhel6.x86_64 2/3 Verifying : postgresql96-9.6.10-1PGDG.rhel6.x86_64 3/3 Installed: postgresql96.x86_64 0:9.6.10-1PGDG.rhel6 postgresql96-server.x86_64 0:9.6.10-1PGDG.rhel6 Dependency Installed: postgresql96-libs.x86_64 0:9.6.10-1PGDG.rhel6 Complete! [root@xgit opt]# service postgresql-9.6 initdb Initializing database: [ OK ] [root@xgit opt]# chkconfig postgresql-9.6 on [root@xgit opt]# service postgresql-9.6 start [root@xgit opt]# service postgresql-9.6 start Starting postgresql-9.6 service: [ OK ] [root@xgit opt]# java -version java version "1.8.0_131" Java(TM) SE Runtime Environment (build 1.8.0_131-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode) [root@xgit opt]#
- 安装web server
其实就是解压 unzip sonarqube-6.7.5.zip
-
对照 检查需求列表是否满足
-
硬件
2G Ram and 1G free Ram 15G Disk 高速读写磁盘
-
支持 java 8 postgresql 9.6 utf8
-
linux参数
sonarqube 不能以root运行 vm.maxmapcount >= 262144 fs.file-max >= 65536 jenkins open files >=65536; threads >= 2048 secomp 编译进内核
-
确认postgresql字符集
-
-bash-4.1$ psql psql (8.4.20, server 9.6.10) WARNING: psql version 8.4, server version 9.6. Some psql features might not work. Type "help" for help. postgres=# \l List of databases Name | Owner | Encoding | Collation | Ctype | Access privileges -----------+----------+----------+-------------+-------------+----------------------- postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres : postgres=CTc/postgres (3 rows)
- 确认linux内核参数
[root@xgit opt]# sysctl vm.max_map_count vm.max_map_count = 65530 [root@xgit opt]# sysctl -w vm.max_map_count=262144 vm.max_map_count = 262144 #修改/etc/sysctl.conf 增加 vm.max_map_count = 262144 [root@xgit opt]# sysctl fs.file-max fs.file-max = 1619009 [root@xgit opt]# ulimit -u 127306 [root@xgit opt]# ulimit -u 127306
- 修改/etc/security/limits.conf 增加下列参数
## sonar为运行sonar服务的系统账号 sonar hard nofile 65536 sonar soft nproc 4096 # 线程 sonar hard nproc 4096 # 进程
2、sonar服务的配置
- 1、sonar.property
## 连接postgresql的用户 sonar.jdbc.username=sonar ## 同上用户的密码 sonar.jdbc.password=yourpassword ## jdbc 连接,sonar为数据库名 sonar.jdbc.url=jdbc:postgresql://localhost/sonar ## sonar 服务器ip或者域名 sonar.web.host=192.168.x.x # The default value is root context (empty value). #sonar.web.context= # TCP port for incoming HTTP connections. Default value is 9000. #sonar.web.port=9000 #因是centos6关闭seccomp filter功能 sonar.search.javaAdditionalOpts=-Dbootstrap.system_call_filter=false
- 2、在postgresql内创建sonar用户和数据库
postgres=# create user sonarqube with password 'your_password' ; CREATE ROLE [root@xgit opt]# su - postgres -bash-4.1$ psql psql (8.4.20, server 9.6.10) postgres=# create database sonar owner sonar; CREATE DATABASE postgres=# grant all privileges on database sonar to sonar; GRANT
- 3、修改postgresql配置文件增加系统用户sonar本机访问数据库的权限
vi /var/lib/pgsql/9.6/data/pg_hba.conf ##添加下面一行数据 host sonar sonar 127.0.0.1/32 md5
3、启动sonar
- 切换用户
su - sonar
- 启动
cd /opt/sonarqube-6.7.5 bin/linux-x86-64/sonar.sh start
- 查看日志确认
logs/sonar.log内出现下面提示,应为正常启动
2018.11.14 20:16:17 INFO app[][o.s.a.es.EsSettings] Elasticsearch listening on /127.0.0.1:9001 2018.11.14 20:16:30 INFO app[][o.s.a.SchedulerImpl] Process[es] is up 2018.11.14 20:16:46 INFO app[][o.s.a.SchedulerImpl] Process[web] is up 2018.11.14 20:16:53 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up 2018.11.14 20:16:53 INFO app[][o.s.a.SchedulerImpl] SonarQube is up
- 配置服务运行脚本
#!/bin/sh ### BEGIN INIT INFO # Provides: sonar # Required-Start: $local_fs $remote_fs $network $time $named # Required-Stop: $local_fs $remote_fs $network $time $named # Default-Start: 3 5 # Default-Stop: 0 1 2 6 # Short-Description: Sonar # Description: Open platform to manage code quality ### END INIT INFO NAME=sonar SCRIPTNAME=/etc/init.d/$NAME if [ `id -u` -ne 0 ]; then echo "The $NAME init script can only be run as root" exit 1 fi MACHINE_TYPE=`getconf LONG_BIT` if [ "${MACHINE_TYPE}" = "64" ]; then JSW=/opt/sonar/bin/linux-x86-64/sonar.sh else JSW=/opt/sonar/bin/linux-x86-32/sonar.sh fi do_start() { su sonar -c "$JSW start" } do_stop() { su sonar -c "$JSW stop" } do_status() { su sonar -c "$JSW status" } do_restart() { su sonar -c "$JSW restart" } case "$1" in start) do_start ;; stop) do_stop ;; status) do_status ;; restart|force-reload) do_restart ;; *) echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2 exit 3 ;; esac exit 0
- centos6上启停命令
service sonar start/stop/restart
4、查看分析结果
- 登录web
浏览器访问
-
默认admin/admin
登录可进行系统和安全等配置
-
默认不登录可以查看jenkins传来的数据
在jenkinss上配置sonar
-
全局配置
-
SonarQube servers 增加一列,下面三项需配置
- name
- Server URL
- Server authentication token 如果sonar服务端开启匿名权限,可以不用token
-
-
各项目内配置
-
Execute SonarQube Scanner
- JDK :jdk1.8 #选择1.8版本
- SonarQube Scanner :选择3.2版本
- Path to project properties:sonar-project.properties #源码内的配置文件
-
Execute SonarQube Scanner
致谢参考