Difference between revisions of "Sonar"

Line 13: Line 13:
  
  
 +
Create an empty DB and MySQL user "sonarqube"
  
=Installation (Ubuntu)=
+
<syntaxhighlight lang="sql">
 
 
 
 
 
 
==Get package==
 
 
 
<syntaxhighlight lang="bash">
 
vim /etc/apt/sources.list
 
</syntaxhighlight>
 
 
 
 
 
Add a new repository
 
 
 
<syntaxhighlight lang="bash">
 
deb http://downloads.sourceforge.net/project/sonar-pkg/deb binary/
 
</syntaxhighlight>
 
 
 
 
 
Update packages list and install sonar
 
 
 
<syntaxhighlight lang="bash">
 
apt-get update
 
apt-get install sonar
 
</syntaxhighlight>
 
 
 
 
 
==Create SONAR database==
 
 
 
SONAR needs to work with a database.
 
 
 
You can create a new MySQL database + MySQL user for it.
 
 
 
 
 
Step 1: Login to MySQL as ROOT
 
 
 
<syntaxhighlight lang="bash">
 
 
mysql -u root -p
 
mysql -u root -p
Enter password:
 
</syntaxhighlight>
 
  
 
+
CREATE USER 'sonarqube'@'localhost' IDENTIFIED BY 'password';
Step 2: Create the Database
+
CREATE DATABASE IF NOT EXISTS sonarqube;
 
+
GRANT ALL PRIVILEGES ON sonarqube.* TO 'sonarqube'@'localhost';
<syntaxhighlight lang="bash">
+
FLUSH PRIVILEGES;
mysql > create database sonar;
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
Step 3: Verify that it’s there
 
  
<syntaxhighlight lang="bash">
+
=Installation=
mysql > show databases;
 
</syntaxhighlight>
 
  
 +
I advise you to use the manual set-up and update. Experience proved that it can be cumbersome to upgrade SonarQube.
  
Step 4: Create the User
 
  
<syntaxhighlight lang="bash">
+
==Get SonarQube==
mysql > create user sonar;
 
</syntaxhighlight>
 
  
 
+
Download the latest version (or the LTS) on http://www.sonarqube.org/downloads/
Step 5: Grant privileges while assigning the password
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
mysql > grant all on sonar.* to 'sonar'@'localhost' identified by 'sonar';
+
cd /opt
 +
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.1.2.zip
 +
unzip sonarqube-5.1.2.zip
 +
ln -s /opt/sonarqube-5.1.2 /opt/sonarqube
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
Step 6: Apply changes
+
(i) It's always good to use a symlink. This make the update and rollback a bit easier.
 
 
<syntaxhighlight lang="bash">
 
mysql > flush privileges;
 
</syntaxhighlight>
 
  
  
Step 7: exit
 
  
<syntaxhighlight lang="bash">
+
==Configuration==
mysql > quit;
 
</syntaxhighlight>
 
  
 
+
Edit the SonarQube configuration file
 
 
==Configure SONAR==
 
 
 
Edit SONAR configuration file
 
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
vim /opt/sonar/conf/sonar.properties
+
vim /opt/sonarqube/conf/sonar.properties
 
</syntaxhighlight>
 
</syntaxhighlight>
  
  
===Database configuration===
+
===Database===
  
 
Disable embedded H2DB and enable MySQL database, lines 20 to 40:
 
Disable embedded H2DB and enable MySQL database, lines 20 to 40:
  
 
<syntaxhighlight lang="apache">
 
<syntaxhighlight lang="apache">
sonar.jdbc.username=sonar            
+
sonar.jdbc.username=sonarqube            
sonar.jdbc.password=sonar
+
sonar.jdbc.password=sonarqube
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonar?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
+
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 125: Line 74:
 
#sonar.web.host:            0.0.0.0
 
#sonar.web.host:            0.0.0.0
 
#sonar.web.port:            9000
 
#sonar.web.port:            9000
sonar.web.context:          /sonar
+
sonar.web.context:          /sonarqube
 +
</syntaxhighlight>
 +
 
 +
!!! This is VERY important that you uncomment and set the '''sonar.web.context''' !!! Without it you cannot use Apache2 proxy.
 +
 
 +
 
 +
===Sonar symlink===
 +
 
 +
The default path to manage SonarQube is, in that example: <code>/opt/sonarqube/bin/linux-x86-64/sonar.sh</code>
 +
idem for the logs...
 +
 
 +
<syntaxhighlight lang="bash">
 +
ln -s /opt/sonarqube/bin/linux-x86-64/sonar.sh /usr/bin/sonarqube
 +
ln -s /opt/sonarqube/bin/linux-x86-64/sonar.sh /etc/init.d/sonarqube
 +
 
 +
mkdir -p /var/log/sonar
 +
ln -s /opt/sonarqube/logs/sonar.log /var/log/sonar/sonar.log
 +
ln -s /opt/sonarqube/logs/access.log /var/log/sonar/access.log
 
</syntaxhighlight>
 
</syntaxhighlight>
  
!!! This is VERY important that you uncomment and set the '''sonar.web.context''' !!!
 
  
  
 
===Apply changes===
 
===Apply changes===
  
You must restart Sonar to use the new settings.
+
You must start Sonar to use the new settings.
  
 
<syntaxhighlight lang="bash">
 
<syntaxhighlight lang="bash">
service sonar restart
+
sonarqube restart
 
</syntaxhighlight>
 
</syntaxhighlight>
  
... wait for some times on 1st start (5 to 7 mn) !!
+
... wait for some times on 1st start (5 to 7 mn) !! Logs are in
 +
 
  
  
Line 152: Line 118:
 
tcp        0      0 0.0.0.0:9000            0.0.0.0:*              LISTEN      xxxxx/java
 
tcp        0      0 0.0.0.0:9000            0.0.0.0:*              LISTEN      xxxxx/java
 
</syntaxhighlight>
 
</syntaxhighlight>
 +
  
  

Revision as of 22:52, 10 September 2015


The following instructions are for Ubuntu 14.04 LTS.


You can find all these instructions and more on the Official how-to: http://sonar-pkg.sourceforge.net/


Requirements

You need to have a MySQL server available.


Create an empty DB and MySQL user "sonarqube"

mysql -u root -p

CREATE USER 'sonarqube'@'localhost' IDENTIFIED BY 'password';
CREATE DATABASE IF NOT EXISTS sonarqube;
GRANT ALL PRIVILEGES ON sonarqube.* TO 'sonarqube'@'localhost';
FLUSH PRIVILEGES;


Installation

I advise you to use the manual set-up and update. Experience proved that it can be cumbersome to upgrade SonarQube.


Get SonarQube

Download the latest version (or the LTS) on http://www.sonarqube.org/downloads/

cd /opt
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-5.1.2.zip
unzip sonarqube-5.1.2.zip
ln -s /opt/sonarqube-5.1.2 /opt/sonarqube


(i) It's always good to use a symlink. This make the update and rollback a bit easier.


Configuration

Edit the SonarQube configuration file

vim /opt/sonarqube/conf/sonar.properties


Database

Disable embedded H2DB and enable MySQL database, lines 20 to 40:

sonar.jdbc.username=sonarqube			            
sonar.jdbc.password=sonarqube
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true


Port number and root context

Adjust port number and context

#sonar.web.host:             0.0.0.0
#sonar.web.port:             9000
sonar.web.context:           /sonarqube

!!! This is VERY important that you uncomment and set the sonar.web.context !!! Without it you cannot use Apache2 proxy.


Sonar symlink

The default path to manage SonarQube is, in that example: /opt/sonarqube/bin/linux-x86-64/sonar.sh idem for the logs...

ln -s /opt/sonarqube/bin/linux-x86-64/sonar.sh /usr/bin/sonarqube
ln -s /opt/sonarqube/bin/linux-x86-64/sonar.sh /etc/init.d/sonarqube

mkdir -p /var/log/sonar
ln -s /opt/sonarqube/logs/sonar.log /var/log/sonar/sonar.log
ln -s /opt/sonarqube/logs/access.log /var/log/sonar/access.log


Apply changes

You must start Sonar to use the new settings.

sonarqube restart

... wait for some times on 1st start (5 to 7 mn) !! Logs are in


Check that Sonar is up:

netstat -pl --numeric | grep 9000

You should have:

tcp        0      0 0.0.0.0:9000            0.0.0.0:*               LISTEN      xxxxx/java



Apache2 proxy

Instead of opening port 9000, it's better to access Sonar through Apache2 proxy.


To use the proxy rule, the target /sonar must match the root URL (see sonar.properties)


Apache2 configuration

Edit configuration file: module or virtual host

vim /etc/apache2/mods-enabled/proxy.conf


Set the following:

# Proxy to a Java application running over Tomcat, with IP filter
<Location /sonar>
	ProxyPass http://localhost:9000/sonar
	ProxyPassReverse http://localhost:9000/sonar

        ### Apache < 2.4
	#Order allow,deny
	#Allow from 127.0.0.1 192.168.1.0/24 193.12.118.196
 
        ### Apache 2.4
        Require local
        Require ip 192.168.1
        Require host 193.12.118.196
</Location>


Test Sonar

The default user and password are “admin” and “admin“.


Logs

Sonar logs are in:


/opt/sonar/logs/sonar.log


Upgrade Sonar

Sometimes when there are a lot of changes the new sonar version required some database change.


The service will not be available until you go to http://myServer/sonar/setup


You have to agree to the terms and upgrade database