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

sonarqube installation and configuration

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 setup 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.

Note: This setup was tested on a Linux ec2  server.

Setup Sonarqube MYSQL Database

1. Install wget

sudo yum install wget -y

2. Add mysql repositor, install and update it.

wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
sudo yum update -y

3. Install MySQL server.

sudo yum install mysql-server

4. Start MySQL service.

sudo systemctl start mysqld

5. Set password for yout MYSQl instance using the following command. The command will prompt for current root password, you can hit enter as there is no default password. All the other prompts are self-explanatory.

sudo mysql_secure_installation

6. Login using MySQL client and check the MySQL engine using the following commands. The storage engine should be InnoDB.

mysql -u root -p
SHOW GLOBAL VARIABLES LIKE 'storage_engine'

7. Create a sonarqube user and a database from MySQL CLI.

CREATE USER 'sonarqube'@'localhost' IDENTIFIED BY 'password';
CREATE DATABASE sonarqube;

8. Grant all privileges on sonarqube database to the sonarqube user.

GRANT ALL PRIVILEGES ON sonarqube.* TO 'sonarqube'@'localhost';

9. Open /etc/my.cnf file and under [mysqld] section add the query cache parameter. The minimum size should be 15 MB. You can increase the size based on your server type.

query_cache_size = 15M

Setup Sonarcube Web Server

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

cd /opt 
sudo wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.0.zip

2. Install unzip and java.

sudo yum install unzip -y
sudo yum install java-1.8.0-openjdk -y

3. Unzip sonarqube source files and rename the folder.

sudo unzip sonarqube-6.0.zip
mv sonarqube-6.0 sonarqube

4. Open /opt/sonarqube/conf/sonar.properties, uncomment and edit the parameters as shown below. Change the password accordingly.

sonar.jdbc.username=sonarqube                                                                                                                     
sonar.jdbc.password=password
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance 

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.

sonar.web.host=0.0.0.0
sonar.web.port=80

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

sonar.web.context=/sonar

Start Sonarqube Service

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

1. Navigate to the start script directory.

 cd /opt/sonarqube/bin/linux-x86-64

2. Start the sonarqube service.

sudo ./sonar.sh start

3.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.

sudo ./sonar.sh start

Setting up Sonarcube as a service

1. Create a file /etc/init.d/sonar and copy the following content on to the file.

#!/bin/sh
#
# rc file for SonarQube
#
# chkconfig: 345 96 10
# description: SonarQube system (www.sonarsource.org)
#
### BEGIN INIT INFO
# Provides: sonar
# Required-Start: $network
# Required-Stop: $network
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: SonarQube system (www.sonarsource.org)
# Description: SonarQube system (www.sonarsource.org)
### END INIT INFO
 
/usr/bin/sonar $*

2. Now, create a symbolic link for /usr/bin/sonar with out sonarqube start scripts in the source file directory. i.e /opt/sonarqube/bin/linux-x86-64

sudo ln -s /opt/sonarqube/bin/linux-x86-64/sonar.sh /usr/bin/sonar

3. Change the file permissions and add sonar to the boot.

sudo chmod 755 /etc/init.d/sonar
sudo chkconfig --add sonar

4. Once you are done with all the above configurations, you can manage sonar using the following commands.

sudo service sonar start
sudo service sonar stop
sudo service sonar restart

For sonarqube support, visit this link

In this tutorial, we covered how to install and configure sonarqube on a Linux server. If you face any issues during the setup, please feel free to contact us or leave a comment below.

Free DevOps Resources

Get DevOps news, tutorials and resources in your inbox. A perfect way If you want to get started with devops. Like you, we dont like spam.

Leave a Reply

Your email address will not be published. Required fields are marked *