SonarQube – PHP Project – Ubuntu

SonarQube-PHP-Ubuntu-3First of all we need to install SonarQube Server onto the local machine. Please note that the Java JRE bit should be same as of the OS bit. SonarQube Runner is required to run the analysis onto the project.

#Check Java JRE bit by using the below command:

$ java -version

Output: if it’s 64-bits it will say so otherwise it’s 32-bit.

For 64-bit:

java version “1.6.0_20”
Java(TM) SE Runtime Environment (build 1.6.0_20-b02-279-10M3065)
Java HotSpot(TM) 64-Bit Server VM (build 16.3-b01-279, mixed mode)

For 32-bit:

java version “1.6.0_20”
Java(TM) SE Runtime Environment (build 1.6.0_20-b02-279-10M3065)
Java HotSpot(TM) Client VM (build 16.3-b01-279, mixed mode, sharing)


#Check OS bit:

$ uname -i

For 32-bit:


For 64-bit:


#Prerequisite Installations for SonarQube :

  1. Install JAVA JRE
  • sudo apt-get update
  • java -version — (check if java is not already installed)
  • sudo apt-get install default-jre
    • This will install JRE-6 on Ubuntu 12.04 and earlier, and on 12.10+ it will install JRE-7.
  1. Install MYSQL
    • sudo apt-get update
    • sudo apt-get install mysql-server
    • During the installation process, you will be prompted to set a password for the MySQL root user. Choose a strong password and keep it safe for future reference.
    • MySQL will bind to localhost( by default.
    • Once you have installed MySQL, activate it with this command :
      1. $ sudo mysql_install_db
    • Finish up by running the MySQL set up script:
      1. $ sudo /usr/bin/mysql_secure_installation
      2. You will be given the choice to change the MySQL root password, remove anonymous user accounts, disable root logins outside of localhost, and remove test databases. It is highly recommended that you answer to yes to these options.
  • To Login to mysql as the root user:
    1. $ mysql -u root -p


#Install SonarQube

Step 1: Create SonarQube database and user

$ mysql -u root -p
// creating database
mysql> CREATE DATABASE sonar CHARACTER SET utf8 COLLATE utf8_general_ci;
// creating sonar user and granting permissions to sonar database
 mysql> CREATE USER 'sonar' IDENTIFIED BY 'sonar';
 mysql> GRANT ALL ON sonar.* TO 'sonar'@'%' IDENTIFIED BY 'sonar';
 mysql> GRANT ALL ON sonar.* TO 'sonar'@'localhost' IDENTIFIED BY 'sonar';

Step 2: Download & Unzip SonarQube Distribution

$ export SONAR_VERSION = 5.1
$ wget${SONAR_VERSION}.zip
$ unzip sonarqube-${SONAR_VERSION}.zip
// move the unzipped folder to /opt/
 $ mv sonarqube-${SONAR_VERSION} /opt/sonar

Step 3: Edit and Configure

// use any of the text editor to edit /opt/sonar/conf/
$ sudo gedit /opt/sonar/conf/
// User Credentials
// Embedded Database
// Web Server Settings

Step 4: Run SonarQube Server

Please note that server can be started even by implementing SonarQube Server as service but for now we will start it by using the given script located inside the /opt/sonar/bin/linux-x86-32/ start

Important thing to note is that as per your os bit version, you shall execute the script.

$ sudo /opt/sonar/bin/linux-x86-32/ start
Usage: { console | start | stop | restart | status | dump }

Visit the sonar server web page at: http://localhost:9000/sonar And Login with credentials(username/password) : admin/admin


Install SonarQube Runner

#Prerequisite installations

  1. SonarQube Server
    1. To Install it please follow the instructions present in previous page
  2. MySQL
    1. To Install MySQL please follow the instructions present in previous page

Step 1: Download & Unzip SonarQube Runner Distribution

 $ unzip sonar-runner-dist-${SONAR_RUNNER_VERSION}.zip
// moving the unzipped folder to /opt/
 $ mv sonar-runner-${SONAR_RUNNER_VERSION} /opt/sonar-runner

Step 2: Edit and Configure

$ sudo  gedit /opt/sonar-runner/conf/
// change the followings

Please note that the should be same as of the file present in /opt/sonar/conf/

Step 3: Create and Set Environmental Variables

// Create ~/.pam_environment file if it does not exist:
$ touch ~/.pam_environment
// Open ~/.pam_environment in a text editor, and add the following lines to it:

Step 4: Restart the SonarQube Server

$ sudo /opt/sonar/bin/linux-x86-32/ restart

Step 5: Install PHP Plugin for SonarQube – Manual Installation

Download the php plugin from here.

Upload the downloaded jar file in your SonarQube Server and put it in the directory : $SONARQUBE_HOME/extensions/plugins.

If another version of the same plugin is already there, you need to remove/backup it as only one version of a given plugin must be available in the extensions/plugins directory.

Once done, you will need to restart your SonarQube Server.


#Running Analysis onto the project

  1. Go to the project’s root directory
  2. Run the following command:
$ sonar-runner -Dsonar.issuesReport.html.enable=true - -Dsonar.projectKey=AdminConsole 
-Dsonar.projectName=AdminConsole -Dsonar.projectVersion=1 -
Dsonar.sources=./app -Dsonar.language=php -
Dsonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance -
Dsonar.jdbc.username=sonar -Dsonar.jdbc.password=sonar

Please note that in the above command i have mentioned the ./app folder for sources as it is related to the laravel project.

If Execution successful is printed onto the console, click on the link provided therein to view the analysis report.

Please note that I have implemented the above steps for setting up SonarQube version 5.1 and SonarQube Runner Version 2.1.


Securing SonarQube Installation

# Change Password for Admin User

  1. localhost:9000/sonar
  2. Click on Log In
  3. Login with credentials admin/admin.
  4. After logging in Click on My Profile present as sub-menu under logged user name in top-right corner.
  5. Change Password accordingly.


# Force User Authentication to stop unlogged user to access SonarQube

  1. Login to the Server using credentials (admin/admin — if password is not changed)
  2. Click on Settings Menu Item present onto the top.
  3. Click on the Security Tab present on the left side.
  4. Set the value to true for Force User Authentication
  5. Click on Save Security Settings.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s