如何在 Ubuntu 20.04 上使用 Let’s Encrypt 安裝 SonarQube

每個開發人員都夢想擁有乾淨且無問題的代碼,可以輕鬆地將其部署到暫存和生產環境中。 可以幫助您實現這一目標的一種工具是在您的 CI/CD 管道中 聲納管. 聲納管 是一種跨平台和基於 Web 的工具,用於持續檢查源代碼。 它是用Java編寫的。 SonarQube 使您能夠通過檢查代碼和檢測錯誤和其他不一致來編寫更乾淨、更安全的代碼。

SonarQube 可以集成到 GitHub、Gitlab、BitBucket 和 Azure DevOps 等平台中,舉幾個平台。 它有各種版本,包括社區版、開發者版、企業版和數據中心版。

在本教程中,我們將學習如何 安裝 SonarQubeUbuntu 20.04. 我們將安裝社區版,因為它可以免費下載和啟用 SSL證書 (https) 與 讓我們加密 通過將 Nginx 設置為反向代理。

先決條件

在開始之前,請確保您滿足以下要求:

  1. Ubuntu 20.04 LTS sudo 用戶配置。
  2. 確保您的系統至少有 4GB RAM 和 2vCPU 內核

您將需要安裝一些工具

$ sudo apt update
$ sudo apt install net-tools unzip vim curl

另外,你需要增加虛擬內存內核

$ sudo sysctl -w vm.max_map_count=262144

具有最大打開文件數

$ sudo sysctl -w fs.file-max=65536

和資源限制

$ ulimit -n 65536

$ ulimit -u 4096

您可以通過修改系統參數來使更改持久化 /etc/sysctl.conf 配置文件

$ sudo vim /etc/sysctl.conf

添加以下幾行。

vm.max_map_count=262144
fs.file-max=65536
ulimit -n 65536
ulimit -u 4096

Save 並退出。 之後,打開 limits.conf 文件

$ sudo vim /etc/security/limits.conf

在最底部,添加以下幾行

sonarqube - nofile 65536
sonarqube - nproc 4096

Save 並退出。 要使更改生效,請重新啟動服務器。

第 1 步:安裝 OpenJDK

由於它是用 Java 編寫的,SonarQube 依賴於 Java 來運行。 我們將安裝提供 Java 的 OpenJDK 11。

$ sudo apt install openjdk-11-jdk

安裝後,您可以驗證 Java 的版本。

$ java -version

第二步:安裝PostgreSQL數據庫

從 2016 年開始,SonarQube 不再支持 MySQL,現在只支持 PostgreSQL。 因此,我們需要安裝 PostgreSQL 數據庫。

首先,下載並添加 PostgreSQL GPG 密鑰。

$ wget -q https://www.postgresql.org/media/keys/ACCC4CF8.asc -O - | sudo apt-key add -

此後,添加 PostgreSQL 存儲庫。

$ sudo sh -c 'echo "deb https://apt.postgresql.org/pub/repos/apt/ `lsb_release -cs`-pgdg main" >> /etc/apt/sources.list.d/pgdg.list'

然後更新包索引以同步新的存儲庫。

$ sudo apt update

完成更新包列表後,安裝 PostgreSQL 數據庫及其依賴項。

$ sudo apt install postgresql postgresql-contrib

默認情況下,PostgreSQL 服務在安裝後啟動,如果沒有啟動運行以下命令。

$ sudo systemctl start postgresql

只是為了確認一切都按預期運行,請驗證其運行狀態。

$ sudo systemctl status postgresql

您還可以確認它正在偵聽的端口:

$ sudo netstat -pnltu | grep 5432

tcp 0 0 127.0.0.1:5432 0.0.0.0:* LISTEN 7768/postgres
tcp6 0 0 ::1:5432 :::* LISTEN 7768/postgres

啟用 PostgreSQL 在啟動時自動啟動:

$ sudo systemctl enable postgresql

到目前為止,我們的 PostgreSQL 已經正常運行。

第 3 步:配置 PostgreSQL

繼續,我們將為 Postgres 用戶設置密碼,該密碼通常在安裝 PostgreSQL 時默認出現。 為此,請運行以下命令:

$ sudo passwd postgres

輸入密碼並確認。 接下來,切換到 Postgres 用戶。

$ su - postgres

接下來,繼續並創建一個新的數據庫用戶。

$ createuser sonar

完成後,使用以下命令切換到 PostgreSQL 提示符:

$ psql

通過訪問 PostgreSQL shell,為您剛剛創建的用戶創建密碼。

ALTER USER sonar WITH ENCRYPTED PASSWORD 'strong_password';

接下來,使用您創建的用戶作為所有者創建一個 SonarQube 數據庫

CREATE DATABASE sonarqube OWNER sonar;

然後,分配或授予數據庫使用的所有權限,以便他們擁有修改數據庫的所有權限。

GRANT ALL PRIVILEGES ON DATABASE sonarqube to sonar;

現在退出數據庫。

q

第 4 步:下載並配置 SonarQube

繼續,我們將下載最新的 SonarQube 二進制 zip 文件。 目前,最新的社區版版本,即長期服務 (LTS) 版本是 SonarQube 版本 9.0.1。 你可以前往 SonarQube 下載頁面 獲取最新下載。

要下載 zip 文件,請發出以下命令:

$ wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-9.0.1.46107.zip

接下來,解壓縮壓縮文件。

$ unzip sonarqube-9.0.1.46107.zip

並將其移動到 /opt/ 路徑。

$ sudo mv sonarqube-9.0.1.46107 /opt/sonarqube

第 5 步:創建新用戶和組

繼續,我們將創建一個將運行 SonarQube 服務的新用戶和組。 所以,創建組。

$ sudo groupadd sonar

接下來,在將用戶添加到新創建的組時,創建主目錄設置為 /opt/sonarqube 的用戶。

$ sudo useradd -c "SonarQube - User" -d /opt/sonarqube/ -g sonar sonar

然後將所有權設置為 /opt/sonarqube 目錄。

$ sudo chown -R sonar:sonar /opt/sonarqube/

第 6 步:配置 SonarQube

現在讓我們配置 SonarQube。 打開 SonarQube 配置文件。

$ sudo vim /opt/sonarqube/conf/sonar.properties

找到並取消註釋以下行

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

這些代表我們在 PostgreSQL 數據庫服務器中創建的 SonarQube 數據庫用戶和密碼。 因此,請相應地填寫值。

sonar.jdbc.username=sonar_user
sonar.jdbc.password=strong_password

接下來,修改這些行,使它們看起來像所提供的那樣

sonar.jdbc.url=jdbc:postgresql://localhost:5432/sonarqube

sonar.search.javaOpts=-Xmx512m -Xms512m -XX:MaxDirectMemorySize=256m -XX:+HeapDumpOnOutOfMemoryError

此後,修改以下幾行以顯示它們的外觀。

sonar.web.host=0.0.0.0

sonar.web.port=9000

sonar.web.javaAdditionalOpts=-server

sonar.log.level=INFO

sonar.path.logs=logs

接下來,通過編輯顯示的文件來修改將運行 SonarQube 服務的用戶。

$ sudo vim /opt/sonarqube/bin/linux-x86-64/sonar.sh

向下滾動並確保下面的行如圖所示。

RUN_AS_USER=sonar

步驟 7:為 SonarQube 創建 Systemd 服務文件

目前,我們的系統無法啟動 SonarQube 服務。 因此,我們需要創建一個systemd服務。 為此,請運行以下命令:

$ sudo vim /etc/systemd/system/sonarqube.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=sonar
Group=sonar
Restart=always
LimitNOFILE=65536
LimitNPROC=4096

[Install]
WantedBy=multi-user.target

Save 更改並退出文件。

啟用 SonarQube 服務以在啟動時啟動

$ sudo systemctl enable sonarqube

並啟動 SonarQube 服務。

$ sudo systemctl start sonarqube

要確保 SonarQube 服務正在運行,請執行以下命令:

$ sudo systemctl status sonarqube

另外,請確保端口 9000 已打開

$ sudo ufw allow '9000'

Rules updated
Rules updated (v6)

檢查服務是否正在偵聽端口 9000

$ sudo netstat -pnltu | grep 9000

tcp6 0 0 :::9000 :::* LISTEN 65140/java

現在您可以嘗試通過輸入您的服務器的公共 IP 和瀏覽器的端口號來檢查是否可以訪問登錄頁面,即 https://<server-ip>:9000/. 默認管理員用戶名是 ‘admin’,密碼是 ‘admin’。

SonarQube 登錄頁面

Sonarqube 將要求您更新密碼以更改默認密碼。

SonarQube 密碼更改

第 8 步:使用 SSL 安裝和配置 Nginx(可選)

為了使用啟用 SSL 的域名訪問您的 Sonarqube,您需要安裝一個反向代理,如 Nginx。 網絡服務器將代理與 SonarQube 的連接,以便開發人員可以從安全域名訪問它。

安裝 Nginx 簡單明了,只需一個命令即可完成。

$ sudo apt install nginx

安裝後,啟用 Nginx 網絡服務器以在引導時啟動。

$ sudo systemctl enable nginx

並啟動服務

$ sudo systemctl start nginx

為了讓網絡服務器知道 SonarQube,我們將創建一個顯示的配置文件。

$ sudo vim /etc/nginx/sites-available/sonarqube.conf

然後粘貼提供的內容。

server {

listen 80;
server_name example.com or SERVER-IP;
access_log /var/log/nginx/sonar.access.log;
error_log /var/log/nginx/sonar.error.log;
proxy_buffers 16 64k;
proxy_buffer_size 128k;

location / {
proxy_pass https://127.0.0.1:9000;
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
}
}

Save 並退出。 然後啟用 Sonarqube 站點:

$ sudo ln -s /etc/nginx/sites-available/sonarqube.conf /etc/nginx/sites-enabled/sonarqube.conf

檢查配置是否正確

$ sudo nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

然後重新啟動 Nginx Web 服務器以應用更改。

$ sudo systemctl restart nginx

現在我們需要配置UFW防火牆以允許nginx。 要完成此操作,請運行命令

$ sudo ufw allow 'Nginx Full'

然後重新加載防火牆以使更改生效。

$ sudo ufw --reload

您現在可以通過其域名訪問您的 SonarQube

在這裡,我們將使用免費的 讓我們加密證書。 要配置它,我們需要為 Nginx 運行 cerbot:

$ sudo certbot --nginx

Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Enter email address (used for urgent renewal and security notices) (Enter 'c' to
cancel): [email protected]

Please read the Terms of Service at
https://letsencrypt.org/documents/LE-SA-v1.2-November-15-2017.pdf. You must
agree in order to register with the ACME server at
https://acme-v02.api.letsencrypt.org/directory

(A)gree/(C)ancel: A

Would you be willing to share your email address with the Electronic Frontier
Foundation, a founding partner of the Let's Encrypt project and the non-profit
organization that develops Certbot? We'd like to send you email about our work
encrypting the web, EFF news, campaigns, and ways to support digital freedom.

(Y)es/(N)o: N
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator nginx, Installer nginx
Which names would you like to activate HTTPS for?

1: websitefortesting.com

Select the appropriate numbers separated by commas and/or spaces, or leave input
blank to select all options shown (Enter 'c' to cancel): 1
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for websitefortesting.com
Waiting for verification…
Cleaning up challenges
Deploying Certificate to VirtualHost /etc/nginx/sites-enabled/sonarqube.conf
Please choose whether or not to redirect HTTP traffic to HTTPS, removing HTTP access.

1: No redirect - Make no further changes to the webserver configuration.
2: Redirect - Make all requests redirect to secure HTTPS access. Choose this for
new sites, or if you're confident your site works on HTTPS. You can undo this
change by editing your web server's configuration.

Select the appropriate number [1-2] then [enter] (press 'c' to cancel): 2
Redirecting all traffic on port 80 to ssl in /etc/nginx/sites-enabled/sonarqube.conf

Congratulations! You have successfully enabled https://websitefortesting.com
You should test your configuration at:
https://www.ssllabs.com/ssltest/analyze.html?d=websitefortesting.com

IMPORTANT NOTES:
Congratulations! Your certificate and chain have been saved at:
/etc/letsencrypt/live/websitefortesting.com/fullchain.pem
Your key file has been saved at:
/etc/letsencrypt/live/websitefortesting.com/privkey.pem
Your cert will expire on 2021-11-27. To obtain a new or tweaked
version of this certificate in the future, simply run certbot again
with the "certonly" option. To non-interactively renew all of
your certificates, run "certbot renew"
If you like Certbot, please consider supporting our work by:
Donating to ISRG / Let's Encrypt: https://letsencrypt.org/donate
Donating to EFF: https://eff.org/donate-le

默認情況下,讓我們加密會在 Nginx 服務器塊文件中添加一些行。

你應該有這樣的東西

server {
server_name websitefortesting.com;
add_header Strict-Transport-Security max-age=2592000;
#rewrite ^ https://$server_name$request_uri? permanent;
access_log /var/log/nginx/sonarqube.access.log;
error_log /var/log/nginx/sonarqube.error.log;
proxy_buffers 16 64k;
proxy_buffer_size 128k;

location / {
proxy_pass https://127.0.0.1:9000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto http;
}
listen 443 ssl; # managed by Certbot
ssl_certificate /etc/letsencrypt/live/websitefortesting.com/fullchain.pem; # managed by Certbot
ssl_certificate_key /etc/letsencrypt/live/websitefortesting.com/privkey.pem; # managed by Certbot
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
if ($host = websitefortesting.com) {
return 301 https://$host$request_uri;
} # managed by Certbot

listen 80;
server_name websitefortesting.com; return 404; # managed by Certbot

}

第 10 步:使用 HTTPS 訪問 SonarQube

現在,您可以使用配置了 let’s encrypt 的 HTTPS URL 安全地訪問 SonarQube。

https://domain-name

啟用 SSL 的 SonarQube

登錄後,您將看到登錄頁面。

結論

在本教程中,我們學習瞭如何在 Ubuntu 20.04 上安裝 SonarQube。 我們使用 Let’s Encrypt 證書啟用了帶有 SSL 的 Sonarqube,使用 Nginx 作為反向代理。