Wednesday, February 8, 2023

How to Setup SonarQube on a Virtual Machine in Azure Cloud | Install SonarQube on Ubuntu 22.0.4 Virtual Machine in Azure Cloud

SonarQube is one of the popular static code analysis tools. SonarQube enables developers to write cleaner, safer code. SonarQube is open-source, Java based tool. SonarQube uses database for storing analysis results. Database can be MS SQL, Oracle or PostgreSQL.  We will use PostgreSQL as it is open source as well.

SonarQube Architecture:



SonarQube have three components namely
1. Scanner - This contains scanner and analyser to scan application code.
2. SonarQube server - contains Webserver(UI) and search server 
3. DB server - used for storing the analysis reports.

Watch steps in YouTube channel:

Please find steps for installing SonarQube on Ubuntu 22.0.4 in Azure Cloud. Make sure port 9000 is opened in firewall rules.

Click here to learn How to setup Virtual Machine in Azure cloud.

How to open port 9000 in Azure VM?

1. Select VM, under Settings--> choose Network


2. Click on Add inbound security role

 
3. Make sure you add entries like below: 
9000 as Destination port ranges
TCP as protocol
310 as priority number
port_9000 as Name

 4. Click on Add, once you add it should be like below:


Pre-requisites:
Instance should have at least 4 GB RAMMake sure port 9000 is opened as port 9000 is default port for SonarQube.

  • Make sure below is taken care off.
Login to instance where you will be installing SonarQube, perform the below command:
sudo vi /etc/sysctl.conf

Add the following lines to the bottom of that file:

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

To make sure changes are getting into effect:
sudo sysctl -p

Change Host Name to SonarQube
sudo hostnamectl set-hostname SonarQube

Perform System update
sudo apt update

Install Docker-Compose
sudo apt install docker-compose -y

Create docker-compose.yml
this yml has all configuration for installing both SonarQube and Postgresql:
sudo vi docker-compose.yml 
(Copy the entire content high lighted below in yellow color)

version: "3"
services:
  sonarqube:
    image: sonarqube:community
    restart: unless-stopped
    depends_on:
      - db
    environment:
      SONAR_JDBC_URL: jdbc:postgresql://db:5432/sonar
      SONAR_JDBC_USERNAME: sonar
      SONAR_JDBC_PASSWORD: sonar
    volumes:
      - sonarqube_data:/opt/sonarqube/data
      - sonarqube_extensions:/opt/sonarqube/extensions
      - sonarqube_logs:/opt/sonarqube/logs
    ports:
      - "9000:9000"
  db:
    image: postgres:12
    restart: unless-stopped
    environment:
      POSTGRES_USER: sonar
      POSTGRES_PASSWORD: sonar
    volumes:
      - postgresql:/var/lib/postgresql
      - postgresql_data:/var/lib/postgresql/data
volumes:
  sonarqube_data:
  sonarqube_extensions:
  sonarqube_logs:
  postgresql:
  postgresql_data:

Save the file by entering :wq!

Now execute the compose file using Docker compose command:
sudo docker-compose up -d 


Make sure SonarQube is up and running by checking the logs
sudo docker-compose logs --follow


Once you see the message, that's it. SonarQube is been installed successfully. press control C and enter.
Now access sonarQube UI by going to browser and enter public dns name with port 9000

No comments:

Post a Comment

How to Configure GitHub Advanced Security for Azure DevOps | How to Perform Security scan for Azure Repos using GitHub Advanced Security

GitHub Advanced Security for Azure DevOps brings the  secret scanning, dependency scanning  and  CodeQL code scanning  solutions already ava...