Setup SonarQube on Ubuntu 18.04

  Reading Time:

Hardware Requirements

Source : https://docs.sonarqube.org/latest/requirements/requirements/

  1. SonarQube server requires at least 2GB of RAM to run efficiently and 1GB of free RAM for the OS.
  2. The amount of disk space you need will depend on how much code you analyze with SonarQube. As an example, SonarCloud the public instance of SonarQube, has more than 350 million lines of code under analysis with 5 years of history. SonarCloud is currently running on clustered Amazon EC2 m5.large instances with allocations of 50 Gb of drive space per node. It handles 19,000+ projects with roughly 14M open issues. SonarCloud runs on PostgreSQL 9.5 and it is using about 250Gb of disk space for the database.
  3. SonarQube does not support 32-bit systems on the server side. SonarQube does, however, support 32-bit systems on the scanner side.

Current setup requires following tools

  1. SonarQube 7.7 (Current version while writing this article)
  2. Java - Open JDK 8
  3. Mysql 5.7  (Must be configured to use UTF8 charset and a case-sensitive (CS) collation , Only InnoDB storage engine is supported, but not MyISAM, Only the bundled mysql-connector-java jar is supported)
  4. Nginx and Let's Encrypt HTTPS certificate

Set up new sudo user - sonarqube

Create 'sonarqube' user on Ubuntu 18.04 and copy all root user ssh keys to sonarqube user account

adduser sonarqube

usermod -aG sudo sonarqube

rsync --archive --chown=sonarqube:sonarqube ~/.ssh /home/sonarqube

Install Java - Open JDK 8  on Ubuntu 18.04

sudo apt install openjdk-8-jdk

Install SonarQube 7.7

We shall install sonarqube under /opt folder

su - sonarqube

sudo mkdir /opt/sonarqube

We shall download the latest SonarQube 7.7 version from this link

cd /opt/sonarqube

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

sudo apt-get install unzip

sudo unzip sonarqube-7.7.zip

sudo chown -R sonarqube:sonarqube /opt/sonarqube

Install MySQL 5.7

Lets install Mysql 5.7 and set it up for Sonarqube

sudo apt install mysql-server

sudo mysql -u root -p

Create Mysql user and database for SonarQube

mysql> CREATE DATABASE sonarqube77;

mysql> CREATE USER sonarqube77@'localhost' IDENTIFIED BY 'tough_password';

mysql> GRANT ALL ON sonarqube77.* to sonarqube77@'localhost';

mysql> FLUSH PRIVILEGES;

mysql> EXIT;

Configure SonarQube with MYSQL

Edit the following file : /opt/sonarqube/sonarqube-7.7/conf/sonar.properties

Add Mysql Username and password to sonar.properties

Finally lets configure the JDBC url information for Mysql under Mysql section in sonar.properties file

Configure SonarQube web server

We will be running sonarqube on localhost port 9011 and nginx running on https  will be the proxy server that will redirect  traffic to port 9011

Full sonar.properties on Github gist

 

Configure SonarQube Service

Lets make Sonarqube a service using systemd

Create a file 'sonarqube.service'  under directory '/etc/systemd/system'

[Unit]
Description=SonarQube v7-7 service
After=syslog.target network.target

[Service]
Type=forking

ExecStart=/opt/sonarqube/sonarqube-7.7/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/sonarqube-7.7/bin/linux-x86-64/sonar.sh stop

User=sonarqube
Group=sonarqube
Restart=always

[Install]
WantedBy=multi-user.target

Lets configure SonarQube to auto start on reboot

sudo systemctl enable sonarqube

Start SonarQube  server

sudo service sonarqube start
sudo service sonarqube status

Install Nginx as reverse proxy

sudo apt install nginx

sudo ufw allow 'Nginx HTTP'

sudo ufw allow 'Nginx HTTPS'


To                         Action      From
--                         ------      ----
Nginx HTTP                 ALLOW       Anywhere
Nginx HTTPS                ALLOW       Anywhere
22/tcp                     ALLOW       Anywhere
Nginx HTTP (v6)            ALLOW       Anywhere (v6)
Nginx HTTPS (v6)           ALLOW       Anywhere (v6)
22/tcp (v6)                ALLOW       Anywhere (v6)


Create configuration called 'sonarqube' for sonarqube port 80 redirect

Create file  '/etc/nginx/sites-enabled/sonarqube'

server {
    listen 80;
    server_name sonarqube.website.com;

    location / {
        proxy_pass http://127.0.0.1:9011;
    }
}

 

Increase large file size upload capability to Nginx by adding the given property to '/etc/nginx/nginx.conf ' . Note : This property is important as the analysis report files will be uploaded from Jenkins to SonarQube and we need to make sure we don't get 'Nginx: 413 – Request Entity Too Large Error'

Add the following line to http or server or location context to increase the size limit in nginx.conf,

client_max_body_size 10M;

 

Test the configuration

sudo nginx -t

sudo service nginx restart

Lets test the URL , point your browser to http://sonarqube.website.com

Enable HTTPS

The server is running on HTTP so lets enable HTTPS using Lets encrypt certificate

sudo add-apt-repository ppa:certbot/certbot
sudo apt-get update
sudo apt-get install python-certbot-nginx

Run certbot

sudo certbot --nginx -d sonarqube.website.com

Cerbot will ask a few questions , please answer those questions and choose rediect all http to https using option 2

Lets test the URL , point your browser to http://sonarqube.website.com , the url should get auto redirected to https://sonarqube.website.com

Summary

We have SonarQube 7.7 running on Ubuntu 18.04 with Mysql 5.7 and Nginx

How to migrate Amazon RDS database from one region to another in AWS

We are going to see how to migrate RDS database from one region to another  We are going to achieve this by taking a snapshot of...

How to configure private key generated by PuTTYGen in Jenkins

Assume you have already create the public and private key using PuTTYGenPrivate and public keyWe need to connect Jenkins to the cloud server using this private...

App & Geek   Never miss a story from App & Geek, get updates in your inbox.