SonarQube是一种自动代码审查工具,可检测代码中的错误,漏洞和代码味道。它可以与您现有的工作流程集成,以实现跨项目分支和请求请求的连续代码检查
在https://www.sonarqube.org/downloads/选择版本,本文选择稳定版本7.9.1
1.jdk环境准备
此版本不支持jdk8,安装jdk11版本
[root@localhost local]# java -version
java version "11" 2018-09-25
Java(TM) SE Runtime Environment 18.9 (build 11+28)
Java HotSpot(TM) 64-Bit Server VM 18.9 (build 11+28, mixed mode)
2.安装数据库
准备PostgreSQL数据库,本文选择使用9.5
[root@localhost ~]# yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
[root@localhost ~]# yum -y install postgresql95
[root@localhost ~]# yum install postgresql95-server -y
[root@localhost ~]# /usr/pgsql-9.5/bin/postgresql95-setup initdb
[root@localhost ~]# systemctl enable postgresql-9.5
[root@localhost ~]# systemctl start postgresql-9.5
1.使用postgres用户登录(PostgresSQL安装后会自动创建postgres用户,无密码)
[root@jenkins2 ~]# su - postgres
-bash-4.2$
2.登录postgresql数据库
-bash-4.2$ psql -U postgres
psql (9.5.19)
Type "help" for help.
3.创建用户和数据库并授权
postgres=# create user sonarqube with password '123456';
CREATE ROLE
postgres=# create database sonarqube owner sonarqube;
CREATE DATABASE
postgres=# grant all on database sonarqube to sonarqube;
GRANT
postgres=# create schema my_schema;
CREATE SCHEMA
postgres=# ALTER USER postgres with encrypted password '123456';
ALTER ROLE
4.退出数据库
postgres=# \q
5.开启远程访问
-bash-4.2$ vi /var/lib/pgsql/9.5/data/postgresql.conf
将这行listen_addresses = 'localhost'
改为listen_addresses = '*'
-bash-4.2$ vi /var/lib/pgsql/9.5/data/pg_hba.conf
增加如下图所示的行
-bash-4.2$ logout
6.重启数据库
[root@localhost ~]# systemctl restart postgresql-9.5.service
可以在https://www.sonarqube.org/downloads/选择版本,本文选择稳定版本7.9.1
[root@localhost ~]# wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.1.zip
[root@localhost ~]# unzip sonarqube-7.9.1.zip
[root@localhost ~]# mv sonarqube-7.9.1 /usr/local/sonar
[root@localhost ~]# cd /usr/local/sonar
[root@localhost sonar]# ls
bin conf COPYING data elasticsearch extensions lib logs temp web
[root@localhost sonar]# vim conf/sonar.properties
[root@localhost sonar]# grep -Pv "^(#|$)" conf/sonar.properties
sonar.jdbc.username=sonarqube
sonar.jdbc.password=123456
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=my_schema
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
sonar.web.host=0.0.0.0
sonar.web.port=9002
[root@localhost ~]# wget http://repo1.maven.org/maven2/org/codehaus/sonar/runner/sonar-runner-dist/2.4/sonar-runner-dist-2.4.zip
[root@localhost ~]# unzip sonar-runner-dist-2.4.zip
[root@localhost ~]# mv sonar-runner-2.4/ /usr/local/sonar-runner
[root@localhost ~]# cd /usr/local/sonar-runner/
[root@localhost sonar-runner]# ls
bin conf lib
[root@localhost sonar-runner]# vim conf/sonar-runner.properties
[root@localhost sonar-runner]# grep -Pv "^(#|$)" conf/sonar-runner.properties
sonar.host.url=http://192.168.59.128:9002
sonar.jdbc.username=sonarqube
sonar.jdbc.password=123456
sonar.login=admin
sonar.password=admin
[root@localhost ~]# vim /etc/profile
[root@localhost ~]# tail -4 /etc/profile
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export TIME_STYLE='+%Y/%m/%d %H:%M:%S'
export SONAR_HOME=/usr/local/sonar
[root@localhost ~]# source /etc/profile
注意sonar不能用root用户来启动
创建新用户
[root@localhost ~]# useradd sonar
[root@localhost ~]# chown -R sonar.sonar /usr/local/sonar
[root@localhost ~]# chown -R sonar.sonar /usr/local/sonar-runner/
[root@localhost ~]# vim /usr/local/sonar/elasticsearch/config/elasticsearch.yml
[root@localhost ~]# grep -Pv "^(#|$)" /usr/local/sonar/elasticsearch/config/elasticsearch.yml
network.host: 192.168.8.176
http.port: 9200
[root@localhost ~]# su - sonar
[sonar@localhost ~]$ cd /usr/local/sonar
[sonar@localhost sonar]$ bin/linux-x86-64/sonar.sh start
Starting SonarQube...
Started SonarQube.
[sonar@localhost sonar]$ tail logs/sonar.log
2019.10.18 15:51:27 INFO app[][o.s.a.SchedulerImpl] Process[es] is up
2019.10.18 15:51:27 INFO app[][o.s.a.ProcessLauncherImpl] Launch process[[key='web', ipcIndex=2, logFilenamePrefix=web]] from [/usr/local/sonar]: /usr/local/jdk/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonar/temp --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.io=ALL-UNNAMED --add-opens=java.rmi/sun.rmi.transport=ALL-UNNAMED -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Dhttp.nonProxyHosts=localhost|127.*|[::1] -cp ./lib/common/*:/usr/local/sonar/lib/jdbc/postgresql/postgresql-42.2.5.jar org.sonar.server.app.WebServer /usr/local/sonar/temp/sq-process3958888579777298916properties
2019.10.18 15:51:41 INFO app[][o.s.a.SchedulerImpl] Process[web] is up
2019.10.18 15:51:41 INFO app[][o.s.a.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/usr/local/sonar]: /usr/local/jdk/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonar/temp --add-opens=java.base/java.util=ALL-UNNAMED -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Dhttp.nonProxyHosts=localhost|127.*|[::1] -cp ./lib/common/*:/usr/local/sonar/lib/jdbc/postgresql/postgresql-42.2.5.jar org.sonar.ce.app.CeServer /usr/local/sonar/temp/sq-process11850425573794699220properties
2019.10.18 15:51:41 INFO app[][o.s.a.SchedulerImpl] Process[web] is up
2019.10.18 15:51:41 INFO app[][o.s.a.ProcessLauncherImpl] Launch process[[key='ce', ipcIndex=3, logFilenamePrefix=ce]] from [/usr/local/sonar]: /usr/local/jdk/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonar/temp --add-opens=java.base/java.util=ALL-UNNAMED -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError -Dhttp.nonProxyHosts=localhost|127.*|[::1] -cp ./lib/common/*:/usr/local/sonar/lib/jdbc/postgresql/postgresql-42.2.5.jar org.sonar.ce.app.CeServer /usr/local/sonar/temp/sq-process11850425573794699220properties
2019.10.18 15:51:47 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up
2019.10.18 15:51:47 INFO app[][o.s.a.SchedulerImpl] SonarQube is up
2019.10.18 15:51:47 INFO app[][o.s.a.SchedulerImpl] Process[ce] is up
2019.10.18 15:51:47 INFO app[][o.s.a.SchedulerImpl] SonarQube is up
访问192.168.59.128:9002
账号密码都是admin
将sonarqube安装中文插件
到这sonarqube的安装就OK了
ERROR: [1] bootstrap checks failed
[1]: max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]
编辑 /etc/security/limits.conf,追加以下内容;
查看sonar日志和es日志都没问题,查看web日志,发现问题,数据库连接不上
2019.10.18 14:54:24 ERROR web[][o.s.s.p.Platform] Web server startup failed
java.lang.IllegalStateException: Fail to connect to database
at org.sonar.db.DefaultDatabase.start(DefaultDatabase.java:90)
at org.sonar.core.platform.StartableCloseableSafeLifecyleStrategy.start(StartableCloseableSafeLifecyleStrategy.java:40)
at org.picocontainer.injectors.AbstractInjectionFactory$LifecycleAdapter.start(AbstractInjectionFactory.java:84)
at org.picocontainer.behaviors.AbstractBehavior.start(AbstractBehavior.java:169)
at org.picocontainer.behaviors.Stored$RealComponentLifecycle.start(Stored.java:132)
at org.picocontainer.behaviors.Stored.start(Stored.java:110)
at org.picocontainer.DefaultPicoContainer.potentiallyStartAdapter(DefaultPicoContainer.java:1016)
at org.picocontainer.DefaultPicoContainer.startAdapters(DefaultPicoContainer.java:1009)
at org.picocontainer.DefaultPicoContainer.start(DefaultPicoContainer.java:767)
at org.sonar.core.platform.ComponentContainer.startComponents(ComponentContainer.java:135)
at org.sonar.server.platform.platformlevel.PlatformLevel.start(PlatformLevel.java:90)
at org.sonar.server.platform.platformlevel.PlatformLevel1.start(PlatformLevel1.java:160)
at org.sonar.server.platform.Platform.start(Platform.java:211)
at org.sonar.server.platform.Platform.startLevel1Container(Platform.java:170)
at org.sonar.server.platform.Platform.init(Platform.java:86)
at org.sonar.server.platform.web.PlatformServletContextListener.contextInitialized(PlatformServletContextListener.java:43)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4817)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5283)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1423)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1413)
at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at java.base/java.lang.Thread.run(Thread.java:834)
数据库连接有问题,修改重新启动