In this tutorial, you will install and configure SonarQube on an Alibaba Cloud ** Elastic Compute Service ** (ECS) instance using Ubuntu 16.04.
SonarQube can statically analyze your code to discover security vulnerabilities, detect bugs, and review your code. Performs automatic reviews to show the health of your application. SonarQube features a Quality Gate that can highlight recently introduced errors and fix leaks. You can analyze pull requests to perform analysis on feature branches before being pushed to SonarQube. This way, you have the opportunity to fix the issue before it reaches SonarQube.
Branch analysis is done to ensure that the code is clean and approved for merging into the master. SonarQube allows you to delve into the problem so that you can analyze and find it in your code. SonarQube visualizes the history of your project and explores all execution paths.
In short, SonarQube helps maintain your code by detecting issues, security vulnerabilities, and code odors and providing the results in a report format. In order to use SonarQube, it is necessary to install the analysis engine application on the developer's personal computer and install a server that centrally manages reports and records. This allows server applications to be accessed from multiple developers' computers, allowing centralized code quality reporting.
In this tutorial, SonarQube is installed on an Alibaba Cloud Elastic Compute Service (ECS) instance using Ubuntu 16.04. Set.
Please update your Ubuntu system with the following command before proceeding with the package installation. Remember to log in with sudo privileges as a non-root user to run this command.
# sudo apt update && sudo apt upgrade
You need to add a repository to install the Oracle JDK. However, in order to add that repository, you need to install add-apt-repository to add the repository to the apt package. To do this, run the following command.
# sudo apt-get install -y software-properties-common
Now you need to set up your Java JDK environment. SonarQube requires the Oracle JDK. To install the Oracle JDK, follow these steps:
Step 1: Run the following command to add the required repositories to the apt repository.
# sudo add-apt-repository ppa:webupd8team/java
Step 2: Run the following command to update the apt repository.
# sudo apt-get update
Step 3: Proceed with the JDK installation. At the time of writing, SonarQube only supports JDK 8, so don't forget to use this version only. Do not use any other version for now.
# sudo apt install oracle-java8-installer
You need to install unzip because you need to unzip the SonarQube compressed zip folder. To install, run the command.
# sudo apt-get install unzip
SonarQube supports PostgreSQL, MySQL, MSSQL and Oracle. For this tutorial, MySQL is not recommended for large instances, so we will use PostgreSQL. If you use MySQL, you have no choice but to use the bundled mysql-connector-java. The InnoDB engine is required for MySQL. To avoid such restrictions, we strongly recommend using PostgreSQL. Follow the steps below to install PostgreSQL.
Step 1: Install PostgreSQL repo
# sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
# wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
Step 2: Install the PostgreSQL server by running the following command
# sudo apt-get -y install postgresql postgresql-contrib
Step 3: Run the following command to start the PostgreSQL server so that it can start automatically after a reboot
# sudo systemctl start postgresql
# sudo systemctl enable postgresql
To install SonarQube for continuous code quality inspection, follow these steps:
Step 1: Create a directory named sonar. You can name the directory whatever you like, but the next step in installing SonarQube will require you to follow your own directory path.
# sudo mkdir /opt/sonar
Step 2: Now, give permissions to the username in the directory you created so that you can make changes. To do so, execute the following command. Don't forget to replace aareez with your username.
# sudo chown -R aareez:aareez /opt/sonar
Now that you have set up your server and installed your PostgreSQL database, let's install SonarQube. Follow these steps to download and install SonarQube.
Step 1: Navigate to the directory / opt / sonar that you created for SonarQube.
# cd /opt/sonar
Step 2: Now you need to download a copy of SonarQube from the official website. To do this, run the following command.
# sudo wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-7.2.1.zip
Step 3: You can now see the CLI compressed zip folder by running the command.
# ls -li
Unzip the compressed folder and unzip the file. To do this, run the following command.
# sudo unzip sonarqube-7.2.1.zip
Follow the steps below to create and configure the database.
Step 1: Change the password for the postgres user and switch to the postgres user. To change the password, run the following command.
# sudo passwd postgres
Run the following command to switch to the postgres user.
# su - postgres
Step 2: Run the following command to create a new user.
# createuser sqube
Step 3: Now open the Postgres shell to execute the query. To open it, run the command.
# psql
Step 4: Run the following query. The first query creates the user's password, the second query creates the database, assigns database privileges to the user, and the third query terminates the MySQL server.
ALTER USER sqube WITH ENCRYPTED password '654321Ab'.
CREATE DATABASE sqube OWNER sqube.
\q
Step 5: Run the following command to return to the sudo user.
# exit
To get started with SonarQube, you need to make some basic settings such as database settings and running in server mode.
Open the sonar.properties file, set the database username and password, and describe the driver SonarQube uses to connect to the database. Run the following command to open the file.
# cd /opt/sonar
# sudo nano sonarquebe-7.2.1/conf/sonar.properties
The above command will open the document. The following area is displayed. Remove # from the beginning of both lines and write the MySQL user name and password that you set when you created the database.
Find the part of sonar.jdbc.url below and remove the # to uncomment it. This will cause SonarQube to use PostgreSQL, and Sonar will need to be changed with the database name sqube.
Find sonar.web.host here, remove the # and uncomment this line and change the IP to 127.0.0.0.1.
Find sonar.web.javaAdditionalOpts here, remove the #, uncomment the line and change the value to -server.
After making changes, press Ctrl + X, type Y, and press Enter to save your settings.
SonarQube listens on localhost port 9000 by default. A reverse proxy must be configured to access over the standard HTTP 80 port.
Follow the procedure below to make the settings.
Step 1: Run the command to enable proxy mode.
# sudo a2enmod proxy
# sudo a2enmod proxy_http
Step 2: Proceed to virtual host configuration Run the following command to proceed with the virtual host creation.
# sudo nano /etc/apache2/sites-available/softpedia.xyz.conf
Step 3: Add the following text to the opened file, press Ctrl + X, type Y and press Enter to save the file.
ProxyRequests Off
ProxyPreserveHost On
<VirtualHost *:80>
ServerName www.softpedia.xyz
ServerAdmin [email protected]
ProxyPreserveHost On
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://www.softpedia.xyz/
</VirtualHost>
Step 4: You need to enable the newly created virtual host and then start and enable the apache server. To do this, run the command.
# sudo a2ensite softpedia.xyz.conf
# sudo systemctl start apache2
# sudo systemctl enable apache2
Step 5: Add a non-root user named sonar.
# sudo adduser sonar
Assign sonar user privileges to the directory / opt / sonar.
# sudo chown -R sonar:sonar /opt/sonar
Now open the bash file to assign run_as_user.
# sudo nano /opt/sonar/sonarquebe-7.2.1/bin/linux-x86-64/sonar.sh
Find RUN_AS_USER in the open file, remove the # sign, uncomment it, and add the value as sonar.
To start SonarQube, run the following command.
# opt/sonar/sonarqube-7.2.1/bin/linux-x86-64/sonar.sh start
To start SonarQube, run the following command.
# /opt/sonar/sonarqube-7.2.1/bin/linux-x86-64/sonar.sh stop
Sorry I made you wait. This completes SonarQube installation and configuration. Easy to access using your domain name. In my case I am using www.softpedia.xyz.
Recommended Posts