How to setup a SonarQube server in Azure

SonarQube allows you to track and manage your technical debt. At the center of a SonarQube installation is the server which we will build in this post.  The data for SonarQube is stored in a database. SonarQube supports many different database systems but for this post we are going to use Microsoft SQL Server running in a VM in Azure.

To begin we are going to create a Microsoft SQL Server VM in Azure. 

Next we need to open up a few endpoints on the VM so we can have external access to SQL and the SonarQube server.

Now we need to connect to the Machine and configure SQL and install SonarQube.  Once you are connected to the VM do yourself a favor and disable the Internet Explorer Enhanced Security Configuration.

Start Microsoft SQL Server Management Studio so we can configure SQL Server.  First we need to enable SQL Server authentication.  Right click the server node in the tree to the left and select Properties.  On the Server Properties dialog select Security from the Select a page section.  Click the radio button for SQL Server and Windows Authentication mode and click OK.

We now need to add a user for connecting as the SonarQube server.  Expand the Security node in the left tree and right click on Logins and select New Login.  On the Login dialog enter SonarQube as the Login name and select the SQL Server authentication radio button.  Enter a password and uncheck the Enforce password policy checkbox.  From the Select a page section click on Server Roles and check sysadmin and click OK.

Now we are going to create the target database for SonarQube.  Right click on Databases and select New Database.  Give the database a name and select Options so we can change the Collation.  For the database to work with SonarQube the Collation must be Latin1_General_100_CS_AS.  While we are here we can change the Recovery model to Simple to improve performance.

For all of the security changes to take effect we need to restart the SQL Server.  To do this right click on the server node in the tree to the left and select Restart.  You can now close Microsoft SQL Server Management Studio.

SonarQube relies on Java so we have to install Java on our machine.  You can download the latest version from http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html.  Simply run the installer and accept all the default values.  Be sure and make a note of the Destination Folder so we can set the JAVA_HOME and update the path environment variables.  Once the installation is complete open Windows Explorer and right click the PC node and select Properties.  This will open the System dialog.  Click the Advanced system settings link.  Now click the Environment Variables button.  In the System variables section click New.  The name of the new system variable will be JAVA_HOME with a value of C:\Program Files\Java\jdk{YourVersion}.  Replace {YourVersion} with the version number of the JDK you have installed.  In the same System variables section double click the Path environment variable.  To the end of the value add ";%JAVA_HOME%" without the quotes. Click OK until all dialogs are closed.

Now let's move on to installing SonarQube.  First we need to download the files from http://www.sonarqube.org/downloads/.  Once you have the files downloaded be sure and unblock the file.

With the file unblocked extract all the files to where you want to install SonarQube.  Now we are going to configure SonarQube to use our SQL Server.  In the conf folder of the SonarQube installation open the sonar.properties file in a text editor.  Find the section for Microsoft SQLServer 2008/2012 and uncomment out the sonar.jdbc.url line.  Update "/sonar" to match the name of the database you created.  We also need to add two lines for the username and password to connect to SQL Server.

sonar.jdbc.username={yourUserName}
sonar.jdbc.password={yourPassword}
sonar.jdbc.url=jdbc:jtds:sqlserver://localhost/{yourDatabaseName};SelectMethod=Cursor

Now we need to open a command prompt as administrator so we can install SonarQube as a service.  With the command prompt open CD to the bin\windows-x86-64 folder of the SonarQube installation folder.  Now run the InstallNTService.bat file.  Because SonarQube depends on SQL Server we are going to setup a dependency on SQL Server for our SonarQube Service.  In the command prompt execute the following command

sc config SonarQube depend= MSSQLSERVER

We also need to update the login account used for the service.  To do that open the services application.

Now open the properties page for the SonarQube service. Click the Log On tab and select the This account radio button. Enter the account used to log into your VM and click OK. Now start the service by clicking the Start the service link.

The final step is to open ports on the Windows firewall so we can access the SonarQube server and SQL from outside the machine. Return to the command prompt and simply run the following commands to add the firewall rules.

netsh advfirewall firewall add rule name="SQL" dir=in action=allow protocol=TCP localport=1433
netsh advfirewall firewall add rule name="Sonar" dir=in action=allow protocol=TCP localport=9000

Using any browser you should now be able to access your SonarQube server using the fully qualified name of your Azure VM.

In future posts I will show you how to use Build vNext to use SonarQube with a Java project with Maven. 

 

Comments (8) -

  • Fokko

    7/31/2015 1:10:29 PM | Reply

    Thanks for your elaboration! I think there is one small mistake; the PATH environment variable should contain %JAVA_HOME%\bin instead of %JAVA_HOME%. If misconfigured, the service will not start and it will throw an error.

  • Kevin Moormann

    1/19/2016 4:59:24 AM | Reply

    With Windows Server 2012 netsh has been deprecated in favor of the NetSecurity powershell module.

    The powershell analgous commands for adding rules to the firewall are

    New-NetFirewall-Rule -DisplayName "SQL" -Direction Inbound -Protocol TCP -LocalPort 1433 -Action Allow

    New-NetFirewall-Rule -DisplayName "Sonar" -Direction Inbound -Protocol TCP -LocalPort 9000 -Action Allow

  • Peter Jenei

    7/20/2018 11:39:42 AM | Reply

    Hi Donovan,

    Thanks for your post!
    I tried to follow it with a minor modification: I used Azure SQL Database as the data store.
    The changes I had to make while following your post:
    - create the Azure SQL Database from the portal,
    - open SQL port from my VM to my Azure SQL Database,
    - change the connection string in sonar.properties to something like this:
    sonar.jdbc.url=jdbc:sqlserver://{myservername}.database.windows.net;database={mydatabasename};encrypt=true;trustServerCertificate=true;hostNameInCertificate=*.database.windows.net;loginTimeout=30;SelectMethod=Cursor

    Regards,
    Peter

    • Donovan

      8/7/2018 8:36:39 PM | Reply

      Thanks for sharing!

  • Lohith

    12/27/2018 5:14:46 AM | Reply

    Hi ,

    I have installed sonarqube server(7.4)  on azure centos 64 bit machine with POSTGRESQL database connection. Sonar Service is running with Elasticsearch and also in web logs i am able to see the web server is operational. With tcp port 9002 i could see the sonar service is up.

    But the problem is , when i tried to connect with my DNS or public ip address like http://public_ip:9002 or http://dns:9002 the sonar instance through web browser is not coming.

    How can i access my sonar instance through public ip or dns on azure centos machine with POSTGRESQL database.

    • Peter Jenei

      1/3/2019 9:45:35 AM | Reply

      Hi,

      I think you missed some of the firewall settings.
      You have to do it twice:
      - first you have to create inbound port rules on Azure portal (select your VM, then under Settings/Networking you can create a rule to open port 9002),
      - then you have to open port 9002 in CentOS.

      Regards,
      Peter

Pingbacks and trackbacks (1)+

Add comment

Loading