centos7-sonarqube-8.4.2安装
1. centos7-sonarqube-8.4.2安装
-
官网文档参考地址:https://docs.sonarqube.org/latest/requirements/requirements/
-
提示要求
使用 sonarqube 对 java 项目代码进行扫描的时候,java 项目的版本不能低于 sonar 的编译版本。
sonarqube:7.8-community 是 sonar 对 jdk1.8 的最后一个版本,从 7.9 以后 sonar 最低支持版本为 jdk 1.11 -
先关闭CentOS 7的自带防火墙和selinux服务
[root@sonarqube ~]# /bin/systemctl stop firewalld [root@sonarqube ~]# /bin/systemctl disable firewalld [root@sonarqube ~]# getenforce #查看selinux是否开启 Enforcing #enforcing表示selinux开启的, [root@sonarqube ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config #修改配置文件,需要重启才后永久关闭 [root@sonarqube ~]# setenforce 0 #临时关闭selinx [root@sonarqube ~]# getenforce #查看是否关闭 Disabled
-
Jdk11安装
-
下载jdk11安装包
-
安装wget
[root@sonarqube ~]# yum install wget
-
下载jdk11包
[root@sonarqube ~]# wget https://repo.huaweicloud.com/openjdk/11.0.2/openjdk-11.0.2_linux-x64_bin.tar.gz
-
解压安装包到指定目录
[root@sonarqube ~]# tar -zxvf openjdk-11.0.2_linux-x64_bin.tar.gz -C /usr/local
-
配置jdk环境变量
cat >> /etc/profile.d/java.sh << EOF #!/bin/bash export JAVA_HOME=/usr/local/jdk-11.0.2 export JRE_HOME=${JAVA_HOME} export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib export PATH=${JAVA_HOME}/bin:$PATH EOF
-
加载环境变量
[root@sonarqube ~]# source /etc/profile.d/java.sh
-
验证是否安装上了
[root@sonarqube ~]# java -version openjdk version "11.0.2" 2019-01-15 OpenJDK Runtime Environment 18.9 (build 11.0.2+9) OpenJDK 64-Bit Server VM 18.9 (build 11.0.2+9, mixed mode)
-
-
上面是二进制包安装jdk如果嫌麻烦用下面这种
[root@sonarqube ~]# yum install -y java-11-openjdk java-11-openjdk-devel
-
由于7.9后sonarqube不支持mysql数据库,需要使用postgresql来支持sonarqube服务
-
安装pgsql源
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
-
安装pgsql数据库
yum install -y postgresql13-serve
-
新建数据库数据存放目录
mkdir -p /data/pgsql chown -R postgres.postgres /data/pgsql
-
初始化数据库
初始化数据 -D指定初始化创建的数据库的文件路径
[root@sonarqube ~]# su postgres bash-4.2$ /usr/pgsql-13/bin/initdb -D /data/pgsql could not change directory to "/root": 权限不够 The files belonging to this database system will be owned by user "postgres". This user must also own the server process. The database cluster will be initialized with locale "zh_CN.UTF-8". The default database encoding has accordingly been set to "UTF8". initdb: could not find suitable text search configuration for locale "zh_CN.UTF-8" The default text search configuration will be set to "simple". Data page checksums are disabled. fixing permissions on existing directory /data/pgsql ... ok creating subdirectories ... ok selecting dynamic shared memory implementation ... posix selecting default max_connections ... 100 selecting default shared_buffers ... 128MB selecting default time zone ... Asia/Shanghai creating configuration files ... ok running bootstrap script ... ok performing post-bootstrap initialization ... ok syncing data to disk ... ok initdb: warning: enabling "trust" authentication for local connections You can change this by editing pg_hba.conf or using the option -A, or --auth-local and --auth-host, the next time you run initdb. Success. You can now start the database server using: /usr/pgsql-13/bin/pg_ctl -D /data/pgsql -l logfile start
-
切回root用户,配置启动项
[root@sonarqube pgsql]# cat /usr/lib/systemd/system/postgresql-13.service # It's not recommended to modify this file in-place, because it will be # overwritten during package upgrades. It is recommended to use systemd # "dropin" feature; i.e. create file with suffix .conf under # /etc/systemd/system/postgresql-13.service.d directory overriding the # unit's defaults. You can also use "systemctl edit postgresql-13" # Look at systemd.unit(5) manual page for more info. # Note: changing PGDATA will typically require adjusting SELinux # configuration as well. # Note: do not use a PGDATA pathname containing spaces, or you will # break postgresql-13-setup. [Unit] Description=PostgreSQL 13 database server Documentation=https://www.postgresql.org/docs/13/static/ After=syslog.target After=network.target [Service] Type=notify User=postgres Group=postgres # Note: avoid inserting whitespace in these Environment= lines, or you may # break postgresql-setup. # Location of database directory # 修改一下启动数据存放目录 Environment=PGDATA=/data/pgsql/ # Where to send early-startup messages from the server (before the logging # options of postgresql.conf take effect) # This is normally controlled by the global default set by systemd # StandardOutput=syslog # Disable OOM kill on the postmaster OOMScoreAdjust=-1000 Environment=PG_OOM_ADJUST_FILE=/proc/self/oom_score_adj Environment=PG_OOM_ADJUST_VALUE=0 ExecStartPre=/usr/pgsql-13/bin/postgresql-13-check-db-dir ${PGDATA} ExecStart=/usr/pgsql-13/bin/postmaster -D ${PGDATA} ExecReload=/bin/kill -HUP $MAINPID KillMode=mixed KillSignal=SIGINT # Do not set any timeout value, so that systemd will not kill postmaster # during crash recovery. TimeoutSec=0 # 0 is the same as infinity, but "infinity" needs systemd 229 TimeoutStartSec=0 TimeoutStopSec=1h [Install] WantedBy=multi-user.target
-
启动服务
systemctl daemon-reload systemctl start postgresql-13.service # 开机启动 systemctl enable postgresql-13.service
-
验证数据库是否启动
[root@sonarqube pgsql]# ps -ef |grep postmaster postgres 1688 1 0 17:40 ? 00:00:00 /usr/pgsql-13/bin/postmaster -D /data/pgsql/ root 1718 1052 0 17:41 pts/1 00:00:00 grep --color=auto postmaster [root@sonarqube pgsql]# netstat -lntup Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1315/sshd tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 1688/postmaster tcp6 0 0 :::22 :::* LISTEN 1315/sshd tcp6 0 0 ::1:5432 :::* LISTEN 1688/postmaster tcp6 0 0 :::3306 :::* LISTEN 26064/mysqld udp 0 0 127.0.0.1:323 0.0.0.0:* 921/chronyd udp6 0 0 ::1:323 :::* 921/chronyd
-
登陆数据库
[root@sonarqube ~]# su postgres # 切换用户 bash-4.2$ psql -U postgres # 登陆数据库 could not change directory to "/root": 权限不够 psql (13.5) Type "help" for help. postgres=# Alter USER postgres WITH PASSWORD 'postgres'; ALTER ROLE # 出现这个才算成功 postgres=# exit bash-4.2$ exit exit
-
修改远程连接配置文件参数
[root@sonarqube ~]# vim /data/pgsql/postgresql.conf # 修改为 * listen_addresses = '*' [root@sonarqube ~]# vim /data/pgsql/pg_hba.conf # 新增这一行,否则连接时会报错:Error connecting to the server:致命错误:没有用于主机“…”,用户“…”,数据库“…”,SSL关闭的pg_hba.conf记录: host all all 0.0.0.0/0 trust
-
重启数据库
systemctl restart postgresql-13.service
-
-
使用navicat登录postgresql数据库,创建为sonarQube专用的sonar用户,用户名为sonar,密码为sonar
-
连接数据库
-
创建sonarqube角色
-
创建数据库,数据库名称为:sonarqube
-
-
安装sonarqube服务
-
下载sonarqube包
[root@sonarqube ~]# wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.4.2.36762.zip
-
安装解压工具unzip
[root@sonarqube ~]# yum -y install unzip
-
解压sonarqube包
[root@sonarqube ~]# unzip sonarqube-8.4.2.36762.zip [root@sonarqube ~]# ll 总用量 226300 -rw-------. 1 root root 1626 1月 24 12:46 anaconda-ks.cfg -rw-r--r--. 1 root root 56 1月 24 14:03 passwd.txt drwxr-xr-x. 11 root root 141 8月 27 2020 sonarqube-8.4.2.36762 -rw-r--r--. 1 root root 231720221 8月 28 2020 sonarqube-8.4.2.36762.zip
-
移动到安装包固定位置
[root@sonarqube ~]# mv sonarqube-8.4.2.36762 sonarqube-8.4.2 [root@sonarqube ~]# mv sonarqube-8.4.2 /usr/local/ [root@sonarqube ~]# ln -s /usr/local/sonarqube-8.4.2/ /usr/local/sonarqube
-
修改启动配置文件
[root@sonarqube ~]# cd /usr/local/sonarqube/conf/ [root@sonarqube conf]# ll 总用量 24 -rw-r--r--. 1 root root 20245 8月 27 2020 sonar.properties -rw-r--r--. 1 root root 3217 8月 27 2020 wrapper.conf [root@sonarqube conf]# vim sonar.properties sonar.web.host=0.0.0.0 sonar.web.port=9000 sonar.jdbc.username=sonar sonar.jdbc.password=sonar sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube?currentSchema=public
-
创建一个普通用户修改sonarqube权限
[root@sonarqube conf]# useradd sonarqube [root@sonarqube conf]# chown -R sonarqube.sonarqube /usr/local/sonarqube-8.4.2/ [root@sonarqube conf]# chown -R sonarqube.sonarqube /usr/local/sonarqube
-
手动启动sonarqube尝试
[root@sonarqube conf]# su - sonarqube 上一次登录:二 1月 25 17:20:41 CST 2022pts/1 上 [sonarqube@sonarqube ~]$ cd /usr/local/sonarqube [sonarqube@sonarqube sonarqube]$ ll 总用量 12 drwxr-xr-x. 6 sonarqube sonarqube 94 8月 27 2020 bin drwxr-xr-x. 2 sonarqube sonarqube 50 2月 8 18:08 conf -rw-r--r--. 1 sonarqube sonarqube 7651 8月 27 2020 COPYING drwxr-xr-x. 2 sonarqube sonarqube 24 8月 27 2020 data drwxr-xr-x. 7 sonarqube sonarqube 131 8月 27 2020 elasticsearch drwxr-xr-x. 4 sonarqube sonarqube 40 8月 27 2020 extensions drwxr-xr-x. 6 sonarqube sonarqube 139 8月 27 2020 lib drwxr-xr-x. 2 sonarqube sonarqube 24 8月 27 2020 logs drwxr-xr-x. 2 sonarqube sonarqube 24 8月 27 2020 temp drwxr-xr-x. 6 sonarqube sonarqube 4096 8月 27 2020 web [sonarqube@sonarqube sonarqube]$ ll bin/ 总用量 0 drwxr-xr-x. 2 sonarqube sonarqube 25 8月 27 2020 jsw-license drwxr-xr-x. 3 sonarqube sonarqube 48 8月 27 2020 linux-x86-64 drwxr-xr-x. 3 sonarqube sonarqube 48 8月 27 2020 macosx-universal-64 drwxr-xr-x. 3 sonarqube sonarqube 167 8月 27 2020 windows-x86-64 [sonarqube@sonarqube sonarqube]$ ll bin/linux-x86-64/ 总用量 132 drwxr-xr-x. 2 sonarqube sonarqube 27 8月 27 2020 lib -rwxr-xr-x. 1 sonarqube sonarqube 16393 8月 27 2020 sonar.sh -rwxr-xr-x. 1 sonarqube sonarqube 111027 8月 27 2020 wrapper [sonarqube@sonarqube sonarqube]$ /usr/local/sonarqube/bin/linux-x86-64/sonar.sh start Starting SonarQube... Started SonarQube.
-
验证是否启动成功
[sonarqube@sonarqube sonarqube]$ ps -ef |grep sonarqube root 1809 1001 0 18:26 pts/0 00:00:00 su - sonarqube sonarqu+ 1940 1 0 18:26 ? 00:00:00 /usr/local/sonarqube-8.4.2/bin/linux-x86-64/./wrapper /usr/local/sonarqube-8.4.2/bin/linux-x86-64/../../conf/wrapper.conf wrapper.syslog.ident=SonarQube wrapper.pidfile=/usr/local/sonarqube-8.4.2/bin/linux-x86-64/./SonarQube.pid wrapper.daemonize=TRUE sonarqu+ 1972 1942 59 18:26 ? 00:00:17 /usr/lib/jvm/java-11-openjdk-11.0.14.0.9-1.el7_9.x86_64/bin/java -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInitiatingOccupancyOnly -Des.networkaddress.cache.ttl=60 -Des.networkaddress.cache.negative.ttl=10 -XX:+AlwaysPreTouch -Xss1m -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djna.nosys=true -XX:-OmitStackTraceInFastThrow -Dio.netty.noUnsafe=true -Dio.netty.noKeySetOptimization=true -Dio.netty.recycler.maxCapacityPerThread=0 -Dlog4j.shutdownHookEnabled=false -Dlog4j2.disable.jmx=true -Djava.io.tmpdir=/usr/local/sonarqube-8.4.2/temp -XX:ErrorFile=../logs/es_hs_err_pid%p.log -Des.enforce.bootstrap.checks=true -Xmx512m -Xms512m -XX:+HeapDumpOnOutOfMemoryError -Des.path.home=/usr/local/sonarqube-8.4.2/elasticsearch -Des.path.conf=/usr/local/sonarqube-8.4.2/temp/conf/es -Des.distribution.flavor=default -Des.distribution.type=tar -cp /usr/local/sonarqube-8.4.2/elasticsearch/lib/* org.elasticsearch.bootstrap.Elasticsearch sonarqu+ 2066 1942 99 18:26 ? 00:00:23 /usr/lib/jvm/java-11-openjdk-11.0.14.0.9-1.el7_9.x86_64/bin/java -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Djava.io.tmpdir=/usr/local/sonarqube-8.4.2/temp -XX:-OmitStackTraceInFastThrow --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/sonarqube-8.4.2/lib/jdbc/postgresql/postgresql-42.2.14.jar org.sonar.server.app.WebServer /usr/local/sonarqube-8.4.2/temp/sq-process17059138969832783266properties postgres 2103 1762 3 18:26 ? 00:00:00 postgres: sonar sonarqube 127.0.0.1(37102) idle postgres 2104 1762 13 18:26 ? 00:00:01 postgres: sonar sonarqube 127.0.0.1(37104) idle in transaction sonarqu+ 2217 1810 0 18:27 pts/0 00:00:00 grep --color=auto sonarqube [sonarqube@sonarqube sonarqube]$ netstat -lntup|grep 9000 (Not all processes could be identified, non-owned process info will not be shown, you would have to be root to see it all.) tcp6 0 0 :::9000 :::* LISTEN 2066/java
-
-
浏览器访问
首次登陆默认登陆用户admin、登陆密码admin
-
汉化界面
安装汉化包试试:页面上找到Administration > Marketplace,在搜索框中输入chinese,出现一个Chinese Pack,点击右侧的install按钮。
安装成功后,会提示重启 SonarQube 服务器。
检查发现已经启动,服务正常
-
配置centos7 进行系统服务systemctl进行管理
-
配置system系统服务
[root@sonarqube ~]# vim /etc/systemd/system/sonarqube.service [root@sonarqube ~]# cat /etc/systemd/system/sonarqube.service [Unit] Description=SonarQube service After=syslog.target network.target [Service] Type=simple User=sonarqube Group=sonarqube PermissionsStartOnly=true ExecStart=/bin/nohup /usr/local/jdk-11.0.2/bin/java -Xms32m -Xmx32m -Djava.net.preferIPv4Stack=true -jar /usr/local/sonarqube/lib/sonar-application-8.4.2.36762.jar StandardOutput=syslog LimitNOFILE=131072 LimitNPROC=8192 TimeoutStartSec=5 Restart=always SuccessExitStatus=143 [Install] WantedBy=multi-user.target
-
添加system系统服务执行权限
chmod +x /etc/systemd/system/sonarqube.service
-
重启服务
systemctl daemon-reload systemctl start sonarqube.service systemctl enable sonarqube.service
-