SonarQube, formerly known as Sonar, is a powerful open-source platform that enables static code analysis and code security. It is designed to help developers detect bugs, enhance application security, and improve code quality. SonarQube provides comprehensive reports on various aspects of code, such as duplicate code, coding standards, code complexity, and security recommendations. This guide will walk you through the process of installing SonarQube on an Ubuntu 22.04 server, including the installation of PostgreSQL as the database and Nginx as the reverse proxy.
Prerequisites
Before we start the installation process, make sure you have the following prerequisites:
- An Ubuntu 22.04 server with UFW firewall enabled.
- A non-root user with sudo/administrator privileges.
- A domain name pointed to the IP address of your Ubuntu server.
Installing Java OpenJDK
The first step is to install Java OpenJDK on your Ubuntu system, as SonarQube requires Java OpenJDK v11. Update and refresh your Ubuntu package index repository by running the following command:
sudo apt update
Now, install the Java OpenJDK v11 using the following command:
sudo apt installdefault-jdk
Verify the Java version installed on your system by running the following command:
java-version
Installing PostgreSQL Database System
SonarQube supports multiple database systems, including PostgreSQL, Microsoft SQL Server, and Oracle Database. In this guide, we will use PostgreSQL as the database for SonarQube.
To install PostgreSQL, we need to add the PostgreSQL repository and install the appropriate packages. Run the following commands to add the repository and update your Ubuntu package index:
wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O- | sudo apt-key add- sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list' sudo apt update
Once the repository is added and updated, install PostgreSQL v13 by running the following command:
sudo apt install postgresql-13
After the installation is complete, verify the status of the PostgreSQL service by running the following command:
sudo systemctl is-enabled postgresql
sudo systemctl status postgresql
Setting up the System
Before installing SonarQube, we need to set up the system by creating a dedicated user for running SonarQube and configuring some additional parameters.
Create a new user named ‘sonarqube’ using the following command:
sudo useradd -b /opt/sonarqube -s /bin/bash sonarqube
Now, open the ‘/etc/sysctl.conf’ file using a text editor:
sudo nano /etc/sysctl.conf
Add the following lines at the bottom of the file to set up custom kernel parameters:
vm.max_map_count=524288 fs.file-max=131072
Save the file and exit the editor. Apply the changes by running the following command:
sudo sysctl --system
Next, set up the ulimit for SonarQube by running the following commands:
ulimit -n 131072 ulimit -u 8192
To make the ulimit configuration permanent, create a new configuration file using the following command:
sudo nano /etc/security/limits.d/99-sonarqube.conf
Add the following lines to the file:
sonarqube- nofile 131072 sonarqube- nproc 8192
Save the file and exit the editor.
Downloading SonarQube Package
Now it’s time to download the SonarQube package and set up the installation. We will download the package from the official SonarQube website.
Install the necessary packages by running the following command:
sudo apt install unzip software-properties-common wget
Download the SonarQube package by running the following command:
wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.6.1.59531.zip
Extract the downloaded package using the following command:
unzip sonarqube-9.6.1.59531.zip
Move the extracted directory to the ‘/opt/sonarqube’ directory:
mv sonarqube-9.6.1.59531 /opt/sonarqube
Change the ownership of the SonarQube installation directory to the ‘sonarqube’ user:
sudo chown -R sonarqube:sonarqube /opt/sonarqube
Configuring SonarQube
After downloading and setting up the SonarQube package, it’s time to configure SonarQube by editing the ‘sonar.properties’ file.
Open the ‘sonar.properties’ file using a text editor:
sudo nano /opt/sonarqube/conf/sonar.properties
Configure the database settings by uncommenting and modifying the following lines:
sonar.jdbc.username=sonarqube sonar.jdbc.password=Password sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube
Configure the max heap memory size for the Elasticsearch process by uncommenting and modifying the following line:
sonar.search.javaOpts=-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m- XX:+HeapDumpOnOutOfMemoryError
Configure the web host, port, and other settings by uncommenting and modifying the following lines:
sonar.web.host=127.0.0.1 sonar.web.port=9000 sonar.web.javaAdditionalOpts=-server sonar.log.level=INFO sonar.path.logs=logs
Save the file and exit the editor.
Setting up the Systemd Service
To start and manage SonarQube as a service, we need to create a Systemd service file.
Create a new service file using a text editor:
sudo nano /etc/systemd/system/sonarqube.service
Add the following content to the 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=sonarqube
Group=sonarqube
Restart=always
LimitNOFILE=65536
LimitNPROC=4096
[Install]
WantedBy=multi-user.target
Save the file and exit the editor.
Reload the systemd manager to apply the changes:
sudo systemctl daemon-reload
Start the SonarQube service and enable it to start at system boot:
sudo systemctl start sonarqube.service sudo systemctl enable sonarqube.service
Verify the status of the SonarQube service:
sudo systemctl status sonarqube.service
Running SonarQube with Reverse Proxy
To access SonarQube from a web browser, we need to set up a reverse proxy using Nginx.
Install Nginx by running the following command:
sudo apt install nginx
Verify the status of the Nginx service:
sudo systemctl is-enabled nginx
sudo systemctl status nginx
Create a new server block configuration file:
sudo nano /etc/nginx/sites-available/sonarqube.conf
Add the following configuration to the file, replacing ‘sonar.hwdomain.io’ with your domain name:
server { listen 80; server_name sonar.hwdomain.io; access_log /var/log/nginx/sonar.access.log; error_log /var/log/nginx/sonar.error.log; proxy_buffers 16 64k; proxy_buffer_size 128k; location / { proxy_pass http://127.0.0.1:9000; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto http; } }
Save the file and exit the editor.
Create a symlink to enable the server block configuration:
sudo ln -s /etc/nginx/sites-available/sonarqube.conf /etc/nginx/sites-enabled/
Verify the Nginx configuration:
sudo nginx -t
Restart Nginx to apply the changes:
sudo systemctl restart nginx
SonarQube Installation
Now that everything is set up, you can access SonarQube through your domain name. Open your web browser and enter the domain name you configured earlier (e.g., http://sonar.example.io).
You will be greeted with the SonarQube login page. Use the default username ‘admin’ and password ‘admin’ to log in.
Upon logging in, you will be prompted to change the password. Enter the old password ‘admin’ and set a new strong password.
Congratulations! You have successfully installed SonarQube on your Ubuntu 22.04 server. You can now start analyzing your projects using SonarQube’s powerful static code analysis capabilities.
Conclusion
In this guide, we have covered the step-by-step process of installing SonarQube on an Ubuntu 22.04 server. We started by installing the necessary dependencies, including Java OpenJDK and PostgreSQL. Then, we set up the system by creating a dedicated user, configuring kernel parameters, and setting up ulimit. After that, we downloaded and configured SonarQube, as well as created the systemd service for managing SonarQube as a service. Finally, we set up a reverse proxy using Nginx to access SonarQube securely.
For reliable and scalable cloud hosting solutions, consider Shape.host’s Cloud VPS services. Shape.host offers secure and efficient hosting options tailored to meet your business’s needs. Visit Shape.host for more information.