Lecture 04-b – SONARQUBE POSTGRESQL DB INSTALLATION
SONARQUBE POSTGRESQL DB INSTALLATION
SonarQube is a widely adopted open-source static code analysis tool that empowers developers to enhance code quality and security. It operates on a Java-based platform and utilizes a database to store analysis results. Various databases such as MS SQL, Oracle, and PostgreSQL are supported, and we will utilize PostgreSQL due to its open-source nature.
By leveraging SonarQube, developers can achieve cleaner and safer code through its comprehensive range of analysis capabilities.
SonarQube comprises three key components that work together to provide a comprehensive code analysis solution:
Scanner: The Scanner component includes the scanner and analyzer tools responsible for scanning the application’s source code. It analyzes the codebase and collects relevant metrics, identifies code issues, and measures code quality against predefined rules and guidelines.
SonarQube Server: The SonarQube Server component consists of the web server, which provides the user interface (UI) for SonarQube. It enables users to view analysis results, access project dashboards, configure quality profiles, and manage code quality-related activities. The SonarQube Server also includes the search server, which facilitates fast and efficient searching of analysis data.
Database Server: The Database Server serves as the backend storage for SonarQube’s analysis reports and associated data. It stores the collected metrics, code issues, and other analysis results.
PRE-REQUISITES
Launch Ubuntu Instance at least 2 GB RAM.
Ensure to have port# 9000 or ALL TRAFFIC open in AWS security group
Step 01: To update server and installs open Java Development Kit (JDK).
COPY & RUN –>$ the below command
sudo apt-get update && sudo apt-get install default-jdk -y
To install and Download Postgres DB
Step 02: To add a new package repository entry for PostgreSQL to the system’s package manager configuration.
COPY & RUN –>$ the below command
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
To download the PostgreSQL repository’s public key from the specified URL, and then adds the key to the system’s list of trusted keys.
COPY & RUN –>$ the below command
sudo wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
To installs the PostgreSQL database server (postgresql) and additional contributed modules (postgresql-contrib) using the system’s package manager.
COPY & RUN –>$ the below command
sudo apt-get -y install postgresql postgresql-contrib
To start Postgresql and enable it
COPY & RUN –>$ the below commands
sudo systemctl start postgresql
sudo systemctl enable postgresql
Step 03: To login and switch to the user postgres with elevated privileges and to create user.
COPY & RUN –>$ the below commands
sudo su - postgres
createuser sonar
To switch to sql shell and grant privileges, modify user credentials to ensure secure access to the specified user account.
COPY & RUN –>$ the below command
psql
ALTER USER sonar WITH ENCRYPTED password 'admin123';
CREATE DATABASE sonarqube OWNER sonar;
GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar;
\q
Type exit to come out of postgres
To Download and Install SonarQube scanner
Step 04: To download SonarQube distribution file using wget.
COPY & RUN –>$ the below command
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.6.0.39681.zip
To install unzip and extract the contents of the SonarQube ZIP file into the /opt directory.
COPY & RUN –>$ the below commands
sudo apt-get -y install unzip
sudo unzip sonarqube*.zip -d /opt
To rename and move sonarqube downloaded file into /opt/sonarqube directory
COPY & RUN –>$ the below command
sudo mv /opt/sonarqube-8.6.0.39681 /opt/sonarqube -v
Step 05: To create and assign to the primary sonarGroup, and change the ownership directory.
COPY & RUN –>$ the below commands
sudo groupadd sonarGroup
sudo useradd -c "user to run SonarQube" -d /opt/sonarqube -g sonarGroup sonar
sudo chown sonar:sonarGroup /opt/sonarqube -R
To edit sonar.properties file using vi editor.
COPY & RUN –>$ the below command
sudo vi /opt/sonarqube/conf/sonar.properties
scroll down and find sonar.jdbc usename and password, remove # and add the value like below.
sonar.jdbc.username=sonar
sonar.jdbc.password=admin123
Add the below line under jdbc postgres localhost
sonar.jdbc.url=jdbc:postgresql://localhost/sonarqube
Press esc and enter :wq! OR Cntrl ZZ to save and exit.
Step 06: Modify sonar script file, uncomment # and set RUN_AS_USER=sonar
COPY & RUN –>$ the below command
sudo vi /opt/sonarqube/bin/linux-x86-64/sonar.sh
Press esc and enter :wq! OR Cntrl ZZ to save and exit.
To setup SonarQube as a service and enable to start automatically when you restart the server.
COPY & RUN –>$ the below commands
sudo vi /etc/systemd/system/sonar.service
[Unit]
Description=SonarQube service
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/bin/linux-x86-64/sonar.sh stop
LimitNOFILE=131072
LimitNPROC=8192
User=sonar
Group=sonarGroup
Restart=always
[Install]
WantedBy=multi-user.target
Press esc and enter :wq! OR Cntrl ZZ to save and exit.
Step 07: To make changes to Kernel System according to your requirements.
COPY & RUN –>$ the below command
It’s important to exercise caution while modifying kernel parameters, as incorrect changes can impact system stability or performance.
sudo vi /etc/sysctl.conf
Add the following lines to the bottom of the file,
vm.max_map_count=262144
fs.file-max=65536
Press esc and enter :wq! OR Cntrl ZZ to save and exit.
To edit limits.conf. and add the following at the end of this file.
COPY & RUN –>$ the below command
sudo vi /etc/security/limits.conf
Add the following lines to the bottom of the file.
sonar - nofile 65536
sonar - nproc 4096
Press esc and enter :wq! OR Cntrl ZZ to save and exit.
Step 08: To reload system level changes without server boot
COPY & RUN –>$ the below command
sudo sysctl -p
To start, enable and check SonarQube status
COPY & RUN –>$ the below commands
sudo systemctl start sonar
sudo systemctl enable sonar
sudo systemctl status sonar
Press q to exit this mode.
To verify if Sonarqube is up and running
COPY & RUN –>$ the below command
tail -f /opt/sonarqube/logs/sonar*.log
Step 09: To verify the output, go to the browser using Sonarqube server IP:9000 and login with username and password below:
user: admin
password: admin
It will prompt to update your password, Use admin123
You should see your SonarQube scanner dashboard like below.
