LoginSignup
2
4

More than 3 years have passed since last update.

EC2にSonarQubeをインストールする

Last updated at Posted at 2020-07-13

はじめに

最近、EC2(amazon linux2)にSonarQubeをインストールしました。
その際の手順をまとめた記事となります。

元は、自分用にメモしたものなので、コマンドはコピペで使えるかと思います。

環境

  • EC2を使用、以下の条件としています。
    • amazon linux2
    • t2.medium(microだと、メモリが足りず、SonarQubeの要件を満たせません)
    • セキュリティグループのインバウンド(22、9000(SonarQube))、アウトバウンド(80/443)を許可
    • EC2には、sshで接続して作業

バージョン

  • PostgreSQL 12 を使用
  • JAVA 11(Amazon Corretto 11) を使用
  • SonarQube 7.9 を使用

作業の流れ

  1. 事前準備
  2. PostgreSQLのインストール
  3. JAVAのインストール
  4. SonarQubeのインストール

0. 事前準備

SonarQube実行ユーザの作成

SonarQube実行ユーザが必要となるため、事前に作成します。
ここでは、sonar としています。

$ sudo useradd sonar

1. PostgreSQLのインストール

こちらのページを参考にさせて頂きました。
https://qiita.com/tmiki/items/00d22edc6a554f61bd04

PostgreSQL 12をインストールします。

リポジトリの更新

$ sudo rpm -ivh --nodeps https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm

リポジトリの修正

$ sudo sed -i 's/\$releasever/7/g' /etc/yum.repos.d/pgdg-redhat-all.repo

インストール

$ sudo yum install -y postgresql12 postgresql12-server
$ sudo systemctl enable postgresql-12.service

DBクラスタの初期化

$ PGSETUP_INITDB_OPTIONS='--encoding=UTF-8 --locale=C' sudo /usr/pgsql-12/bin/postgresql-12-setup initdb

設定ファイル修正

  • ファイル
$ sudo vi /var/lib/pgsql/12/data/postgresql.conf
  • 内容
-#listen_addresses = 'localhost'         # what IP address(es) to listen on;
+listen_addresses = '0.0.0.0'            # what IP address(es) to listen on;
                                         # comma-separated list of addresses;
                                         # defaults to 'localhost'; use '*' for
  • ファイル
$ sudo vi /var/lib/pgsql/12/data/pg_hba.conf
  • 内容
 # "local" is for Unix domain socket connections only
 local   all             all                                     trust
 # IPv4 local connections:
-host    all             all             127.0.0.1/32            trust
+host    all             all             0.0.0.0/0               md5
 # IPv6 local connections:
 host    all             all             ::1/128                 trust---

Postgresを起動

$ sudo systemctl start postgresql-12.service

Postgresに接続

$ sudo su - postgres
$ psql

SonarQube用のユーザ、データベースを作成

ここでは、ユーザ、及びパスワードを sonar としています。

create user sonar;
alter user sonar with password 'sonar';
create database sonar owner sonar TEMPLATE template1;

作成後、Postgresを切断、ec2-userに戻ります。

2.Javaのインストール

インストール

$ sudo yum install -y java-11-amazon-corretto

カーネルパラメータの調整

  • ファイル(新規作成)
$ sudo vi /etc/sysctl.d/99-sonarqube.conf
  • 内容
+fs.file-max= 65536
+vm.max_map_count= 262144

Ulimitの設定

  • ファイル(新規作成)
$ sudo vi /etc/security/limits.d/99-sonarqube.conf
  • 内容

実行ユーザを変更している場合には、sonar を適宜変更して下さい。

+sonar   -   nofile   65536
+sonar   -   nproc    8192

今回は、systemdを使用して起動していますので、上記設定は不要ですが、
systemdを使用せず、SonarQubeを起動する際には必要となるため、設定しております。
systemdを使用しない起動/停止は、以下のイメージとなります。

$ sudo su sonar -c "/usr/local/sonarqube-7.9.3/bin/linux-x86-64/sonar.sh start"  # 起動
$ sudo su sonar -c "/usr/local/sonarqube-7.9.3/bin/linux-x86-64/sonar.sh stop"   # 停止

カーネルパラメータ/ulimitを反映するため、EC2を再起動

$ sudo shutdown -r now

3.SonarQubeのインストール

ダウンロード

実行ユーザを変更している場合には、sonar を適宜変更して下さい。

$ wget -P /tmp https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.3.zip
$ unzip /tmp/sonarqube-7.9.3.zip
$ sudo mv sonarqube-7.9.3 /usr/local/
$ sudo chown -R sonar:sonar /usr/local/sonarqube-7.9.3/

Postgresを使用するよう設定ファイルを修正

  • ファイル
$ sudo vi /usr/local/sonarqube-7.9.3/conf/sonar.properties
  • 内容

Postgresのユーザ/パスワードを変更している場合には、sonar を適宜変更して下さい。

@@ -13,8 +13,8 @@
 # User credentials.
 # Permissions to create tables, indices and triggers must be granted to JDBC user.
 # The schema must be created first.
-#sonar.jdbc.username=
-#sonar.jdbc.password=
+sonar.jdbc.username=sonar
+sonar.jdbc.password=sonar

 #----- Embedded Database (default)
 # H2 embedded database server listening port, defaults to 9092
@@ -31,7 +31,7 @@

 #----- PostgreSQL 9.3 or greater
 # By default the schema named "public" is used. It can be overridden with the parameter "currentSchema".
-#sonar.jdbc.url=jdbc:oracle:thin:@localhost:1521/XE
+sonar.jdbc.url=jdbc:postgresql://localhost/sonar

systemdに追加

  • ファイル(新規作成)
$ sudo vi /etc/systemd/system/sonarqube.service
  • 内容

実行ユーザを変更している場合には、User/Group を適宜変更して下さい。
JAVAのバージョンを替えたい場合には、ExecStart のパスを変更して下さい。

+[Unit]
+Description=SonarQube service
+After=syslog.target network.target
+
+[Service]
+Type=simple
+User=sonar
+Group=sonar
+PermissionsStartOnly=true
+ExecStart=/bin/nohup /usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java -Xms32m -Xmx32m -Djava.net.preferIPv4Stack=true -jar /usr/local/sonarqube-7.9.3/lib/sonar-application-7.9.3.jar
+StandardOutput=syslog
+LimitNOFILE= 65536
+LimitNPROC=8192
+TimeoutStartSec=5
+Restart=always
+SuccessExitStatus=143
+
+[Install]
+WantedBy=multi-user.target

systemdをenableに変更

$ sudo systemctl enable sonarqube.service

起動

$ sudo systemctl start sonarqube.service

SonarQubeに接続

http://<EC2のGIP>:9000

初期のID/パスワードは共にadminになります。

終わりに

今回は、ログインできるところまで確認しております。
基本的には、コピペでインストール可能なはずですが、もし、SonarQubeが動かない場合は、ログをご確認下さい。

ログは、sonarqubeのlogs配下に出力されます。今回は、/usr/local/配下に置きましたので、

/usr/local/sonarqube-7.9.3/logs

に出力されます。
こちらの sonar.log をご確認下さい。

また、念の為、前提条件(https://docs.sonarqube.org/latest/requirements/requirements/)
を確認してみて下さい。
自分の場合、確認したつもりが、見落としがありました。。。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4