{"id":926,"date":"2019-09-08T03:33:20","date_gmt":"2019-09-08T03:33:20","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=926"},"modified":"2019-09-08T03:33:20","modified_gmt":"2019-09-08T03:33:20","slug":"setup-and-configure-sonarqube-on-linux","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=926","title":{"rendered":"How to Install and configure Sonarqube on Linux (RHEL\/Centos\/ec2)"},"content":{"rendered":"<p><a href=\"http:\/\/www.sonarqube.org\/?ref=devopscube.com\" rel=\"noopener noreferrer\">Sonarqube<\/a> is a great tool for source code quality management, code analysis, etc. This is the most widely used tool for code coverage and static code analysis that supports 17 languages.<\/p>\n<h2 id=\"install-and-configure-sonarqube-on-linux\">Install and  Configure Sonarqube on Linux<\/h2>\n<p>This guide will help you to set up and configure Sonarqube on Linux servers (<strong>Redhat\/Centos 7<\/strong> versions) on any cloud platforms like ec2, azure, compute engine, or on-premise data centers. Follow the steps given below for the complete Sonarqube configuration.<\/p>\n<h3 id=\"sonarqube-system-requirements\">Sonarqube System Requirements<\/h3>\n<p>Following are the minimum server requirement for running the Sonarqube server.<\/p>\n<ol>\n<li>Server with minimum 2GB RAM  and 1 vCPU capacity<\/li>\n<li>PostgreSQL version 9.3 or greater.<\/li>\n<li>OpenJDK 11 or JRE 11<\/li>\n<li>All Sonarquber processes should run as a non-root sonar user.<\/li>\n<\/ol>\n<p>You can find the official requirement <a href=\"https:\/\/docs.sonarqube.org\/latest\/requirements\/requirements\/?ref=devopscube.com\" rel=\"noreferrer noopener\">doc here<\/a>.<\/p>\n<h2 id=\"prepare-the-server-with-the-required-software\">Prepare the Server With the Required Software<\/h2>\n<h3 id=\"redhatamazon-linux\">Redhat\/Amazon Linux<\/h3>\n<p>If you are using Redhat, Centos, or Amazon Linux-based servers, execute the following commands one by one.<\/p>\n<pre><code>sudo yum update -y\nsudo yum install wget unzip -y\nsysctl vm.max_map_count\nsysctl fs.file-max\nulimit -n\nulimit -u<\/code><\/pre>\n<h3 id=\"for-ubuntudebian\">For Ubuntu\/Debian<\/h3>\n<p>If you are using Ubuntu or Debian-based server, execute the following commands one by one.<\/p>\n<pre><code>sudo apt update -y\nsudo apt install wget unzip -y\nsysctl vm.max_map_count\nsysctl fs.file-max\nulimit -n\nulimit -u<\/code><\/pre>\n<h2 id=\"setup-postgresql-10-for-sonarqube\">Setup PostgreSQL 10 For SonarQube<\/h2>\n<p><strong>Step 1:<\/strong> Install PostgreSQL 10 repo.<\/p>\n<pre><code>sudo yum install https:\/\/download.postgresql.org\/pub\/repos\/yum\/10\/redhat\/rhel-7-x86_64\/pgdg-centos10-10-2.noarch.rpm -y<\/code><\/pre>\n<p><strong>Step 2: <\/strong>Install PostgreSQL 10<\/p>\n<pre><code>sudo yum install postgresql10-server postgresql10-contrib -y<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Initialize the database.<\/p>\n<pre><code>sudo \/usr\/pgsql-10\/bin\/postgresql-10-setup initdb<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Open <code>\/var\/lib\/pgsql\/data\/pg_hba.conf<\/code> file to change the authentication to md5.<\/p>\n<pre><code>sudo vi \/var\/lib\/pgsql\/10\/data\/pg_hba.conf<\/code><\/pre>\n<p>Find the following lines at the bottom of the file and change <code>peer<\/code> to <code>trust<\/code> and <code>idnet<\/code> to <code>md5<\/code><\/p>\n<pre><code># TYPE  DATABASE        USER            ADDRESS                 METHOD\n\n# \"local\" is for Unix domain socket connections only\nlocal   all             all                                     peer\n# IPv4 local connections:\nhost    all             all             127.0.0.1\/32            ident\n# IPv6 local connections:\nhost    all             all             ::1\/128                 ident<\/code><\/pre>\n<p>Once changed, it should look like the following.<\/p>\n<pre><code># TYPE  DATABASE        USER            ADDRESS                 METHOD\n\n# \"local\" is for Unix domain socket connections only\nlocal   all             all                                     trust\n# IPv4 local connections:\nhost    all             all             127.0.0.1\/32            md5\n# IPv6 local connections:\nhost    all             all             ::1\/128                 md5<\/code><\/pre>\n<p><strong>Step 5: <\/strong>Start and enable PostgreSQL.<\/p>\n<pre><code>sudo systemctl start postgresql-10\nsudo systemctl enable postgresql-10<\/code><\/pre>\n<p><strong>Step 6:<\/strong> You can verify the installation using the following version select query.<\/p>\n<pre><code>sudo -u postgres \/usr\/pgsql-10\/bin\/psql -c \"SELECT version();\"<\/code><\/pre>\n<h2 id=\"create-sonar-user-and-database\">Create Sonar User and Database<\/h2>\n<p>We need to have a sonar user and database for the sonar application.<\/p>\n<p><strong>Step 1:<\/strong> Change the default password of the Postgres user. All Postgres commands have to be executed by this user.<\/p>\n<pre><code>sudo passwd postgres<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Login as postgres user with the new password.<\/p>\n<pre><code>su - postgres<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Log in to the PostgreSQL CLI.<\/p>\n<pre><code>psql<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Create a sonarqubedb database.<\/p>\n<pre><code>create database sonarqubedb;<\/code><\/pre>\n<p><strong>Step 5:<\/strong> Create the sonarqube DB user with a strongly encrypted password. Replace your-strong-password with a strong password.<\/p>\n<pre><code>create user sonarqube with encrypted password 'your-strong-password';<\/code><\/pre>\n<p><strong>Step 6:<\/strong> Next, grant all privileges to sonrqube user on sonarqubedb.<\/p>\n<pre><code>grant all privileges on database sonarqubedb to sonarqube<\/code><\/pre>\n<p><strong>Step 7:<\/strong> Exit the psql prompt using the following command.<\/p>\n<pre><code>\\q<\/code><\/pre>\n<p>Step 6: Switch to your sudo user using the exit command.<\/p>\n<pre><code>exit<\/code><\/pre>\n<h2 id=\"setup-sonarqube-web-server\">Setup Sonarqube Web Server<\/h2>\n<p><strong>Step 1:<\/strong> Download the latest Sonarqube installation file to \/opt folder. You can get the latest download <a href=\"http:\/\/www.sonarqube.org\/downloads\/?ref=devopscube.com\" rel=\"noreferrer noopener\">link from here<\/a>.<\/p>\n<pre><code>cd \/opt \nsudo wget https:\/\/binaries.sonarsource.com\/Distribution\/sonarqube\/sonarqube-7.6.zip<\/code><\/pre>\n<p>2. Unzip sonarqube source files and rename the folder.<\/p>\n<pre><code>sudo unzip sonarqube-7.6.zip\nsudo mv sonarqube-7.6 sonarqube<\/code><\/pre>\n<p>4. Open <code>\/opt\/sonarqube\/conf\/sonar.properties<\/code> file.<\/p>\n<pre><code>sudo vi \/opt\/sonarqube\/conf\/sonar.properties<\/code><\/pre>\n<p>Uncomment and edit the parameters as shown below. Change the password accordingly. You will find the JDBC parameter under the PostgreSQL section.<\/p>\n<pre><code>sonar.jdbc.username=sonar                                                                                                                     \nsonar.jdbc.password=sonar-db-password\nsonar.jdbc.url=jdbc:postgresql:\/\/localhost\/onarqubedb<\/code><\/pre>\n<p>By default, sonar will run on 9000. If you want on port 80 or any other port, change the following parameters for accessing the web console on that specific port.<\/p>\n<pre><code>sonar.web.host=0.0.0.0\nsonar.web.port=80<\/code><\/pre>\n<p>If you want to access Sonarqube some path like http:\/\/url:\/sonar, change the following parameter.<\/p>\n<pre><code>sonar.web.context=\/sonar<\/code><\/pre>\n<h2 id=\"add-sonar-user-and-privileges\">Add Sonar User and Privileges<\/h2>\n<p>Create a user named sonar and make it the owner of the <code>\/opt\/sonarqube<\/code> directory.<\/p>\n<pre><code>sudo useradd sonar\nsudo chown -R sonar:sonar \/opt\/sonarqube<\/code><\/pre>\n<h2 id=\"start-sonarqube-service\">Start Sonarqube Service<\/h2>\n<p>To start sonar service, you need to use the script in sonarqube bin directory.<\/p>\n<p><strong>Step 1:<\/strong> Login as sonar user<\/p>\n<pre><code>sudo su - sonar<\/code><\/pre>\n<p><strong>Step 2:<\/strong> Navigate to the start script directory.<\/p>\n<pre><code>cd \/opt\/sonarqube\/bin\/linux-x86-64 <\/code><\/pre>\n<p><strong>Step 3:<\/strong> Start the sonarqube service.<\/p>\n<pre><code>.\/sonar.sh start<\/code><\/pre>\n<p>Now, you should be able to access sonarqube on the browser on port <strong>9000<\/strong><\/p>\n<p><strong>Step 4: <\/strong>Check the application status. If it is in a running state, you can access the sonarqube dashboard using the DNS name or Ip address of your server.<\/p>\n<pre><code>sudo .\/sonar.sh status<\/code><\/pre>\n<h2 id=\"setting-up-sonarqube-as-systemd-service\">Setting up Sonarqube as Systemd Service<\/h2>\n<p><strong>Step 1<\/strong>: Create a file <code>\/etc\/systemd\/system\/sonarqube.service<\/code><\/p>\n<pre><code>sudo vi \/etc\/systemd\/system\/sonarqube.service<\/code><\/pre>\n<p>Step 2: Copy the following content onto the file.<\/p>\n<pre><code>[Unit]\nDescription=SonarQube service\nAfter=syslog.target network.target\n\n[Service]\nType=simple\nUser=sonarqube\nGroup=sonarqube\nPermissionsStartOnly=true\nExecStart=\/bin\/nohup java -Xms32m -Xmx32m -Djava.net.preferIPv4Stack=true -jar \/opt\/sonarqube\/lib\/sonar-application-7.6.jar\nStandardOutput=syslog\nLimitNOFILE=65536\nLimitNPROC=8192\nTimeoutStartSec=5\nRestart=always\n\n[Install]\nWantedBy=multi-user.target<\/code><\/pre>\n<p><strong>Step 3:<\/strong> Start and enable sonarqube<\/p>\n<pre><code>sudo systemctl start sonarqube\nsudo systemctl enable sonarqube<\/code><\/pre>\n<p><strong>Step 4:<\/strong> Check the sonarqube status to ensure it is running as expected.<\/p>\n<pre><code>sudo systemctl status  sonarqube<\/code><\/pre>\n<h2 id=\"troubleshooting-sonarqube\">Troubleshooting Sonarqube<\/h2>\n<p>All the logs of sonarqube are present in the <code>\/opt\/sonarqube\/logs<\/code> directory.<\/p>\n<pre><code>cd \/opt\/sonarqube\/logs<\/code><\/pre>\n<p>You can find the following log files.<\/p>\n<pre><code>es.log\nsonar.log\nweb.log\naccess.log<\/code><\/pre>\n<p>Using the tail command you can check the latest logs. For example,<\/p>\n<pre><code>tail -f access.log<\/code><\/pre>\n<p>For Sonarqube support, visit<a href=\"http:\/\/www.sonarqube.org\/get-support\/?ref=devopscube.com\" rel=\"noopener noreferrer\"> this link<\/a><\/p>\n<p>In this tutorial, we covered how to install and configure Sonarqube on a Linux server. If you face any issues during the setup, please feel free to leave a comment below.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-and-configure-sonarqube-on-linux\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Install and configure Sonarqube on Linux (RHEL\/Centos\/ec2) \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-and-configure-sonarqube-on-linux\/<\/p>\n","protected":false},"author":1,"featured_media":927,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-926","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/926","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=926"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/926\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/927"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=926"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=926"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=926"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}