Building an Automated SAST Code Analysis and Scoring System with SonarQube on VPS
This guide provides a detailed walkthrough for deploying an automated Static Application Security Testing (SAST) system using SonarQube on a VPS, covering installation,...
This guide provides a detailed walkthrough for deploying an automated Static Application Security Testing (SAST) system using SonarQube on a VPS, covering installation, configuration, CI/CD integration, and code quality monitoring. It is ideal for organizations aiming to establish automated code review processes, detect security vulnerabilities early, and improve code quality within a DevOps environment on a limited budget.
Overview of SonarQube and SAST
SonarQube is a powerful open-source platform for analyzing and evaluating code quality, supporting over 30 programming languages including Java, Python, JavaScript, and C#. It enables the detection of security vulnerabilities, code standard violations, and quality metrics, making it a cornerstone of DevOps workflows. SAST (Static Application Security Testing) is a method of analyzing source code without executing the application, allowing early identification of security flaws during development.
Deploying SonarQube on a VPS gives organizations full control over the system, allowing customization to meet specific needs while reducing costs compared to cloud-based solutions. This guide uses NVMe Cloud VPS from NVMe Cloud VPS for Docker, Nginx & Databases, offering high performance and flexible scalability.
Requirements and Preparation
To successfully deploy SonarQube, you will need:
- A VPS with at least 4GB RAM, 2 CPU cores, and 50GB SSD (NVMe is recommended for better performance).
- Linux operating system (Ubuntu 20.04/22.04 or CentOS 8/9).
- SSH access with root or sudo privileges.
- Docker and Docker Compose installed (used to deploy SonarQube and PostgreSQL).
Ensure your VPS is updated:
sudo apt update && sudo apt upgrade -yInstall Docker and Docker Compose if not already present:
sudo apt install docker.io docker-compose -yDeploying SonarQube and PostgreSQL
SonarQube requires a database to store analysis data. We will use PostgreSQL via Docker Compose. Create a file named `docker-compose.yml`:
version: '3.8'
services:
sonarqube:
image: sonarqube:latest
container_name: sonarqube
ports:
- "9000:9000"
volumes:
- sonarqube_data:/opt/sonarqube/data
- sonarqube_logs:/opt/sonarqube/logs
- sonarqube_extensions:/opt/sonarqube/extensions
environment:
- SONARQUBE_JDBC_URL=jdbc:postgresql://postgres:5432/sonar
- SONARQUBE_JDBC_USERNAME=sonar
- SONARQUBE_JDBC_PASSWORD=sonar
depends_on:
- postgres
postgres:
image: postgres:13
container_name: sonarqube_postgres
environment:
- POSTGRES_DB=sonar
- POSTGRES_USER=sonar
- POSTGRES_PASSWORD=sonar
volumes:
- postgres_data:/var/lib/postgresql/data
ports:
- "5432:5432"
volumes:
sonarqube_data:
sonarqube_logs:
sonarqube_extensions:
postgres_data:Start the services:
docker-compose up -dOnce running, access http://your-vps-ip:9000 to log in (default credentials: admin/admin). Immediately change the password.
Integrating CI/CD for Automated Code Analysis
To enable automated code analysis, integrate SonarQube with a CI/CD platform like GitHub Actions, GitLab CI, or Jenkins. Below is an example using GitHub Actions:
Create a file `.github/workflows/sonarqube.yml`:
name: SonarQube Analysis
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
sonarqube:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: SonarQube Scan
uses: sonarqube/sonarqube-scan-action@v1
with:
projectKey: my-project
organization: my-organization
token: ${{ secrets.SONAR_TOKEN }}In SonarQube, create a new project and obtain a token from http://your-vps-ip:9000 (User Settings → Security → Tokens).
Monitoring and Maintenance
To ensure stable operation, perform the following tasks:
- Regularly check SonarQube logs:
docker logs sonarqube - Back up the PostgreSQL database periodically.
- Update SonarQube and plugins regularly to receive security patches.
- Monitor VPS resources (RAM, CPU, disk) to prevent overload.
For more in-depth DevOps guidance, refer to Kubernetes on VPS: Professional K8s Cluster Setup Under $50/Month and Green DevOps Strategy: Configuring Keda to Automatically Shut Down Node.js Bots on VPS During Low Load.
Recommended NVMe Cloud VPS Plans to Deploy This Setup
Enterprise RAID 10 NVMe SSDs, CMC & FPT District 9 uplinks with Anycast Anti-DDoS and 99.99% SLA.
VPS-3E
10% VAT included
- CPU: 2 vCPU
- RAM: 4 GB
- SSD: 50 GB NVMe
