Install and configure SonarQube on CentOS 7
Introduction
SonarQube is an open-source platform for continuous inspection of code quality. It is used to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on more than 20 programming languages.
Platform notes
If you're running on Linux, you must ensure that:
Set Default JDK
Check Java version
Install and Setup PostgreSQL 10 Database For SonarQube
Install repository
Install the postgresql 10 database server by using following command
Initialize the postgres database
Modify pg_hba.conf file; change "peer" to "trust"and "ïdnet" to "md5"
Start postgresql database server
Enable postgresql start automatically at system startup
Change the password for the default PostgreSQL user
Switch to postgres user
Create a new user for SonarQube
Switch to postgresql shell
Set a password for sonarq user
Create a new database for SonarQube
Grant all privileges to sonarq user on sonarqube database
Exit from the psql shell
Return to root user
Download and configure SonarQube
Download sonarqube installer file
Unzip the archive setup to /opt directory
Rename extracted setup
We can't run SonarQube as a root user, following steps is to create an user for sonarqube
Edit SonarQube configuration file
Find the following lines, then uncomment and modify values
Edit sonar script file
- vm.max_map_count is greater or equals to 262144
- fs.file-max is greater or equals to 65536
- the user running SonarQube can open at least 65536 file descriptors
- the user running SonarQube can open at least 4096 threads
vim /etc/sysctl.d/90-sonarqube.conf
vm.max_map_count=262144
fs.file-max=65536
vim /etc/security/limits.conf
sonarqube - nofile 65536
sonarqube - nproc 4096
Install OpenJDK
yum install -y java-11-openjdk
Set Default JDK
update-alternatives --config java
There is 1 program that provides 'java'. Selection Command ----------------------------------------------- *+ 1 java-11-openjdk.x86_64 (/usr/lib/jvm/java-11-openjdk-11.0.5.10-0.el7_7.x86_64/bin/java) Enter to keep the current selection[+], or type selection number:
Press Enter
Check Java version
java -version
openjdk version "11.0.5" 2019-10-15 LTS
OpenJDK Runtime Environment 18.9 (build 11.0.5+10-LTS)
OpenJDK 64-Bit Server VM 18.9 (build 11.0.5+10-LTS, mixed mode, sharing)
Install and Setup PostgreSQL 10 Database For SonarQube
Install repository
yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Install the postgresql 10 database server by using following command
yum install postgresql10-server postgresql10-contrib -y
Initialize the postgres database
/usr/pgsql-10/bin/postgresql-10-setup initdb
Modify pg_hba.conf file; change "peer" to "trust"and "ïdnet" to "md5"
vim /var/lib/pgsql/10/data/pg_hba.conf
From:# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all peer
# IPv4 local connections:
host all all 127.0.0.1/32 ident
# IPv6 local connections:
host all all ::1/128 ident
To:
# TYPE DATABASE USER ADDRESS METHOD
# "local" is for Unix domain socket connections only
local all all trust
# IPv4 local connections:
host all all 127.0.0.1/32 md5
# IPv6 local connections:
host all all ::1/128 md5
systemctl start postgresql-10
Enable postgresql start automatically at system startup
systemctl enable postgresql-10
Change the password for the default PostgreSQL user
passwd postgres
Switch to postgres user
su - postgres
Create a new user for SonarQube
createuser sonarq
Switch to postgresql shell
psql
Set a password for sonarq user
ALTER USER sonarq WITH ENCRYPTED password 'PASSword123';
Create a new database for SonarQube
CREATE DATABASE sonarqube OWNER sonarq;
Grant all privileges to sonarq user on sonarqube database
grant all privileges on database sonarqube to sonarq;
Exit from the psql shell
\q
Return to root user
exit
Download and configure SonarQube
Download sonarqube installer file
cd /tmp
curl -O https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.1.0.31237.zip
Unzip the archive setup to /opt directory
unzip sonarqube-8.1.0.31237.zip -d /opt
Rename extracted setup
mv /opt/sonarqube-8.1.0.31237 /opt/sonarqube
We can't run SonarQube as a root user, following steps is to create an user for sonarqube
groupadd sonar
useradd -c "user to run SonarQube" -d /opt/sonarqube -g sonar sonar
chown -R sonar:sonar /opt/sonarqube
Edit SonarQube configuration file
vim /opt/sonarqube/conf/sonar.properties
Find the following lines, then uncomment and modify values
sonar.jdbc.username=sonarq sonar.jdbc.password=PASSword123 sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube sonar.web.host="your_server_ip" sonar.web.port=9000 sonar.web.javaOpts=-server -Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError
Edit sonar script file
vim /opt/sonarqube/bin/linux-x86-64/sonar.sh
Set RUN_AS_USER
RUN_AS_USER=sonar
Setting up SonarQube as a service
vim /etc/systemd/system/sonarqube.service
[Unit] Description=SonarQube service After=syslog.target network.target [Service] Type=forking ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop LimitNOFILE=65536 LimitNPROC=4096 User=sonar Group=sonar Restart=on-failure [Install] WantedBy=multi-user.target
Reload "systemctl" daemon and enable sonar on system boot
systemctl daemon-reload
systemctl enable sonarqube.service
Start service and check its status
systemctl start sonarqube.service
systemctl status sonarqube.service
Comments