How To Install SonarQube on Amazon EC2 Linux?

How To Install SonarQube on Amazon EC2 Linux?

July 17, 2020 / Eternal Team

What is SonarQube?

SonarQube is a web-based open-source platform used to measure and analyse the source code quality. SonarQube is written in java but it can analyze and manage code of more than 20 programming languages, including c/c++, PL/SQL, Cobol etc through plugins.

Prerequisites

  • 1. Ubuntu 18.04 server with 3GB or more RAM
  • 2. Oracle Java 8 installed on the server
  • 3. Nginx and MySQL
  • 4. Certbot
  • 5. A fully-qualified domain name and an A record pointing to the server where you’ll install SonarQube.

Let’s get started…

Step 1

Login to the server and update the server.

$ sudo apt update && sudo apt upgrade -y

Step 2

Create a sonarqube user.

$ sudo adduser --system --no-create-home --group --disabled-login sonarqube

Step 3

Create the directory to install SonarQube into.

$ sudo mkdir /opt/sonarqube

Step 4

Install the unzip utility that will allow you to extract files.

$ sudo apt-get install unzip

Step 5

Log in to the MySQL server as the root user.

$ mysql -u root -p

Step 6

Then create the SonarQube database

CREATE DATABASE sonarqube;

Step 7

Now create the credentials that SonarQube will use to access the database change “your_password”

CREATE USER sonarqube@'localhost' IDENTIFIED BY 'your_password';

Step 8

Then grant permissions so that the newly created user can make changes to the SonarQube database.

GRANT ALL ON sonarqube.* to sonarqube@'localhost';

Step 9

Then apply the permission changes and exit the MySQL console.

FLUSH PRIVILEGES;
EXIT;

Installing SonarQube

Step 10

Navigate to the newly created folder.

$ cd /opt/sonarqube

Step 11

Download the SonarQube with the following command.

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

Step 12

Unzip the downloaded file.

$ unzip sonarqube-7.5.zip

Step 13

Change the permission of the extracted files.

$ sudo chown -R sonarqube:sonarqube /opt/sonarqube

Configuring the SonarQube Server.

Step 14

To configure we need to edit the sonar.properties.

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

Step 15

Change the username and password that SonarQube will use to access the database to the username and password you created for MySQL.

sonar.jdbc.username=sonarqube
sonar.jdbc.password=your_password

Step 16

Next, tell SonarQube to use MySQL as the database driver.

sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance&useSSL=false

Step 17

We could add the -server option to activate SonarQube’s server mode, which will help in maximizing performance.
Nginx will handle the communication between the SonarQube clients and your server, so you will tell SonarQube to only listen to the local address.

sonar.web.javaAdditionalOpts=-server
sonar.web.host=127.0.0.1

Step 18

To configure we need to edit the sonar.properties.

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

Step 19

Create the service file:

$ sudo nano /etc/systemd/system/sonarqube.service

Step 20

Add the following content to the file which specifies how the SonarQube service will start and stop.

[Unit]
Description=SonarQube service
After=syslog.target network.target
[Service]
Type=forking
ExecStart=/opt/sonarqube/sonarqube-7.5/bin/linux-x86-64/sonar.sh start
ExecStop=/opt/sonarqube/sonarqube-7.5/bin/linux-x86-64/sonar.sh stop
User=sonarqube
Group=sonarqube
Restart=always
[Install]
WantedBy=multi-user.target

Step 21

Close and save the file, then start the SonarQube service.

$ sudo service sonarqube start

Step 22

Check the status of the SonarQube service to ensure that it has started and is running as expected.

$ sudo service sonarqube status

Output

sonarqube.service - SonarQube service
Loaded: loaded (/etc/systemd/system/sonarqube.service; enabled; vendor preset
Active: active (running) since Sat 2019-01-05 19:00:00 UTC; 2s ago

Step 23

Next step is to configure the SonarQube service to start automatically on boot.

$ sudo systemctl enable sonarqube

Configuring the Reverse Proxy

Step 24

Start by creating a new Nginx configuration file for the site.

$ sudo nano /etc/nginx/sites-enabled/sonarqube

Step 25

Add this configuration so that Nginx will route incoming traffic to SonarQube.

server {
    listen 80;
    server_name sonarqube.example.com;
 
    location / {
        proxy_pass http://127.0.0.1:9000;
    }
}

Step 26

Next, make sure your configuration file has no syntax errors.

$ sudo nginx -i 

Step 27

Restart nginx server

$ sudo service nginx restart

You can now visit http://sonarqube.example.com in your web browser. You’ll be greeted with the SonarQube web interface.

Step 28

Now to secure install certbot to the server.

$ sudo certbot --nginx -d sonarqube.example.com

Configure the required details and you are ready to go.

AWS-Consulting-Partner

Talk to AWS Certified Consultant

Want to start a project?

It’s simple.

Contact us