Setup SonarQube in Ubuntu 18.04.

share on:
Setup SonarQube in Ubuntu 18.04.

According to wikipedia:


SonarQube (formerly Sonar)[1] is an open-source platform developed by SonarSource for continuous inspection of code quality to perform automatic reviews with static analysis of code to detect bugs, code smells, and security vulnerabilities on 20+ programming languages. SonarQube offers reports on duplicated code, coding standards, unit tests, code coverage, code complexity, comments,bugs, and security vulnerabilities.[

We will install and setup sonarqube in ubutnu 18.04 instance and use apache to reverse proxy to this sonarqube instance to access it over port 80.

Step 1: create a user for SonarQube.

useradd -M -r -s /usr/sbin/nologin sonar

Setup system. Install java 8 and other required versions.

apt-get update
apt-get upgrade -y
apt-get install unzip wget net-tools openjdk-8-jre -y


Step 2: Install PostgreSQL in Ubntu18.04

Add PostgreSQL repository in Ubuntu 18.04.

apt-get update
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - 
sudo apt-get update 


Install PostgreSQL version of your choice.

apt-get install postgresql-11 -y

Create a user for SonarQube in postgresql.
su - postgres
createuser sonar

Create a database and make user ‘sonar’ owner of that database.

psql
ALTER USER sonar WITH ENCRYPTED password '<Very strong password>';
CREATE DATABASE sonar OWNER sonar;

Step 3 — Download and Install SonarQube 7.5.

Start by changing the current working directory to the SonarQube installation directory:

cd /opt/sonarqube

You can visit SonarQube downloads page and get the download link for SonarQube 7.5. We are going to set 7.5 as its compatible with Java 8.

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

Unzip the file:

unzip sonarqube-7.5.zip -d /opt/

Change the name of /opt/sonarqube-7.5 to /opt/sonar

cd /opt
mv sonarqube-7.5 sonar

Finally, update the permissions so that the sonar user will own these files, and be able to read and write files in this directory:

chown -R sonar:sonar /opt/sonar

Now we can start configuring the SonarQube server.

Make changes to /opt/sonar/conf/sonar.properties. Add these lines at the end of file.

sonar.jdbc.username=sonar
sonar.jdbc.password=(sonar user's postgres password)
sonar.jdbc.url=jdbc:postgresql://localhost/sonar
sonar.web.javaAdditionalOpts=-server
sonar.web.port=30000
sonar.web.host=127.0.0.1

These settings will make SonarQube run in server mode on port 30000 on localhost.

Step 4. Manage SonarQube service with Systemd.

vi /etc/systemd/system/sonar.service

[Unit]
Description=SonarQube service
After=syslog.target network.target

[Service]
Type=forking

ExecStart=/opt/sonar/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonar/bin/linux-x86-64/sonar.sh stop

User=sonar
Group=sonar
Restart=always

[Install]
WantedBy=multi-user.target

Start SonarQube service.

systemctl daemon-reload
systemctl enable sonar
systemctl start sonar && systemctl status sonar

Step 5: Install and setup Apache as Reverse proxy for SonarQube.

apt-get update
apt-get install apache2
a2enmod rewrite proxy proxy_http headers

Configure Virtualhost for SonarQube in Apache. Change the name of domain sonar01.debyum.com in virtualhost configuration below.

cd /etc/apache2/sites-available

<VirtualHost *:80>
  ServerName    sonar01.debyum.com
  ServerAdmin   [email protected]

    ProxyRequests Off
    ProxyPreserveHost On
 
    AllowEncodedSlashes NoDecode


    ProxyPass / http://localhost:30000/ nocanon
    ProxyPassReverse / http://localhost:30000/
    ProxyPassReverse / http://sonar01.debyum.com/


    LogFormat "%{X-Forwarded-For}i %l %u %t \"%r\" %>s %b" common_forwarded
    ErrorLog /var/log/apache2/sonar01-dev.debyum.com_error.log
    CustomLog /var/log/apache2/sonar01-dev.debyum.com_forwarded.log common_forwarded
    CustomLog /var/log/apache2/sonar01-dev.debyum.com_access.log combined env=!dontlog
    CustomLog /var/log/apache2/sonar01-dev.debyum.com.log combined

</VirtualHost>

Enable the VirtualHost.

a2ensite sonar
systemctl reload apache2

Now you should be able to access sonarqube by visiting the url http://sonar01.<yourdomain> in your browser.

Thanks for reading. Hope this post on setup sonarqube in ubutnu 18.04 was helpful to you. Please leave your feedback.

share on:
engy

engy

Hello there, My name is Rishi Guleria and I work as a Linux system administrator. I have created this blog to share what I have learned so far and to learn new things. Don't forget to leave the feedback. Have a great day. :)

Leave a Response

share on: