Install and configure Sonarqube on Linux (RHEL/Centos/ec2)

Install and configure Sonarqube on Linux (RHEL/Centos/ec2)

sonar_linux

 

Sonarqube is a great tool for source code quality management, code analysis etc. This is the most widely used tool for code coverage and analysis.

Install and  Configure Sonarqube on Linux

This guide will help you to set up and configure sonarqube on Linux servers (Redhat/Centos 7 versions) on any cloud platforms like ec2, azure, compute engine or on-premise data centers. Follow the steps given below for the complete sonarqube configuration.

Sonarqube requirements

  1. Server with minimum 2GB/1 vcpu capacity
  2. PostgreSQL version 9.3 or greater.
  3. OpenJDK 11 or JRE 11
  4. All sonarquber process should run as a non-root sonar user.

You can find the official requirement doc here.

Update: MySQL for Sonarqube is depricated

Prep the Server With Required Softwares

Step 1: Update the server.

Step 2: Install wget & unzip

Step 3: Install java 11

Step 4: Login as root and execute the following commands.

Setup PostgreSQL 10 Database For SonarQube

Step 1: Install PostgreSQL 10 repo.

Step 2: Install PostgreSQL 10

Step 3: Initialize the database.

Step 4: Open /var/lib/pgsql/data/pg_hba.conf file to change the authentication to md5.

Find the following lines at the bottom of the file and change peer to trust and idnet to md5

Once changed, it should look like the following.

Step 5: Start and enable PostgreSQL.

Step 6: You can verify the installation using the following version select query.

Setup Sonar User and Database

We need to have a sonar user and database for the sonar application.

Step 1: Change the default password of the Postgres user. All Postgres commands have to be executed from this user.

Step 2: Login as postgres user with the new password.

Step 3: Login to the PostgreSQL CLI.

Step 4: Create a sonarqubedb database.

Step 5: Create the sonarqube DB user with a strongly encrypted password. Replace your-strong-password with a strong password.

Step 6: Next, grant all privileges to sonrqube user on sonarqubedb.

Step 7: Exit the psql prompt using the following command.

Step 6: Switch to your sudo user using the exit command.

Setup Sonarqube Web Server

Step 1: Download the latest sonarqube installation file to /opt folder. You can get the latest download link from here. http://www.sonarqube.org/downloads/

2. Unzip sonarqube source files and rename the folder.

4. Open /opt/sonarqube/conf/sonar.properties file.

Uncomment and edit the parameters as shown below. Change the password accordingly. You will find jdbc parameter under PostgreSQL section.

By default, sonar will run on 9000. If you want on port 80 or any other port, change the following parameters for accessing the web console on that specific port.

If you want to access sonarqube some path like http://url:/sonar, change the following parameter.

Add Sonar User and Privileges

Create a user named sonar and make it the owner of the /opt/sonarqube directory.

Start Sonarqube Service

To start sonar service, you need to use the script in sonarqube bin directory.

Step 1: Login as sonar user

Step 2: Navigate to the start script directory.

Step 3: Start the sonarqube service.

Now, you should be able to access sonarqube on the browser on port 9000

Step 4: Check the application status. If it is in running state, you can access the sonarqube dashboard using the DNS name or Ip address of your server.

Setting up Sonarqube as a service

Step 1: Create a file /etc/systemd/system/sonarqube.service

Step 2: Copy the following content on to the file.

Step 3: Start and enable sonarqube

Step 4: Check the sonarqube status to ensure it is running as expected.

Troubleshooting Sonarqube

All the logs of sonarqube are present in the /opt/sonarqube/logs directory.

You can find the following log files.

Using tail command you can check the latest logs. For example,

For sonarqube support, visit this link

Tech Chronicles