How to install and configure SonarQube on CentOS 7

This tutorial covers installing and configuring SonarQube 7.9.x LTS with Oracle JAVA 11, PostgreSQL 10.x, Nginx, and Let's Encrypt certificates.

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.

Here we are going to install and configure SonarQube 7.9.x LTS with Oracle JAVA 11, PostgreSQL 10.x, Nginx, and Let’s Encrypt certificates.

Installing and configuring SonarQube on CentOS

Execute the following commands using the root user.

1. Update System

yum update

2. Disable SELinux

Open SELinux configuration and edit the file:

vim /etc/sysconfig/selinux

Change “SELINUX=enforcing” to “SELINUX=disabled”.

Save and exit the file.

3. Set hostname to the server

 vim /etc/hostname

If vim is not available, install vim command first.

 yum install vim -y

Then reboot the system.

reboot

4. Prerequisite

You can check the official document for complete information.

  • Java (Oracle JRE 11 or OpenJDK 11)
  • PostgreSQL 10 or 9.3–9.6

Hardware Requirements

  • Server with 2GB or plus RAM
  • Systems setting for Linux

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

5. Add System settings

Edit “sysctl.conf” file:

vim /etc/sysctl.conf

Add the following lines:

vm.max_map_count=262144
fs.file-max=65536

Sysctl Values
Sysctl Values

Save and exit the file.

6. Install Oracle Java 11

Download Oracle JDK 11 from here.

Oracle JAVA
Oracle JAVA

Before you download, it will redirect to the oracle login. If you have an account, use it or create a new one.

Oracle Login
Oracle Login

You can download the rpm package to your machine and upload it to the sonar server.

OR you can use the following steps:

a) You can copy the download link from web browser downloads.

Copy Download Link
Copy Download Link

b) Then go to your server and download using the “wget” command.

wget https://download.oracle.com/otn/java/jdk/11.0.5+10/e51269e04165492b90fa15af5b4eb1a5/jdk-11.0.5_linux-x64_bin.rpm?AuthParam=1573886978_5511f6acaa0b321333446e8e838c1045

download using wget
download using wget

c) If “wget” command is not available in your system to install it using the following command:

 yum install wget -y

Rename the downloaded file.

 mv jdk-11.0.5_linux-x64_bin.rpm?AuthParam=1573886978_5511f6acaa0b321333446e8e838c1045 jdk-11.0.5_linux-x64_bin.rpm

Install Oracle JDK:

yum localinstall jdk-11.0.5_linux-x64_bin.rpm

Install Oracle JDK
Install Oracle JDK

Java installed in the following location:

cd /usr/java/

Java Installed Location
Java Installed Location

Add java environment variables:

vim /etc/bashrc

Add following lines to end of the file:

export JAVA_HOME=/usr/java/jdk-11.0.5/
export JRE_HOME=/usr/java/jdk-11.0.5/jre

PATH=$PATH:$HOME/bin:$JAVA_HOME/bin

Add Environment Variables
Add Environment Variables

Save and exit the file and check the java version.

java -version

Check Java Version
Check Java Version

7. Install PostgreSQL 10

You can see downloads for Redhat based distributions here.

Install repository first:

yum install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

Install repository
Install repository

Install server:

yum install postgresql10-server postgresql10-contrib

Install postgresql10
Install postgresql10

Initialize the database:

/usr/pgsql-10/bin/postgresql-10-setup initdb

Initialize Database
Initialize Database

Modify pg_hba.conf file; change “peer” to “trust” and “idnet” to “md5”.

vim /var/lib/pgsql/10/data/pg_hba.conf

Change file
Change file

After the modification is done, the file should be as follows:

After Modification
After Modification

To start service and set on boot, enable PostgreSQL on system boot:

systemctl enable postgresql-10

Check service status and start it.

systemctl status postgresql-10
systemctl start postgresql-10

Change the default password of the Postgres user:

passwd postgres

Switch to the Postgres user.

su - postgres

Create a new user.

createuser sonar

Switch to PostgreSQL shell.

psql

PostgreSQL shell
PostgreSQL shell

Set a password for the newly created user for the SonarQube database:

ALTER USER sonar WITH ENCRYPTED password 'd98ffW@123?Q';

Create a new database for the PostgreSQL database.

CREATE DATABASE sonar OWNER sonar;

Exit from the psql shell.

q

Exit from the “postgres” user.

exit

User and Database
User and Database

8. Download and configure SonarQube

We are going to download the package in to “opt” directory. So change directory

cd /opt

Here we are going to use 7.9.x LTS version and can be download here

I. Download Latest LTS version

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

II. Then unzip

unzip sonarqube-7.9.1.zip

If unzip command not available. Install unzip.

yum install unzip -y

III. Rename folder

mv sonarqube-7.9.1 sonarqube

IV. Modify “sonar.properties file”.

vim /opt/sonarqube/conf/sonar.properties

Find the following lines. Then uncomment and modify values.

sonar.jdbc.username=sonar
sonar.jdbc.password=d98ffW@123?Q
sonar.jdbc.url=jdbc:postgresql://localhost/sonar
sonar.web.host=127.0.0.1
sonar.web.port=9000
sonar.web.javaOpts=-server -Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError
sonar.search.javaOpts=-server -Xms512m -Xmx512m -XX:+HeapDumpOnOutOfMemoryError

Configuring the Elasticsearch storage path:

sonar.path.data=/var/sonarqube/data
sonar.path.temp=/var/sonarqube/temp

Save and exit the file.

V. Create a user for sonar

useradd sonar

Set password:

passwd sonar

VI. Modify folder permissions

chown -R sonar:sonar /opt/sonarqube

Create the following folders and grant permission:

mkdir -p /var/sonarqube/data
mkdir -p /var/sonarqube/temp
chown -R sonar:sonar /var/sonarqube

VII. Setting up Sonarqube as a service

vim /etc/systemd/system/sonarqube.service

Add the following content to file:

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

Sonar Status
Sonar Status

VIII. logfile location

cd /opt/sonarqube/logs/
  • SonarQube service log
tail -f /opt/sonarqube/logs/sonar.log
  • Web Server Logs
tail -f /opt/sonarqube/logs/web.log
  • ElasticSearch logs
tail -f /opt/sonarqube/logs/es.log
  • Compute Engine logs
tail -f /opt/sonarqube/logs/ce.log

9. Configure reverse proxy

Install Nginx, start service, and enable on system boot.

yum install -y nginx
systemctl start nginx
systemctl enable nginx

10. Configure SSL

Enable epel repo and install certbot.

yum install – y epel-release
yum install certbot python2-certbot-nginx

Run the following command to get a certificate and have Certbot edit your Nginx configuration automatically to serve it, turning on HTTPS access in a single step.

certbot --nginx

Command will ask questions . Then add needed details according to that.

Certbot
Certbot

Certbot Configurations
Certbot Configurations

After installation is done, open nginx.conf.

vim /etc/nginx/nginx.conf

You should see certbot SSL configuration.

After SSL configuration
After SSL configuration

Then add the following contents to a Location Blocks.

location / {

proxy_pass "http://127.0.0.1:9000";
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;

}

Save and exit the file. The modified file looks like below:

Nginx Configurations
Nginx Configurations

Check nginx syntax:

nginx -t

Restart nginx:

systemctl restart nginx

11. DNS

Then go to your DNS manager and add A record for your sonar server.

A   Domain Name   Server IP

12. Modify Firewall Rules

If you have the firewall enabled, run the following command to open https traffic.

firewall-cmd --zone=public --permanent --add-service=https
firewall-cmd --reload

Firewall Rule HTTPS
Firewall Rule HTTPS

if you need to open sonar for specific IP, run the below command:

firewall-cmd --permanent --zone=public --add-rich-rule='
rule family="ipv4"
source address="122.43.8.188/32"
port protocol="tcp" port="443" accept'
firewall-cmd --reload

13. Browse Sonarqube

Go to your browser and type your domain name.

eg:- https://sonar.fosslinux.com/

Browse Sonarqube
Browse Sonarqube

Then click “login.”

14. Login page

The default username and password is “admin”.

Login
Login

Dashboard

Dash Board
DashBoard

Now we configured SonarQube successfully. In our upcoming articles, we will see how to integrate SonarQube with Jenkins.

Darshana
Hey! I'm Darshana, a Linux / DevOps Engineer and also a contributor to FOSS Linux. I enjoy working on various kind of Linux distributions and cloud technologies. During my free time, I love to swim and hike across nature trails. Linux is my love and I'm here to share all my learnings with all of you! Hope you enjoyed reading my article.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

24,270FansLike
192FollowersFollow

LATEST ARTICLES

clean virus ubuntu live CD
In this tutorial, we are going to show you how to clean your Windows machine from infected viruses using an Ubuntu live USB or CD and the ClamAV antivirus. The ClamAV is a free, open-source antivirus that can be used on Ubuntu.
essential linux commands
Last week, we shared with you several "cool and fun" commands to get comfortable and confident with the Linux command-line. In our quest to further aid Linux users with mastery of the command line, or CLI, we present you with a variety of command-line utilities essential for all Linux users, regardless of proficiency level.
Application Launchers for Ubuntu
Ubuntu is one of the most used Linux distributions worldwide. It is also the reason why it has the maximum number of available programs for itself. Today we are going to talk about one category of those programs, the application launchers.