7

Install SonarQube Code Review Tool in Rocky Linux 8

 2 years ago
source link: https://computingforgeeks.com/install-sonarqube-code-review-tool-in-rocky-linux/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Install SonarQube Code Review Tool in Rocky Linux 8

SonarQube® is an automatic code review tool to detect bugs, vulnerabilities, and code smells in your code. It can integrate with your existing workflow such as Jenkins to enable continuous code inspection across your project branches and pull requests.

In this brief guide, we shall be installing this fantastic Open Source tool so that you can have the opportunity to review your team’s code before they are deployed in production. It will help in streamlining your applications as well as improving in their security by detecting outdated software used and making apt recommendations on the fly.

Pre-requisites

  • SonarQube is built on Java, so we shall ensure that Java 11 is installed
  • Another user apart from root to run elasticsearch hence SonarQube
  • PostgreSQL

To have this tool installed in your Rocky Linux 8, follow the steps shared below:

Step 1: Update and install required tools and fulfill system settings

In this step, ensure that your server is well updated as well as install all tools you will require during the installation process. We shall also tweak system settings such as SELinux, max_map_count and fs.file-max. Run the commands below to update your server.

sudo yum update
sudo yum install vim wget curl -y

Configure SELinux as Permissive

This can be done by running the commands below:

sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config

Tweak max_map_count and fs.file-max

From Linux Kernel Documentation, this file contains the maximum number of memory map areas a process may have. Memory map areas are used as a side-effect of calling malloc, directly by mmap, mprotect, and madvise, and also when loading shared libraries.

To tweak the settings to befit SonarQube requirements, open “/etc/sysctl.conf” file and add the settings as shown below:

$ sudo vim /etc/sysctl.conf
vm.max_map_count=262144
fs.file-max=65536

Then reload sysctl configuration as follows:

sudo sysctl --system

Create a user for sonar

It is recommended that a separate user is created to run SonarQube. Let us create one as follows:

sudo useradd sonar

Then set a password for the user

sudo passwd sonar

Step 2: Install Java 11 on Rocky Linux 8

As it had been mentioned in the introductory section, SonarQube is written in Java and it needs Java installed (11 particularly in this setup).

sudo yum install java-11-openjdk-devel

Step 3: Install and configure PostgreSQL

In this example guide, we are going to install PostgreSQL 13 server on the same sever SonarQube will reside. You can host it in a different server depending on your needs. To install PostgreSQL 13 on your Rocky Linux 8 Server, follow the steps in the guide shared below to get it up and running real quick. You can ignore Install pgAdmin 4 Web interface in Step 6 in the guide if you do not need it.

Install PostgreSQL 13 on Rocky Linux 8

After the installation is done and we can connect to our Postgres Database successfully, we can go on to create a user and database for SonarQube.

Create SonarQube user and database

Here, we are going to create a user for SonarQube. Proceed as shown below before exiting your database.

postgres=# create user sonar;
postgres=# create database sonar_db owner sonar;
postgres=# grant all privileges on database sonar_db to sonar;

Set a password for the sonar user created

postgres=# ALTER USER sonar WITH ENCRYPTED password 'StrongPassword';

Step 4: Fetch and install SonarQube

Now we are at the point we have been waiting to arrive at for a long time. We shall download Long Term Release of SonarQube then install in our Rocky Linux Server. Proceed as follows to get our SonarQube installed.

Fetch SonarQube LTS Version

You can visit SonarQube Downloads Page to view their various offerings. We shall be downloading the Long Term Release (LTS) which at the time of writing was sonarqube-8.9.

cd /opt/
sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.9.1.44547.zip

Then unzip the file

sudo yum -y install unzip
sudo unzip sonarqube-8.9.1.44547.zip

After that, rename the folder to sonarqube

sudo mv sonarqube-8.9.1.44547 sonarqube

Step 5: Configure SonarQube on Rocky Linux 8

Once the files have been extracted to /opt/ directory, it is time to configure the application.

Open “/opt/sonarqube/conf/sonar.properties” file and add database details as shown below. In addition to that, find the lines shared and uncomment them.

$ sudo vim /opt/sonarqube/conf/sonar.properties

\##Database details
sonar.jdbc.username=sonar
sonar.jdbc.password=StrongPassword
sonar.jdbc.url=jdbc:postgresql://localhost/sonar_db

\##How you will access SonarQube Web UI
sonar.web.host=192.168.171.86
sonar.web.port=9000

\##Java options
sonar.web.javaOpts=-Xmx512m -Xms128m -XX:+HeapDumpOnOutOfMemoryError
sonar.search.javaOpts=-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError

\##Also uncomment the following Elasticsearch storage paths
sonar.path.data=data
sonar.path.temp=temp

Give SonarQube files ownership to the sonar user we created in Step 1.

sudo chown -R sonar:sonar /opt/sonarqube

In case Java cannot be found in the default location, you will have to specify the binary files for SonarQube to find. You can specify where java is located in the “/opt/sonarqube/conf/wrapper.conf” file. Look for “wrapper.java.command” line and place your Java location beside it thus.

sudo vim /opt/sonarqube/conf/wrapper.conf

wrapper.java.command=/usr/local/jdk-11.0.2/bin/java

Add SonarQube SystemD service file

Finally we are going to ensure that we shall be able to manage our SonarQube application via Systemd so that we can start and stop it like other services in your server

$ sudo vim /etc/systemd/system/sonarqube.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=65536
LimitNPROC=4096
User=sonar
Group=sonar
Restart=on-failure

[Install]
WantedBy=multi-user.target

After editing systemd files, we have to reload them so that they can be read and loaded.

sudo systemctl daemon-reload

Then start and enable the service

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

Check its status if it successfully started and is running.

$ sudo systemctl status sonarqube.service
● sonarqube.service - SonarQube service
   Loaded: loaded (/etc/systemd/system/sonarqube.service; enabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-07-27 18:49:18 EAT; 6s ago
  Process: 43024 ExecStart=/opt/sonarqube/bin/linux-x86-64/sonar.sh start (code=exited, status=0/SUCCESS)
 Main PID: 43073 (wrapper)
    Tasks: 36 (limit: 4522)
   Memory: 548.3M
   CGroup: /system.slice/sonarqube.service
           ├─43073 /opt/sonarqube/bin/linux-x86-64/./wrapper /opt/sonarqube/bin/linux-x86-64/../../conf/wrapper.conf wrapper.sy>           ├─43075 /usr/local/jdk-11.0.2/bin/java -Dsonar.wrapped=true -Djava.awt.headless=true -Xms8m -Xmx32m -Djava.library.p>           └─43097 /usr/local/jdk-11.0.2/bin/java -XX:+UseConcMarkSweepGC -XX:CMSInitiatingOccupancyFraction=75 -XX:+UseCMSInit

Step 6: Alter Firewall rules to allow SonarQube Access

At this juncture, sonarqube service should be running. In case you cannot access the web interface, visit the log files located in “/opt/sonarqube/logs” where you will find:

  • elasticsearch logs (es.log)
  • Sonar logs (sonar.log)
  • web logs (web.log)
  • Access logs (access.log)
  • And others

As you can remember, we enabled SonarQube web to listen on port 9000. For everything to work, we should allow this port on the firewall. Proceed to do this by running the command shared below.

sudo firewall-cmd --permanent --add-port=9000/tcp && sudo firewall-cmd --reload

Step 7: Access the Web User Interface

The time we have been waiting for has finally showed up. We are now ready to access SonarQube interface and begin assessing our code for security. To access the interface, open your favorite browser and point it to http://server-ip-or-fqdn:9000. You should see a page similar to the one below.

Step 8: Logging in

To log in, simply click on the “Log In” button and you should be ushered in a page similar to the one shared below. Use username as “admin” and password as “admin“.

SonarQube Login

After that, you will be asked to update the password to a new one. Enter new Administrator Password and Proceed to login.

You should be ushered into the main area as illustrated below:

Final Musings

Now we have our automatic code review tool that you can use to scan various applications before they are approved for production. It is simple, thorough and complements your organizational security needs. Try it out.We appreciate your visit and for the tremendous support you continue to extend. Other guides you might enjoy are listed below:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK