SonarQube是一個用於質量系統開發的開源工具。它是用Java編寫的,支持多個數據庫。它提供了持續檢查代碼、顯示應用程序的健康和突出新引入的問題的功能。它包含了用於檢測棘手問題的代碼分析程序。它也很容易與DevOps集成。
在本文中,筆者將與大家分享如何在Ubuntu 16.10上安裝最新版本的SonarQube。
准備:
一款使用至少2gb RAM的Ubuntu 64位的Ubuntu 16.04服務器實例。
sudo用戶。
步驟1:執行系統更新
在Ubuntu服務器實例上安裝任何包之前,建議對系統進行更新。使用sudo用戶登錄,並運行以下命令來更新系統。
sudo apt-get update
sudo apt-get -y upgrade
步驟2:安裝JDK
通過運行在服務器上添加Oracle Java存儲庫。
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
通過輸入來安裝Oracle JDK:
sudo apt install oracle-java8-installer
現在,您可以通過鍵入來檢查Java的版本:
java -version
步驟3:安裝和配置PostgreSQL
安裝PostgreSQL庫。
sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'
wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -
通過運行:安裝PostgreSQL數據庫服務器:
sudo apt-get -y install postgresql postgresql-contrib
啟動PostgreSQL服務器,並允許它在啟動時自動啟動:
sudo systemctl start postgresql
sudo systemctl enable postgresql
更改默認的PostgreSQL用戶的密碼。
sudo passwd postgres
切換到postgres用戶。
su - postgres
通過輸入創建新用戶:
createuser sonar
切換到PostgreSQL shell。
psql
為新創建的SonarQube數據庫設置一個密碼。
ALTER USER sonar WITH ENCRYPTED password 'StrongPassword';
通過運行:為PostgreSQL數據庫創建一個新的數據庫:
CREATE DATABASE sonar OWNER sonar;
退出psql shell:
\q
通過運行exit命令切換回sudo用戶。
步驟4:下載和配置SonarQube
下載SonarQube安裝文件存檔。
wget https://sonarsource.bintray.com/Distribution/sonarqube/sonarqube-6.4.zip
在SonarQube下載頁面上,您可以始終查找該應用程序的最新版本的鏈接。
安裝解壓運行:
apt-get -y install unzip
使用下面的命令解壓縮存檔。
sudo unzip sonarqube-6.4.zip -d /opt
重命名目錄:
sudo mv /opt/sonarqube-6.4 /opt/sonarqube
使用您最喜歡的文本編輯器打開SonarQube配置文件。
sudo nano /opt/sonarqube/conf/sonar.properties
找到下面的線。
#sonar.jdbc.username=
#sonar.jdbc.password=
取消注釋,並提供我們先前創建的數據庫的PostgreSQL用戶名和密碼。它應該看起來像:
sonar.jdbc.username=sonar
sonar.jdbc.password=StrongPassword
接下來,找到:
#sonar.jdbc.url=jdbc:postgresql://localhost/sonar
取消注釋,保存文件並退出編輯器。
步驟5:配置Systemd服務
可以直接使用安裝包中提供的啟動腳本啟動SonarQube。為了方便起見,您應該為SonarQube設置一個Systemd單元文件。
nano /etc/systemd/system/sonar.service
填充文件:
[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=root
Group=root
Restart=always
[Install]
WantedBy=multi-user.target
啟動應用程序:
sudo systemctl start sonar
允許SonarQube服務在啟動時自動啟動。
sudo systemctl enable sonar
要檢查服務是否正在運行,運行:
sudo systemctl status sonar
步驟5:配置反向代理
默認情況下,SonarQube在端口9000上監聽本地主機。在本教程中,我們將使用Apache作為反向代理,以便可以通過標准的HTTP端口訪問應用程序。通過運行安裝Apache web服務器:
sudo apt-get -y install apache2
啟用mod_proxy。
sudo a2enmod proxy
sudo a2enmod proxy_http
創建一個新的虛擬主機。
sudo nano /etc/apache2/sites-available/sonar.yourdomain.com.conf
填充文件:
<VirtualHost *:80>
ServerName sonar.yourdomain.com
ServerAdmin me@yourdomain.com
ProxyPreserveHost On
ProxyPass / http://localhost:9000/
ProxyPassReverse / http://localhost:9000/
TransferLog /var/log/httpd/sonar.yourdomain.com_access.log
ErrorLog /var/log/httpd/sonar.yourdomain.com_error.log
</VirtualHost>
啟用虛擬主機。
sudo a2ensite sonar.yourdomain.com.conf
啟動Apache並使它在啟動時自動啟動:
sudo systemctl start apache2
sudo systemctl enable apache2
如果您的服務器已經在運行,請重新啟動它:
sudo systemctl restart apache2
第六步:安裝完成
啟動SonarQube服務:
sudo systemctl start sonar
SonarQube安裝在您的服務器上,在以下地址訪問儀表板。
http://sonar.yourdomain.com
使用初始管理員帳戶、admin和admin登錄。您現在可以使用SonarQube不斷分析你所寫的代碼。