:::warning 开头警告⚠️: 看似简单的安装,浪费了我大量的时间.有些坑 :::
:::success
更新说明:
java : 1.8
postgres: 9.6 or mysql 5.7 5.8
sonarqube:6.7 :::
:::success
的环境 也是可以适用于本文章的, 建议更建议 使用 此版本的环境,符合生产环境:::
1 文档环境
1.1 系统环境
[root@jenkins ~]# cat /etc/redhat-releaseCentOS Linux release 7.6.1810 (Core)[root@jenkins ~]# uname -r3.10.0-957.el7.x86_64
:::warning 注意: 别忘了关闭防火墙 和seliux ,不然你会发现都对呀怎么就不成功呢! :::
1.2 软件环境
1️⃣ SonarQube :7.9 官方下载
地址如下:
https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.1.zip
2️⃣ 数据库: postgresql-11 官方安装
由于sonar 7.9 版本 官方不建议 以后也不打算支持mysql 了 所以根据官方推荐安装 postgresql
3️⃣ java: openjdk 11.0.4 使用centos7 yum 安装的
yum install java-11-openjdk.x86_64
:::danger
由于上述三种软件都是采用 最新版本 ,生产环境可能使用率会非常小,不过踩得坑都是差不多的.所以就算使用之前的版本也是可以的 比如
java : 1.8
postgres: 9.6 or mysql 5.7 5.8
sonarqube:6.7
这种版本才更实用
:::
2 安装
下载并解压
[root@jenkins ~]# unzip sonarqube-7.9.1.zip[root@jenkins ~]# lltotal 357540-rw-------. 1 root root 1358 Aug 24 21:26 anaconda-ks.cfgdrwxr-xr-x. 11 sonar sonar 141 Apr 16 15:48 sonarqube-6.7.7-rw-r--r--. 1 root root 159921852 Aug 25 01:22 sonarqube-6.7.7.zipdrwxr-xr-x. 11 root root 141 Jul 10 12:32 sonarqube-7.9.1-rw-r--r--. 1 root root 206192630 Aug 25 00:42 sonarqube-7.9.1.zip[root@jenkins ~]#
将文件夹传到/usr/share下面
[root@jenkins ~]# cp sonarqube-7.9.1 /opt/sonarqube-7.9.1 -r # 复制到opt下[root@jenkins ~]# ln -s /opt/sonarqube-7.9.1 /opt/sonarqube # 软连接
创建一个用户
[root@jenkins ~]# useradd sonar[root@jenkins ~]# passwd sonar # 同名Changing password for user sonar.New password:BAD PASSWORD: The password is shorter than 8 charactersRetype new password:passwd: all authentication tokens updated successfully.[root@jenkins ~]#
修改用户和用户组
[root@jenkins ~]# chown -R sonar:sonar /opt/sonarqube-7.9.1[root@jenkins ~]# chown -R sonar:sonar /opt/sonarqube
:::info
以下步骤假设 数据库 和 java中安装好了 数据库也 新建了用户和数据库 ,数据安装和配置请参考官方文档
这里我们使用 数据库用户为 sonar 数据库名称为 sonar 密码 也是 sonar
:::
配置
首先要配置系统环境
不配置就报错
(1) max file descriptors [4096] for elasticsearch process is too low, increase to at least [65536] (2) max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
配置如下:
echo "vm.max_map_count=262144" > /etc/sysctl -p.confsysctl -pvi /etc/security/limits.conf* soft nofile 65536* hard nofile 131072* soft nproc 2048* hard nproc 4096
配置好了 需要重新登录终端,
配置数据库
有一个地方需要注意, 安装好数据库软件后 需要初始化 数据库 以下命令就是
/usr/pgsql-11/bin/postgresql-11-setup initdb
但这不是重点 ,初始化好了后 需要配置一下
vim /var/lib/pgsql/11/data/pg_hba.conf # 11为当前的版本 根据你的版本自己找这个文件
进行如下编辑
找到这个位置的文字 并将 peer 改成 trust idnet 改成 md5. 别问我为什么 ,至少现在不知道,反正不改 sonar 就是连不上. 其实就是登陆验证的问题
可以参考: https://yq.aliyun.com/articles/55898 PostgreSQL ident和peer基于操作系统用户的认证
# TYPE DATABASE USER ADDRESS METHOD# "local" is for Unix domain socket connections onlylocal all all peer# IPv4 local connections:host all all 127.0.0.1/32 ident# IPv6 local connections:host all all ::1/128 ident
修改后如下:
# TYPE DATABASE USER ADDRESS METHOD# "local" is for Unix domain socket connections onlylocal all all trust# IPv4 local connections:host all all 127.0.0.1/32 md5# IPv6 local connections:host all all ::1/128 md5
然后 启动 postgres
systemctl enable postgresql-11 systemctl start postgresql-11
配置sonar
[root@jenkins ~]# vim /opt/sonarqube/conf/sonar.properties# 修改成如下:sonar.jdbc.username=sonarsonar.jdbc.password=sonar#----- PostgreSQL 9.3 or greater# By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonar # 默认端口就是5432 不加也可以 后面是数据库名称# By default, ports will be used on all IP addresses associated with the server.sonar.web.host=0.0.0.0 # 默认就是 不改也罢# Web context. When set, it must start with forward slash (for example /sonarqube).# 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 # 默认就是 不改也罢
然后就可以起服务了…
如果以上步骤你都做了, 按理说 是可以起来的
需要注意的是 要用 sonar 用户去起服务, 而不是 root
[root@jenkins sonarqube]# ./bin/linux-x86-64/sonar.sh start
然后就看 日志吧
es.log 节选:
2019.08.25 04:19:50 INFO es[][o.e.n.Node] started 2019.08.25 04:19:50 INFO es[][o.e.g.GatewayService] recovered [7] indices into cluster_state #这下面没报错 基本服务就起来了, 我都是在这边就 直接停止了……… 2019.08.25 04:19:51 INFO es[][o.e.c.r.a.AllocationService] Cluster health status changed from [RED] to [GREEN] (reason: [shards started [[metadatas][0]] …]). 2019.08.25 04:19:58 INFO es[][o.e.c.m.MetaDataDeleteIndexService] [users/QNDb-NvyS7GX93gk951sKA] deleting index 2019.08.25 04:19:59 INFO es[][o.e.c.m.MetaDataDeleteIndexService] [issues/7nzw-vvGRKqmGDMpWc48XQ] deleting index 2019.08.25 04:19:59 INFO es[][o.e.c.m.MetaDataDeleteIndexService] [rules/A5J2cIbsTPKy0wPP4xsPrg] deleting index
2019.08.25 04:20:01 INFO es[][o.e.c.m.MetaDataDeleteIndexService] [components/fQMRehO8RliBjqXR-QI_FQ] deleting index
2019.08.25 04:20:01 INFO es[][o.e.c.m.MetaDataDeleteIndexService] [projectmeasures/y9lPA8v-QfiVv78DttACZw] deleting index
2019.08.25 04:20:01 INFO es[][o.e.c.m.MetaDataDeleteIndexService] [views/OCaEnDSwQCSA4ijWcb0RnA] deleting index
2019.08.25 04:20:02 INFO es[][o.e.c.m.MetaDataCreateIndexService] [components] creating index, cause [api], templates [], shard
附加操作
就是做一些 启动脚本 或者 添加个 开机自启动, 还就就是做 web代理 可以使用 nginx 或者 Apache
可以参考:
官方提供了两种方式 ,我选择 systemd的方式
脚本如下
[root@jenkins sonarqube]# vim /etc/systemd/system/sonar.serviceExecStatus=/opt/sonarqube/bin/linux-x86-64/sonar.sh statusExecRestart=/opt/sonarqube/bin/linux-x86-64/sonar.sh restart[Unit]Description=SonarQube serviceAfter=syslog.target network.target[Service]Type=forkingExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh startExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stopUser=sonarGroup=sonarRestart=always[Install]WantedBy=multi-user.target
这是 init.d 的方式
[sonar@jenkins ~]$ sudo vim /etc/init.d/sonar[sudo] password for sonar:#!/bin/sh##rc file for SonarQube## chkconfig: 345 96 10#/usr/bin/sonar $*
至此 安装并启动完毕

中文插件自己搞吧
参考:
严重感谢:
https://www.vultr.com/docs/how-to-install-sonarqube-on-centos-7#Step_1__Perform_a_system_update
此文章最为详实, 其中 修改 postgres 配置才是 重点 ,也是我成功安装并运行sonar的关键
其他:
https://docs.sonarqube.org/latest/setup/install-server/
https://www.postgresql.org/download/linux/redhat/
https://stackoverflow.com/questions/30087416/sonarqube-could-not-connect-to-postgresql
https://blog.csdn.net/fuyuwei2015/article/details/73195936 Centos7下安装与卸载Jdk1.8
https://mozillazg.com/2014/06/hello-postgresql.html [database] postgresql 常用操作
https://dunterov.github.io/sq-psql/ Install Sonarqube with PostgreSQL
