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
Ubuntu 22.04 / 20.04 LTS
CPU: 2 vCPU
Volume: 20 GB
RAM: 4 GB
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
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.
First, update the packages on your instance:
sudo apt update
sudo apt upgrade -y
Step 2: Installing Java and PostgreSQL
To fully utilize SonarQube, ensure OpenJDK 17 is installed:
sudo apt install -y openjdk-17-jdk
Check the installed Java version:
java -version
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'
Next, add the PostgreSQL signing key:
wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
Install PostgreSQL:
sudo apt install postgresql postgresql-contrib -y
Enable the database server to start automatically on boot:
sudo systemctl enable postgresql
Start the database server:
sudo systemctl start postgresql
Check the status of the database server:
sudo systemctl status postgresql
Validate the installed version of PostgreSQL:
psql --version
Switch to the Postgres user:
sudo -i -u postgres
Step 3: Configuring the Database
Create a new database user for managing the sqube database:
createuser new_user
For instance, to create the "sona" user:
createuser sona
Access the PostgreSQL database:
psql
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';
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;
Grant all privileges on the "sqube" database to the "sona" user:
GRANT ALL PRIVILEGES ON DATABASE sqube to sona;
Verify the database creation:
l
Check the created user:
du
Exit the PostgreSQL command-line:
q
Return to your non-root sudo user account:
exit
Step 4: Installing SonarQube
Install the zip utility necessary for extracting SonarQube files:
sudo apt install zip -y
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
Unzip the downloaded file:
sudo unzip sonarqube-10.4.1.88267.zip
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
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
Grant the "sona" user access to the /opt/sonarqube directory:
sudo chown -R sona:sona /opt/sonarqube
Step 6: Configuring SonarQube for Optimal Performance
Edit the SonarQube configuration file located at /opt/sonarqube/conf/sonar.properties:
sudo nano /opt/sonarqube/conf/sonar.properties
Uncomment the following lines and add your database credentials:
sonar.jdbc.username=sona
sonar.jdbc.password=Sona#123
Specify the JDBC URL:
sonar.jdbc.url=jdbc:postgresql://localhost:5432/sqube
Save and exit the configuration file.
Edit the SonarQube script file:
sudo nano /opt/sonarqube/bin/linux-x86-64/sonar.sh
Specify the user to run SonarQube:
RUN_AS_USER=sona
Save and exit the script file.
Step 7: Creating a systemd Service for SonarQube
Create a systemd service file for SonarQube:
sudo nano /etc/systemd/system/sonar.service
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
Save and close the file.
Enable the SonarQube service:
sudo systemctl enable sonar
Start the SonarQube service:
sudo systemctl start sonar
Check the status of the SonarQube service:
sudo systemctl status sonar
Congratulations! Your SonarQube service is now up and running.
Step 8: Adjusting System Limits for Performance
Edit the sysctl configuration file to ensure proper functionality with Elasticsearch:
sudo nano /etc/sysctl.conf
Add the following lines to the configuration file:
vm.max_map_count=262144
fs.file-max=65536
ulimit -n 65536
ulimit -u 4096
Save and exit the configuration file.
Reboot the system to apply changes:
sudo reboot
Step 9: Accessing SonarQube
- To access SonarQube, open your web browser and enter your server's IP address followed by port 9000:
For instance, http://IP:9000.
- Use "admin" as both the username and password.
- To change your password:
- Log in with the credentials "admin" / "admin".
- You will be prompted to change your password. Enter "admin" for the current password and your new password twice.
- 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.