1 概述
1.1 环境信息
- 操作系统:CentOS7.6
- PostgreSQL:13
- Sonarqube:9.1
1.2 原理介绍
Sonar 是一款静态代码质量分析工具,支持 Java、Python、PHP、JavaScript、CSS 等25种以上的语言,而且能够集成在 IDE、Jenkins、Git 等服务中,方便随时查看代码质量分析报告。
Sonar 通过配置的代码分析规则,从可靠性、安全性、可维护性、覆盖率、重复率等方面分析项目,风险等级从 A~E 划分为5个等级;同时,sonar 可以集成 pmd、findbugs、checkstyle 等插件来扩展使用其他规则来检验代码质量。
Sonar 设置了质量门,通过设置的质量门评定此次提交分析的项目代码是否达到了规定的要求。
1.3 工作流程
- 开发人员在其 IDE 中进行编码,并使用 SonarLint 运行本地分析。
- 开发人员将其代码推送到他们最喜欢的 SCM 中:git,SVN,TFVC 等。
- Continuous Integration Server 会触发自动构建,并执行运行 SonarQube 分析所需的 SonarScanner。
- 分析报告将发送到 SonarQube 服务器进行处理。
- SonarQube Server 处理分析报告结果并将其存储在 SonarQube 数据库中,并在 UI 中显示结果。
- 开发人员通过 SonarQube UI 审查,评论,挑战他们的问题,以管理和减少技术债务。
- 经理从分析中接收报告。Ops 使用 API 自动执行配置并从 SonarQube 提取数据。运维人员使用 JMX 监视 SonarQube Server。
2 软件下载
2.1 Sonarqube 下载
cd /opt
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.1.0.47736.zip
3 配置 postgresql yum 源
# Install the repository RPM:
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
4 安装 PostgreSQL
# Install PostgreSQL:
yum install -y postgresql13-server
# Optionally initialize the database and enable automatic start:
/usr/pgsql-13/bin/postgresql-13-setup initdb
systemctl enable postgresql-13
# start postgresql13
systemctl start postgresql-13
# run status
systemctl status postgresql-13
# 查看进程信息
ps -ef | grep postgres
# 查看 5432端口是否打开
netstat -tpnl | grep 5432
5 修改 PostgreSQL 用户密码和有关配置文件
# 切换用户
su - postgres
# 连接pgsql server
psql -U postgres
# 添加密码
alter user postgres with password '12345678';
select * from pg_shadow;
# 退出
\q
# 进入data目录
cd /var/lib/pgsql/13/data
# 去掉listen_addresses注释,将listen_addresses = 'localhost' 改成 listen_addresses = '*'
vi postgresql.conf
# 在 'IPv4 local connections:' 下面增加一行允许所有连接
vi pg_hba.conf
host all all 0.0.0.0/0 trust
# 退出postgres用户
exit
# 重启postgresql-13
systemctl restart postgresql-13
# 最后云服务器防火墙开放5432端口就可以使用Navicat工具连接啦!
6 安装 Sonarqube
6.1 解压
cd /opt
unzip sonarqube-9.1.0.47736.zip
mv sonarqube-9.1.0.47736 sonarqube
cd sonarqube/conf
6.2 修改配置
mkdir -p /opt/sonarqube/{data,temp}
mv sonar.properties{,.bak}
vim sonar.properties
sonar.jdbc.username=sonarqube
sonar.jdbc.password=12345678
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube
sonar.path.data=/opt/sonarqube/data
sonar.path.temp=/opt/sonarqube/temp
sonar.web.host=192.168.0.181
sonar.web.port=9000
sonar.web.context=/
vim wrapper.conf
wrapper.java.command=/usr/bin/java
6.3 安装 openjdk
yum install java-11-openjdk.x86_64 -y
6.4 创建普通用户并修改权限
useradd sonar
chown -R sonar. /opt/sonarqube
6.5 优化内核参数以使 ElasticSearch 正常启动
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
echo -e "sonar soft nofile 65535\nsonar hard nofile 65535" >> /etc/security/limits.conf
sysctl -p
退出终端重新连入。
6.6 创建数据库用户
su - postgres
psql -U postgres
# 创建用户
CREATE USER sonarqube WITH PASSWORD '12345678';
# 创建数据库
CREATE DATABASE sonarqube;
# 授权
GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonarqube;
# 但此时用户还是没有读写权限,需要继续授权表
GRANT ALL PRIVILEGES ON all tables in schema public TO sonarqube;
6.7 启动 sonarqube 服务
su - sonar -c "/opt/sonarqube/bin/linux-x86-64/sonar.sh start"
6.8 登录 web 页面
登录后强制修改默认密码,修改即可,修改完后重新登录:
http://192.168.0.181:9000
- 默认用户:admin
- 默认密码:admin
修改中文:
Administration -> Marketplace -> All -> chinese
6.9 设置开机自启动
echo "su - sonar -c \"/opt/sonarqube/bin/linux-x86-64/sonar.sh start\"" >> /etc/rc.d/rc.local
chmod +x /etc/rc.d/rc.local
7 配置 Nginx 反向代理
7.1 下载 Nginx
cd /tmp
curl -o /tmp/nginx-1.21.0-with-third-module.zip https://oss.iuskye.com/article/2021-08-15/nginx-1.21.0-with-third-module.zip
7.2 编译安装
unzip -q nginx-1.21.0-with-third-module.zip
mv nginx-1.21.0 nginx
cd nginx/nginx-1.21.0
# 打补丁
patch -p1 < ../ngx_http_proxy_connect_module-master/patch/proxy_connect_rewrite_1018.patch
# 编译安装
./configure --prefix=${install_dir}/nginx --sbin-path=${install_dir}/nginx/sbin/nginx --conf-path=${install_dir}/nginx/conf/nginx.conf --error-log-path=${install_dir}/nginx/log/error.log --http-log-path=${install_dir}/nginx/log/access.log --pid-path=${install_dir}/nginx/nginx.pid --lock-path=${install_dir}/nginx/nginx.lock --user=mbs --group=mbs --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --http-client-body-temp-path=${install_dir}/nginx/client/ --http-proxy-temp-path=${install_dir}/nginx/proxy/ --http-fastcgi-temp-path=${install_dir}/nginx/fcgi/ --http-uwsgi-temp-path=${install_dir}/nginx/uwsgi --http-scgi-temp-path=${install_dir}/nginx/scgi --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-stream_realip_module --with-http_realip_module --with-http_gunzip_module --with-http_degradation_module --with-mail --with-mail_ssl_module --with-http_slice_module --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_v2_module --with-http_secure_link_module --with-pcre-jit --with-http_sub_module --with-pcre=/tmp/nginx/pcre-8.37 --with-openssl=/tmp/nginx/openssl-1.1.1k --with-zlib=/tmp/nginx/zlib-1.2.11 --add-module=/tmp/nginx/nginx-upload-module-2.3.0 --add-module=/tmp/nginx/nginx_upstream_check_module-master --add-module=/tmp/nginx/headers-more-nginx-module-master --add-module=/tmp/nginx/ngx_http_proxy_connect_module-master --with-debug
cores=$(cat /proc/cpuinfo| grep "processor"| wc -l)
make -j $cores
make install
7.3 配置反向代理
mkdir -p /opt/nginx/conf/conf.d
vim /opt/nginx/conf/conf.d/sonarqube.conf
upstream sonarqube {
server 192.168.0.181:9000;
}
server {
listen 80;
server_name 192.168.0.181 sq.test.com;
access_log /opt/nginx/log/access.log;
error_log /opt/nginx/log/error.log;
location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://sonarqube;
}
}
7.4 启动 Nginx
/opt/nginx/sbin/nginx -t
/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf