How to install SonarQube on CentOS 7

SONARQUBE
      SonarQube is an open source tool for quality system development. It is written in Java and supports multiple databases. It provides capabilities to continuously inspect the code, show the health of an application, and highlight newly introduced issues. It contains code analyzers which are equipped to detect tricky issues. It also integrates easily with DevOps.

PREREQUISITES
  • User with sudo permission
  • CentOS 7 server instance with at least 2 GB RAM
SYSTEM UPDATES
Before installing any packages in CentOS, it is recommended to update the system by running the following commands
  • sudo yum -y install epel-release
  • sudo yum -y update
  • sudo shutdown -r now
Note: Make sure to get the latest version of Java, PostgreSQL, SonarQube and Sonar-Scanner

INSTALL JAVA

Make sure to get the latest JDK package. Download the SE JDK RPM package
wget --no-cookies --no-check-certificate --header "Cookie:oraclelicense=accept-securebackup-cookie" "http://download.oracle.com/otn-pub/java/jdk/8u131-b11/d54c1d3a095b4ff2b6607d096fa80163/jdk-8u131-linux-x64.rpm"
Install the downloaded package
sudo yum -y localinstall jdk-8u131-linux-x64.rpm
Check the version of Java
java -version
INSTALL AND CONFIGURE POSTGRESQL
Install PostgreSQL repo
sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg- centos96-9.6-3.noarch.rpm
Install PostgreSQL db server
sudo yum -y install postgresql96-server postgresql96-contrib   
Initialize the database
sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb
Edit the pg_hba.conf to enable MD5-based authentication
sudo nano /var/lib/pgsql/9.6/data/pg_hba.conf
In the pg_hba.conf  file, go to last section one table will be there, where you have to change the METHOD column from peer to trust and idnet to md5 respectively

After updating that file, the table configuration file looks like this

pg_hba.conf file

Start PostgreSQL server and enable it to start automatically at boot time
sudo systemctl start postgresql-9.6sudo systemctl enable postgresql-9.6
Change the password for the default PostgreSQL user
sudo passwd postgres
Switch to the Postgres user
su - postgres
Create a new user
createuser sonarqube
Switch to the PostgreSQL shell
psql
Set a password for the newly created user for SonarQube database
ALTER USER sonar WITH ENCRYPTED password ‘<New Password>’;
Create a new database for PostgreSQL database
CREATE DATABASE sonarqube OWNER sonarqube;
Exit from the psql shell
\q
Switch back to the sudo user by running the exit command
exit
DOWNLOAD AND CONFIGURE SONARQUBE SERVER
Click here and choose the latest sonarqube server
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.4.zip
Unzip the SonarQube by (If you  don’t have zip package means, install it by sudo yum -y install unzip)
sudo unzip sonarqube-6.4.zip -d /opt
Rename the directory by
sudo mv /opt/sonarqube-6.4 /opt/sonarqube
Open the SonarQube configuration file using your nano text editor.
sudo nano /opt/sonarqube/conf/sonar.properties
Find the following lines
sonar.jdbc.username 
sonar.jdbc.password
Uncomment and give the PostgreSQL username and password of the database that we have created earlier
sonar.jdbc.username=sonarqube 
sonar.jdbc.password=<password>
And then find the
#sonar.jdbc.url      
Uncomment the line, save the file and exit from the editor.

CONFIGURE SYSTEMD SERVICE
Open the sonar.service file in your editor
 sudo nano /etc/systemd/system/sonar.service
Populate the file with
[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
User=root
Group=root
Restart=always
[Install]
WantedBy=multi-user.target
After updating that file, the file should  look like this

sonar.service file
sonar.service

Start the application by
sudo systemctl start sonar
Enable the SonarQube service to automatically start at boot time
sudo systemctl enable sonar
Check the SonarQube service status
sudo systemctl status sonar
Likewise, you can stop or restart the service
sudo systemctl stop sonar 

sudo systemctl restart sonar
CONFIGURE FIREWALL
In order to allow the clients to access the SonarQube, you need to set inbound traffic on specified port
Syntax: sudo firewall-cmd --add-service=http --permanent <port number>/tcpeg: sudo firewall-cmd --zone=public --permanent --add-port=9000/tcp
After adding the port details in firewall, you must reload your firewall
sudo firewall-cmd –reload
Checking your port by
sudo firewall-cmd --list-ports
Start the sonar by
sudo systemctl start sonar

SonarQube is installed on your server, access the dashboard at the following address.
http://<your_ip:port_number>

Default port number of SonarQube is 9000

If you want to change the port number, change the sonar.web.port=9000 in sonar configuration file
Log in using the initial administrator account, admin and admin.

Reference: https://docs.sonarqube.org/display/SONAR/Installing+the+Server

Comments

Post a Comment