How Do You Install SonarQube on a Red Hat Distribution of Linux?

Problem scenario
You have a Red Hat distribution of Linux (e.g., CentOS/RHEL/Fedora).  How do you install SonarQube?

Solution

Prerequisites
Install the Java Development Kit so you can later take advantage of plugins.  If you need assistance with this, see this link.

Procedures
For future reference, at some point the "9.6"s you see below will have to be incremented to a newer version of PostgreSQL. 

1.  If you are running RHEL, run this command:  sudo yum -y update

If you are using CentOS, run these two commands:
sudo yum -y install epel-release
sudo yum -y update

2.  If you are running RHEL, run this command:
sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-redhat96-9.6-3.noarch.rpm

If you are running CentOS, run this command:
sudo rpm -Uvh https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm

3.  Run these commands:
sudo yum -y install postgresql96-server postgresql96-contrib
sudo /usr/pgsql-9.6/bin/postgresql96-setup initdb

4.a.  Enable MD5-based authentication by editing the /var/lib/pgsql/9.6/data/pg_hba.conf file.

4.b.  Run this command:  sudo vi /var/lib/pgsql/9.6/data/pg_hba.conf

4.c.  Find the following stanzas; change the word "peer" to "trust" and change the word "ident" to "md5".  Here is how it may appear before you make these changes:

# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     peer
# IPv4 local connections:
host    all             all             127.0.0.1/32            ident
# IPv6 local connections:
host    all             all             ::1/128                 ident

Here is what it will look like after the changes:
# TYPE  DATABASE        USER            ADDRESS                 METHOD

# "local" is for Unix domain socket connections only
local   all             all                                     trust
# IPv4 local connections:
host    all             all             127.0.0.1/32            md5
# IPv6 local connections:
host    all             all             ::1/128                 md5

5.  Run these two commands:

sudo systemctl start postgresql-9.6
sudo systemctl enable postgresql-9.6

6.  Change the postgres user's password:  sudo passwd postgres

7.  Assume the postgres user with this command:  su - postgres

8.  Create a user named "sonar" with this command:  createuser sonar

9.   Enter the PostgreSQL command prompt by running this command:  psql

10.  Prepare a command such as this, but replace "goodPassword" with the password you want for the sonar PostgreSQL database user:

ALTER USER sonar WITH ENCRYPTED password 'goodPassword';

11.  Run the prepared command above (after you replaced goodPassword with the password of your choice).

12.  Run this command from the SQL command prompt:  CREATE DATABASE sonar OWNER sonar;

13.  Leave the psql prompt by running this command:  \q

14.  Exit the postgres user to be a user that is a sudoer.  Run this:  exit

15.  Run a script with these five lines (using sudo privileges when you run it):

version=7.0
curl -L https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-$version.zip > /tmp/sonarqube-$version.zip
yum -y install unzip
unzip /tmp/sonarqube-$version.zip -d /opt
mv /opt/sonarqube-$version /opt/sonarqube

# Call the file above "sonar.sh" and put it in /tmp.  Run it with "sudo bash /tmp/sonar.sh"

16.  Modify the SonarQube configuration file.  To enter it, run this:  sudo vi /opt/sonarqube/conf/sonar.properties

Find the following two stanzas:

#sonar.jdbc.username=
#sonar.jdbc.password=

"Uncomment and provide the PostgreSQL username and password of the database that we have created earlier. It should look like:" (Taken from Vultr.com.)

sonar.jdbc.username=sonar
sonar.jdbc.password=goodPassword

17.  While in the same file, find this stanza (it may be on the 39th line):

#sonar.jdbc.url=jdbc:postgresql://localhost/sonar

Uncomment the line, and save the file.  (Also taken from Vultr.com.)

18.a  Create this file: /etc/systemd/system/sonar.service

18.b.  The contents should be the following (with the first line being "[Unit]" and last line being "WantedBy = multi-user.target"):

[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=sonar
Group=sonargroup
Restart=always

[Install]
WantedBy = multi-user.target

19.a. Run these commands:

sudo useradd sonar
sudo groupadd sonargroup
sudo passwd sonar
  # respond with a new password
sudo usermod -a -G sonargroup sonar

 Run a command like this but replace sonar with the user that will start the sonar service and replace sonargroup with the user's group:

sudo chown -R sonar:sonargroup /opt/sonarqube

# If you do not know how to find the user's group, log in as the user and run the command "groups" to list the user's membership(s) of any group(s).

19.b.  Run these three commands:
sudo systemctl start sonar
sudo systemctl enable sonar
sudo systemctl status sonar

20.  Configure the firewall to allow connections.  If you are in a special environment, you can disable your OS firewall with this command:

sudo systemctl stop firewalld   # Normally you would configure a special exception in the firewall.
sudo setenforce 0

21.  Run this command to install Apache web server: sudo yum -y install httpd

22.a  Run this command, but replace x.x.x.x with the external IP address of your server (or the URL that you will present with this server):  

sudo vi /etc/httpd/conf.d/sonar.x.x.x.x.conf

22.b.  Place these lines in the file (where the top and bottom lines have "VirtualHost" in them):

<VirtualHost *:80>
    ServerName sonar.x.x.x.x
    ServerAdmin admin@continualintegration.com
    ProxyPreserveHost On
    ProxyPass / http://localhost:9000/
    ProxyPassReverse / http://localhost:9000/
    TransferLog /var/log/httpd/sonar.x.x.x.x_access.log
    ErrorLog /var/log/httpd/sonar.x.x.x.x_error.log
</VirtualHost>

22.c.  Replace x.x.x.x with the IP address or domain name of your server.

22.d.  Replace "admin@continualintegration.com" with an email address of yours.

23.  Run these two commands:

sudo systemctl start httpd
sudo systemctl enable httpd

24.  Open a web browser.  Go to the external IP address of the server.  You should see the web UI for SonarQube.

Leave a comment

Your email address will not be published. Required fields are marked *