newcohospitality.com

Harnessing SonarQube on AWS EC2: A Comprehensive Setup Guide

Written on

Unlock the potential of your codebase using SonarQube on AWS EC2, merging advanced technology with cloud adaptability to elevate your software quality and performance.

Introduction

SonarQube serves as a robust solution for maintaining code quality, offering developers thorough insights into code vulnerabilities and issues. By setting up SonarQube on AWS EC2, you can take advantage of the cloud’s scalability and flexibility to improve your software development workflow. This guide provides a step-by-step approach to establishing SonarQube on an AWS EC2 instance, enabling you to confidently deliver high-quality code.

Specifications

  1. Ubuntu 22.04 / 20.04 LTS

  2. CPU: 2 vCPU

  3. Volume: 20 GB

  4. RAM: 4 GB

  5. Security Ports (see below)

    The specifications listed are the minimum; feel free to adjust them based on your requirements.

Step 1: Accessing Your Ubuntu EC2 Instance

  1. Connect to your AWS EC2 Ubuntu server via SSH:

    chmod 600 "key-pair-file"

    ssh -i "key-pair-file" ubuntu@ec2-Public-IP.ap-south-1.compute.amazonaws.com

You are now connected to your instance.

  1. First, update the packages on your instance:

    sudo apt update

    sudo apt upgrade -y

Step 2: Installing Java and PostgreSQL

  1. To fully utilize SonarQube, ensure OpenJDK 17 is installed:

    sudo apt install -y openjdk-17-jdk

  2. Check the installed Java version:

    java -version

  3. Begin by adding 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'

  4. Next, add the PostgreSQL signing key:

    wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

  5. Install PostgreSQL:

    sudo apt install postgresql postgresql-contrib -y

  6. Enable the database server to start automatically on boot:

    sudo systemctl enable postgresql

  7. Start the database server:

    sudo systemctl start postgresql

  8. Check the status of the database server:

    sudo systemctl status postgresql

  9. Validate the installed version of PostgreSQL:

    psql --version

  10. Switch to the Postgres user:

    sudo -i -u postgres

Step 3: Configuring the Database

  1. Create a new database user for managing the sqube database:

    createuser new_user

For instance, to create the "sona" user:

createuser sona
  1. Access the PostgreSQL database:

    psql

  2. Assign a secure password to the "sona" user. Replace my_password with your chosen password:

    ALTER USER [Created_user_name] WITH ENCRYPTED password 'my_password';

For example:

ALTER USER sona WITH ENCRYPTED password 'Sona#123';
  1. Create the sqube database and assign its ownership to the "sona" user:

    CREATE DATABASE [database_name] OWNER [Created_user_name];

Example:

CREATE DATABASE sqube OWNER sona;
  1. Grant all privileges on the "sqube" database to the "sona" user:

    GRANT ALL PRIVILEGES ON DATABASE sqube to sona;

  2. Verify the database creation:

    l

  3. Check the created user:

    du

  4. Exit the PostgreSQL command-line:

    q

  5. Return to your non-root sudo user account:

    exit

Step 4: Installing SonarQube

  1. Install the zip utility necessary for extracting SonarQube files:

    sudo apt install zip -y

  2. Download the latest version of SonarQube from the official website:

[Download SonarQube](https://www.sonarsource.com/products/sonarqube/downloads/)

For our setup, we will download the SonarQube 10.4 Community Edition:

sudo wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-10.4.1.88267.zip
  1. Unzip the downloaded file:

    sudo unzip sonarqube-10.4.1.88267.zip

  2. Move the unzipped files to the /opt/sonarqube directory:

    sudo mv sonarqube-10.4.1.88267 sonarqube

    sudo mv sonarqube /opt/

Step 5: Configuring SonarQube User and Group

  1. Create a dedicated user and group for SonarQube, preventing it from running as root:

    sudo groupadd sona

    sudo useradd -d /opt/sonarqube -g sona sona

  2. Grant the "sona" user access to the /opt/sonarqube directory:

    sudo chown -R sona:sona /opt/sonarqube

Step 6: Configuring SonarQube for Optimal Performance

  1. Edit the SonarQube configuration file located at /opt/sonarqube/conf/sonar.properties:

    sudo nano /opt/sonarqube/conf/sonar.properties

  2. Uncomment the following lines and add your database credentials:

    sonar.jdbc.username=sona

    sonar.jdbc.password=Sona#123

  3. Specify the JDBC URL:

    sonar.jdbc.url=jdbc:postgresql://localhost:5432/sqube

  4. Save and exit the configuration file.

  5. Edit the SonarQube script file:

    sudo nano /opt/sonarqube/bin/linux-x86-64/sonar.sh

  6. Specify the user to run SonarQube:

    RUN_AS_USER=sona

  7. Save and exit the script file.

Step 7: Creating a systemd Service for SonarQube

  1. Create a systemd service file for SonarQube:

    sudo nano /etc/systemd/system/sonar.service

  2. Add the following content to the service file:

    [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=sona

    Group=sona

    Restart=always

    LimitNOFILE=65536

    LimitNPROC=4096

    [Install]

    WantedBy=multi-user.target

  3. Save and close the file.

  4. Enable the SonarQube service:

    sudo systemctl enable sonar

  5. Start the SonarQube service:

    sudo systemctl start sonar

  6. Check the status of the SonarQube service:

    sudo systemctl status sonar

  7. Congratulations! Your SonarQube service is now up and running.

Step 8: Adjusting System Limits for Performance

  1. Edit the sysctl configuration file to ensure proper functionality with Elasticsearch:

    sudo nano /etc/sysctl.conf

  2. Add the following lines to the configuration file:

    vm.max_map_count=262144

    fs.file-max=65536

    ulimit -n 65536

    ulimit -u 4096

  3. Save and exit the configuration file.

  4. Reboot the system to apply changes:

    sudo reboot

Step 9: Accessing SonarQube

  1. To access SonarQube, open your web browser and enter your server's IP address followed by port 9000:

For instance, http://IP:9000.

  1. Use "admin" as both the username and password.
  2. To change your password:
  1. Log in with the credentials "admin" / "admin".
  2. You will be prompted to change your password. Enter "admin" for the current password and your new password twice.
  3. Click "Change password" to finalize the update.

Your password is now changed successfully.

Congratulations on successfully installing SonarQube Community Edition 10.0 on AWS EC2 Ubuntu 22! If you have any questions or need assistance, don’t hesitate to ask. Enjoy coding!

Conclusion

You have now completed the installation of SonarQube Community Edition 10.4 on AWS EC2 Ubuntu 22/20. This achievement opens new avenues for enhancing code quality and refining development workflows. Should you face any difficulties or need further help, feel free to reach out. Let’s harness SonarQube for superior software development practices!

Thank you for taking the time to read this guide. Your feedback is greatly appreciated. Please feel free to share your thoughts and suggestions.