:::warning 开头警告⚠️: 看似简单的安装,浪费了我大量的时间.有些坑 :::

:::success 更新说明:
java : 1.8
postgres: 9.6 or mysql 5.7 5.8 sonarqube:6.7 ::: :::success 的环境 也是可以适用于本文章的, 建议更建议 使用 此版本的环境,符合生产环境:::

1 文档环境

1.1 系统环境

  1. [root@jenkins ~]# cat /etc/redhat-release
  2. CentOS Linux release 7.6.1810 (Core)
  3. [root@jenkins ~]# uname -r
  4. 3.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

https://www.postgresql.org/download/linux/redhat/

3️⃣ java: openjdk 11.0.4 使用centos7 yum 安装的

  1. yum install java-11-openjdk.x86_64

:::danger 由于上述三种软件都是采用 最新版本 ,生产环境可能使用率会非常小,不过踩得坑都是差不多的.所以就算使用之前的版本也是可以的 比如
java : 1.8
postgres: 9.6 or mysql 5.7 5.8
sonarqube:6.7
这种版本才更实用 :::

2 安装

下载并解压

  1. [root@jenkins ~]# unzip sonarqube-7.9.1.zip
  2. [root@jenkins ~]# ll
  3. total 357540
  4. -rw-------. 1 root root 1358 Aug 24 21:26 anaconda-ks.cfg
  5. drwxr-xr-x. 11 sonar sonar 141 Apr 16 15:48 sonarqube-6.7.7
  6. -rw-r--r--. 1 root root 159921852 Aug 25 01:22 sonarqube-6.7.7.zip
  7. drwxr-xr-x. 11 root root 141 Jul 10 12:32 sonarqube-7.9.1
  8. -rw-r--r--. 1 root root 206192630 Aug 25 00:42 sonarqube-7.9.1.zip
  9. [root@jenkins ~]#

将文件夹传到/usr/share下面

  1. [root@jenkins ~]# cp sonarqube-7.9.1 /opt/sonarqube-7.9.1 -r # 复制到opt下
  2. [root@jenkins ~]# ln -s /opt/sonarqube-7.9.1 /opt/sonarqube # 软连接

创建一个用户

  1. [root@jenkins ~]# useradd sonar
  2. [root@jenkins ~]# passwd sonar # 同名
  3. Changing password for user sonar.
  4. New password:
  5. BAD PASSWORD: The password is shorter than 8 characters
  6. Retype new password:
  7. passwd: all authentication tokens updated successfully.
  8. [root@jenkins ~]#

修改用户和用户组

  1. [root@jenkins ~]# chown -R sonar:sonar /opt/sonarqube-7.9.1
  2. [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]

配置如下:

  1. echo "vm.max_map_count=262144" > /etc/sysctl -p.conf
  2. sysctl -p
  3. vi /etc/security/limits.conf
  4. * soft nofile 65536
  5. * hard nofile 131072
  6. * soft nproc 2048
  7. * hard nproc 4096

配置好了 需要重新登录终端,

记一次环境变量导致的elasticsearch启动错误:

配置数据库

有一个地方需要注意, 安装好数据库软件后 需要初始化 数据库 以下命令就是

/usr/pgsql-11/bin/postgresql-11-setup initdb

但这不是重点 ,初始化好了后 需要配置一下

  1. vim /var/lib/pgsql/11/data/pg_hba.conf # 11为当前的版本 根据你的版本自己找这个文件

进行如下编辑

找到这个位置的文字 并将 peer 改成 trust idnet 改成 md5. 别问我为什么 ,至少现在不知道,反正不改 sonar 就是连不上. 其实就是登陆验证的问题

可以参考: https://yq.aliyun.com/articles/55898 PostgreSQL ident和peer基于操作系统用户的认证

  1. # TYPE DATABASE USER ADDRESS METHOD
  2. # "local" is for Unix domain socket connections only
  3. local all all peer
  4. # IPv4 local connections:
  5. host all all 127.0.0.1/32 ident
  6. # IPv6 local connections:
  7. host all all ::1/128 ident

修改后如下:

  1. # TYPE DATABASE USER ADDRESS METHOD
  2. # "local" is for Unix domain socket connections only
  3. local all all trust
  4. # IPv4 local connections:
  5. host all all 127.0.0.1/32 md5
  6. # IPv6 local connections:
  7. host all all ::1/128 md5

然后 启动 postgres

systemctl enable postgresql-11 systemctl start postgresql-11

配置sonar

  1. [root@jenkins ~]# vim /opt/sonarqube/conf/sonar.properties
  2. # 修改成如下:
  3. sonar.jdbc.username=sonar
  4. sonar.jdbc.password=sonar
  5. #----- PostgreSQL 9.3 or greater
  6. # By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
  7. sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonar # 默认端口就是5432 不加也可以 后面是数据库名称
  8. # By default, ports will be used on all IP addresses associated with the server.
  9. sonar.web.host=0.0.0.0 # 默认就是 不改也罢
  10. # Web context. When set, it must start with forward slash (for example /sonarqube).
  11. # The default value is root context (empty value).
  12. #sonar.web.context=
  13. # TCP port for incoming HTTP connections. Default value is 9000.
  14. sonar.web.port=9000 # 默认就是 不改也罢

然后就可以起服务了…

如果以上步骤你都做了, 按理说 是可以起来的

需要注意的是 要用 sonar 用户去起服务, 而不是 root

  1. [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

可以参考:

https://docs.sonarqube.org/latest/setup/operate-server/

官方提供了两种方式 ,我选择 systemd的方式
脚本如下

  1. [root@jenkins sonarqube]# vim /etc/systemd/system/sonar.service
  2. ExecStatus=/opt/sonarqube/bin/linux-x86-64/sonar.sh status
  3. ExecRestart=/opt/sonarqube/bin/linux-x86-64/sonar.sh restart
  4. [Unit]
  5. Description=SonarQube service
  6. After=syslog.target network.target
  7. [Service]
  8. Type=forking
  9. ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
  10. ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
  11. User=sonar
  12. Group=sonar
  13. Restart=always
  14. [Install]
  15. WantedBy=multi-user.target

这是 init.d 的方式

  1. [sonar@jenkins ~]$ sudo vim /etc/init.d/sonar
  2. [sudo] password for sonar:
  3. #!/bin/sh
  4. #
  5. #rc file for SonarQube
  6. #
  7. # chkconfig: 345 96 10
  8. #
  9. /usr/bin/sonar $*

至此 安装并启动完毕

image.png

中文插件自己搞吧

参考:

严重感谢:

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