Sonarqube Installation on Centos/Rhel-Sonarqube with Mysql

Sonarqube Installation on centos 7/Rhel

Sonarqube is opensource continuous code quality integration tool. By using sonarqube we can identifies code vulnerabilities,bugs and code quality. In this recipe i will show you how to install sonarqube on centos 7 and  rhel 7 amazon linux and i will show you how to configure sonarqube with mysql in centos rhel and amazon linux..

Install mysql:

in the following steps i will configure sonarqube with mysql. Download the repository, update and install MySQL

sudo yum install wget unzip -y
wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
sudo rpm -ivh mysql-community-release-el7-5.noarch.rpm
yum update -y
sudo yum install mysql-server
sudo systemctl start mysqld

Create db and user for Sonarqube:

Login into MySQL and execute below commands step by step.

to login to MySQL use this command “mysql -u root”

mysql -u root
CREATE DATABASE sonarqube_db;
CREATE USER 'sonarqube_user'@'localhost' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON sonarqube_db.* TO 'sonarqube_user'@'localhost' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;
exit

Download and install SonarQube:

wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-6.7.6.zip
unzip sonarqube-6.7.6.zip
mv sonarqube-6.7.6 /opt/sonarqube

Create a user for SonarQube:

Create a new user for SonarQube. Because we cannot run the newer versions of sonarqube as the root user. If you run sonarqube with root user will get error like java.lang.RuntimeException: can not run elasticsearch as root.

Since we are running SonarQube as a separate user, assign proper ownership to SonarQube files.

useradd sonarqube
passwd sonarqube
chown -R sonarqube:sonarqube /opt/sonarqube

Open the SonarQube startup script and specify the sonarqube user details.

vi /opt/sonarqube/bin/linux-x86-64/sonar.sh
#add below line
RUN_AS_USER=sonarqube

Configure sonarqube with MySQL database:

open the SonarQube configuration file

vi /opt/sonarqube/conf/sonar.properties add the below lines

vi /opt/sonarqube/conf/sonar.properties
#add below lines
sonar.jdbc.username=sonarqube_user
sonar.jdbc.password=password
sonar.jdbc.url=jdbc:mysql://localhost:3306/sonarqube_db?useUnicode=true&characterEncoding=utf8&rewriteBatchedStatements=true&useConfigs=maxPerformance

Start SonarQube

You can now start SonarQube by running the following command.

/opt/sonarqube/bin/linux-x86-64/sonar.sh start

That’s all we have configured sonarqube successfully in centos and rhel.

If you have done everything correctly, You can access sonarqube using http://ipaddress:9000

Note: for sonarqube you need minumum 2gb ram in your server. then only sonarqube will work as expected.

The following are optional

Configuring sonarqube as a Systemd service:

if you want to configure sonar as a systemd service follow below steps

sudo /opt/sonarqube/bin/linux-x86-64/sonar.sh stop
sudo vi /etc/systemd/system/sonar.service
add below lines

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

[Install]
WantedBy=multi-user.target

restart the sonar service with systemd:

sudo systemctl restart sonar
sudo systemctl start sonar
sudo systemctl enable sonar

verify the status with sudo systemctl status sonar

Change sonarqube port and web context:

By default, sonar will run on 9000. If you want to access sonarqube like http://url/sonar follow below steps

vi /opt/sonarqube/conf/sonar.properties
add below lines
sonar.web.context=/sonar
sonar.web.port=80

systemctl restart sonar.service

or


/opt/sonarqube/bin/linux-x86-64/sonar.sh stop

/opt/sonarqube/bin/linux-x86-64/sonar.sh start

if you want to change just port number of sonarqube server, add sonar.web.port=XXX X  to sonar.properties file and restart the sonarqube service.

  • install sonarqube on centos 7
  • how to install sonarqube in centos 6
  • sonarqube installation
  • sonarqube mysql setup
  • sonarqube installation and configuration
  • install sonarqube on amazon linux
  • install sonarqube on rhel7

Leave a Reply

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