Doyenhub

How to Set Up SonarQube Server on Linux Local System

SonarQube is an open-source static code analysis tool that helps developers manage code quality and consistency. It supports over 25 programming languages, including Java, C#, C++, Python, JavaScript, and more. Some advanced features are available under a commercial license.

Why Use SonarQube?

SonarQube performs various static code checks, including:

  • Detecting potential bugs and security vulnerabilities
  • Identifying over-complexity in the code
  • Enforcing coding best practices
  • Detecting code duplication
  • Integrating with Jenkins for automated testing

With its powerful features, such as code smells detection, execution path analysis, and branch analysis, SonarQube is a must-have for maintaining clean, efficient, and secure code.

Steps to Set Up SonarQube on a Linux Local System

1. Install and Configure PostgreSQL

  1. Add the PostgreSQL repository:
    sudo sh -c ‘echo “deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main” >> /etc/apt/sources.list.d/pgdg.list’\
  2. Install PostgreSQL:
    sudo apt-get -y install postgresql postgresql-contrib
     
  3. Start and enable PostgreSQL:
    sudo systemctl start postgresql
    sudo systemctl enable postgresql
  4. Set a password for the postgres user and create a new database user:
    sudo passwd postgres
    su – postgres
    createuser sonar
  5. Create the SonarQube database:
    psql
    CREATE DATABASE sonar OWNER sonar;
    ALTER USER sonar WITH ENCRYPTED PASSWORD ‘P@ssword’;
    \q

2. Download and Configure SonarQube

  1. Download the latest SonarQube version:
    wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-8.7.0.41497.zip
  2. Install unzip and extract SonarQube:
    sudo apt-get -y install unzip
    sudo unzip sonarqube-8.7.0.41497.zip -d /opt
    sudo mv /opt/sonarqube-8.7.0.41497 /opt/sonarqube
  3. Update permissions:
    sudo chown -R administrator:administrator /opt/sonarqube/
  4. Configure the database in the sonar.properties file: sonar.jdbc.username=sonar
    sonar.jdbc.password=P@ssword
    sonar.jdbc.url=jdbc:postgresql://localhost/sonar
    sonar.web.javaAdditionalOpts=-server

3. Configure Systemd Service

  1. Create a service file for SonarQube:
    sudo nano /etc/systemd/system/sonar.service
  2. Add the following content:
    [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
  3. Start and enable the service:sudo systemctl start sonar
    sudo systemctl enable sonar

4. Install and Configure Nginx

  1. Install Nginx: sudo apt-get install nginx -y
  2. Create an Nginx configuration file:
    sudo nano /etc/nginx/sites-enabled/sonarqube.conf
  3. Add the following configuration:
    server {
    listen 9000;
    server_name sonarqube.local;location / {
    proxy_pass http://127.0.0.1:9000;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
    }
  4. Restart Nginx: sudo systemctl restart nginx

Accessing SonarQube

Once the setup is complete, access SonarQube at http://localhost:9000. Use the default admin credentials to log in and change the password. Now, you can set up projects, scan code, and analyze results for better quality and security.

Also read – Secure data on server without installing Server side language

 

Leave a comment

Your email address will not be published. Required fields are marked *