centos7 安装 SonarQube

发布时间 2023-10-26 09:41:30作者: 小⑦

3.1安装postgresql数据库

yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql13-server
/usr/pgsql-13/bin/postgresql-13-setup initdb
systemctl enable postgresql-13
systemctl start postgresql-13

创建数据库

#先切换到postgres用户
su - postgres
# 执行创建指令
psql
create user sonarqube with password 'sonarqube';
create database sonarqube owner sonarqube;
grant all on database sonarqube to sonarqube;
\q
su -

3.2添加用户

启动前先创建sonar用户组,sonar不能采用root用户直接启动(因为里面有es,es不能用root用户启动)

[root@localhost ~]# adduser sonar
[root@localhost ~]# passwd sonar

授权

个人用户的权限只可以在本home下有完整权限,其他目录要看别人授权。而经常需要root用户的权限,这时候sudo可以化身为root来操作。我记得我曾经sudo创建了文件,然后发现自己并没有读写权限,因为查看权限是root创建的。

新创建的用户并不能使用sudo命令,需要给他添加授权。

sudo命令的授权管理是在sudoers文件里的。

[root@localhost ~]# sudoers
bash: sudoers: 未找到命令...
[root@localhost ~]# whereis sudoers
sudoers: /etc/sudoers /etc/sudoers.d /usr/libexec/sudoers.so /usr/share/man/man5/sudoers.5.gz
#查看权限
[root@localhost ~]# ls -l /etc/sudoers
-r--r----- 1 root root 4251 9月 25 15:08 /etc/sudoers

#先添加w权限
chmod -v u+w /etc/sudoers

#添加内容
vim /etc/sudoers
## Allow root to run any commands anywher
root ALL=(ALL) ALL
sonar ALL=(ALL) ALL ALL #这个是新增的用户,ALL 必须全部大写。
#可以尝试使用 visudo 命令来编辑 /etc/sudoers 文件并修复语法错误。
#直接编辑 /etc/sudoers 文件可能会导致安全风险,因此建议使用 visudo 命令。

#wq保存退出,将写权限收回:
[root@localhost ~]# chmod -v u-w /etc/sudoers
mode of "/etc/sudoers" changed from 0640 (rw-r-----) to 0440 (r--r-----)

3.3下载sonar社区版

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-10.2.1.78527.zip

unzip sonarqube-10.2.1.78527.zip

mv sonarqube-10.2.1.78527.zip /home/sonarqube/
chown -R sonar:sonar /home/sonar/
su - sonar
yum -y install unzip
unzip sonarqube-10.2.1.78527.zip

#更改配置文件
vim sonarqube-10.2.1.78527/conf/sonar.properties
#该命令使用 grep 命令从 sonarqube-10.2.1.78527/conf/sonar.properties 文件中筛选出不以 # 开头且不为空的行。
[sonarqube@localhost ~]$ grep -v "^#" sonarqube-10.2.1.78527.zip/conf/sonar.properties | grep -v "^$"
sonar.jdbc.username=sonarqube
sonar.jdbc.password=sonarqube
sonar.jdbc.url=jdbc:postgresql://127.0.0.1/sonarqube

修改/etc/sysctl.conf文件,文件末尾追加vm.max_map_count=262144

修改/etc/security/limits.conf文件,文件末尾追加

* soft nproc 4096
* hard nproc 4096
* soft nofile 1000000
* hard nofile 1000000
#* 表示所有用户。将为所有用户设置 nproc(最大进程数)和 nofile(最大打开文件数)的软硬限制。
#软限制是警告值,而硬限制是强制值。

启动

启动命令: ./sonar.sh start
查看启动日志: tail -f ../../logs/sonar.log

浏览器访问:http://192.168.2.28:9000 登录用户admin 密码admin