Install of SonarQube on Ubuntu 22.04

Before installing packages, run the following apt command to update and refresh your Ubuntu package index repository.

Your first step is installing the Java OpenJDK on your Ubuntu system. The SonarQube server required Java OpenJDK v17 to be installed on your Linux machine.

​sudo apt update
sudo apt install -y openjdk-17-jdk
java -version


Install and Configure PostgreSQL Add the PostgreSQL repository.

The latest version of SonarQube required at least PostgreSQL v9.6. In this blog, we will install PostgreSQL v13 from the official PostgreSQL repository.

wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
sudo apt update
sudo apt install postgresql-13
sudo systemctl is-enabled postgresql
sudo systemctl status postgresql

With the PostgreSQL installed on your server, you can set up a new database and user for the SonarQube via the PostgreSQL shell.

Run the following command to log in to the PostgreSQL shell.

sudo -u postgres psql

Now, run the following PostgreSQL queries to create a new database and user for SnonarQube. In this example, you will create the PostgreSQL database and user 'sonarqube'.  Be sure to change the Password with a strong password.

CREATE USER sonarqube WITH PASSWORD 'Password';

CREATE DATABASE sonarqube OWNER sonarqube;

GRANT ALL PRIVILEGES ON DATABASE sonarqube TO sonarqube;

Setting Up System For SonarQube

To install SonarQube on a Linux system, you must have a dedicated user running SonarQube and some additional configurations, such as ulimit and kernel parameters.

Now, you will create a new user for SonarQube and set up custom kernel parameters via sysctl.conf file, and set up ulimit.

Run the following command to create a new user, 'sonarqube', on your system.

sudo useradd -b /opt/sonarqube -s /bin/bash sonarqube

sudo nano /etc/sysctl.conf

Add the following configuration to the bottom of the line. The SonarQube required the kernel parameter vm.max_map_count to be greater than '524288' and the fx.file-max to be greater than '131072'.

vm.max_map_count=524288
fs.file-max=131072

Save the file and exit the editor when you are finished.

Now, run the sysctl command below to apply new changes on the '/etc/sysctl.conf' file.

​sudo sysctl --system

Next, run the following command to set up ulimit for the SonarQube. This will take temporary effects on your system, when the system is rebooted, the ulimits will revert to default.

ulimit -n 131072
ulimit -u 8192

To make ulimit configuration permanently, create a new config file '/etc/security/limits.d/99-sonarqube.conf' using the following command.

sudo nano /etc/security/limits.d/99-sonarqube.conf

Add the following configuration to the file.

sonarqube   -   nofile   131072
sonarqube   -   nproc    8192

Save the file and close the editor when you are finished.

Now that you have completed configuring your Ubuntu system for SnonarQube installation. You will download the SonarQube package and set up the SonarQube installation in the next step.

Download SonarQube Package

We will install the SonarQube via the zip file package that you will download from the official SonarQube download page.

sudo apt install unzip software-properties-common wget

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

unzip sonarqube-9.9.4.87374.zip


Move the directory 'sonarqube-9.6.1.59531' to the '/opt/sonarqube' using the below command.

mv sonarqube-9.6.1.59531 /opt/sonarqube

Lastly, change the ownership of the SonarQube installation directory '/opt/sonarqube' to the user 'sonarquba' via the chown command as below.


sudo chown -R sonarqube:sonarqube /opt/sonarqube

Configuring SonarQube 

After downloading the SonarQube package, you will install the SonarQube by editing the default config file '/opt/SonarQube/conf/sonar. Properties'.

You will add the PostgreSQL database details, set up the max memory heap for the Elasticsearch process, and set up the web host and port for the SonarQube service via the file '/opt/SonarQube/conf/sonar. Properties'. And lastly, you will set up SonarQube as a system service.

Open the SonarQube configuration file '/opt/SonarQube/conf/sonar. Properties' using any editor.

nano /opt/sonarqube/conf/sonar.properties

For the database configuration, uncomment some of the following options and change the default value using your database details.

sonar.jdbc.username=sonarqube
sonar.jdbc.password=Password

sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube

​Now, uncomment the following configuration to set up the max heap memory size for the elasticsearch process. In his example, the max heap will be 512 MB.

sonar.search.javaOpts=-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError

Lastly, uncomment and change the following configurations to set up the IP address and port of the SonarQube will be running. Also, the log level will be 'INFO" and stored in the 'logs' directory of the SonarQube installation directory.

sonar.web.host=127.0.0.1
sonar.web.port=9000
sonar.web.javaAdditionalOpts=-server

sonar.log.level=INFO
sonar.path.logs=logs

Save the file and exit the editor when you are finished.

After you have finished the SonarQube configuration. Now, you will set up the systemd service file for SonarQube. This allows you to easily control the SonarQube process using the systemctl command.

Run the following command to create a new systemd service file '/etc/systemd/system/sonarqube.service'.

sudo nano /etc/systemd/system/sonarqube.service

Add the following configuration to the file.

[Unit]
Description=SonarQube service
After=syslog.target network.target
[Service]
Type=forking
User=sonarqube
Group=sonarqube
PermissionsStartOnly=true
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
StandardOutput=syslog
LimitNOFILE=131072
LimitNPROC=8192
TimeoutStartSec=5
Restart=always
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target

Save the file and exit the editor when you are done.

Now, reload the systemd manager by using the following command.

sudo systemctl daemon-reload

After that, start and enable the 'sonarqube.service' via the systemctl command below.

sudo systemctl start sonarqube.service
sudo systemctl enable sonarqube.service
sudo systemctl status sonarqube.service


Running SonarQube with Reverse Proxy

Your SonarQube installation is running, you can now install the Nginx web server and set up server blocks that will be used as the reverse proxy for SonarQube.

Run the following apt command to install the Nginx web server to your Ubuntu system. Input Y when prompted to confirm the installation and press ENTER to proceed.

sudo apt install nginx
sudo systemctl is-enabled nginx
sudo systemctl status nginx

After you have the Nginx web server is running, you will create a new server block configuration that will be used as a reverse proxy for SonarQube.

Create a new server blocks configuration '/etc/nginx/sites-available/sonarqube.conf' using the following command.

sudo nano /etc/nginx/sites-available/sonarqube.conf

​Add the following configuration to your file and be sure to change the domain name.

server {

    listen 80;
    server_name Your_sever_ip_or_domain_name;
    access_log /var/log/nginx/sonar.access.log;
    error_log /var/log/nginx/sonar.error.log;
  
    location / {
        proxy_pass http://127.0.0.1:9000;
        proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
        proxy_redirect off;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto http;
    }
}

Save the file and exit the editor when you are finished.

Next, activate the server block configuration 'sonarqube.conf' by creating a symlink of that file to the '/etc/nginx/sites-enabled' directory. Then, verify your Nginx configuration files.

sudo ln -s /etc/nginx/sites-available/sonarqube.conf /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl restart nginx

After you have finished the reverse proxy configuration for SonarQube, you can now access your SonarQube installation via your domain and set up some basic SonarQube configurations.

Now, you will get the SonarQube login page. Input the default username and password admin/admin and click Login.

While testing our code manually on another virtual machine using a sonar scanner, when we try to push our code scan report from that VM to the sonarQube VM, it shows a connection error 

When we check the nginx log we found that the report file size is larger than the allow limit of nginx .

siddharth@sonarcube-instance:~$ tail -f /var/log/nginx/sonar.error.logerror:

 2024/02/19 03:31:13 [error] 12704#12704: *131120 client intended to send too large body: 64228393 bytes, client: 35.195.27.132, server: 34.144.215.92, request: "POST /api/ce/submit?projectKey=healtharx_portal_AY26xXw47PaCpIlhws4L HTTP/1.1", host: "34.144.215.92"

Nginx by default allow only 1MB file size from client side . So In that case we change the nginx sonarqube.conf file to this and add Nginx by default allow only 1MB file size from client side . So In that case we change the nginx sonarqube.conf file to this and add 

client_max_body_size 100M

sudo vim /etc/nginx/sites-available/sonarqube.conf


server {
listen 80;
server_name Your_sever_ip_or_domain_name;
access_log /var/log/nginx/sonar.access.log;
error_log /var/log/nginx/sonar.error.log;
#Increase the allowed size for uploads if needed
client_max_body_size 100M;
location / {
proxy_pass http://127.0.0.1:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection "";
# Adjust the following timeout settings based on your requirements
proxy_connect_timeout 1800;
proxy_send_timeout 1800;
proxy_read_timeout 1800;
send_timeout 1800;
# Enable gzip compression for improved performance
gzip on;
gzip_types text/plain text/css application/javascript application/json;
}
# Additional configuration if needed...
}
~
~
~

After saving the configuration 

sudo systemctl restart nginx