見出し画像

Amazon Linux 2 に Redmine をインストール

子供の日。GW に福井へ行く予定が、長男が塾の宿題をやっていないことが分かり、急きょ勉強デーになりました。
数学の計算問題などは僕も一緒にやったりしましたが、図形問題などはお呼びがかからず暇だったので、久しぶりに Redmine 環境を構築することにしました。

Redmine は Bitnami を利用するとお手軽ですし、SaaS 版もあります。
そのため、わざわざインストールする機会は少ないと思いますが、結構ハマりポイントがあってトラブルシュートが勉強になります。

これまでも 3 度ほどトライしていまして、2 勝 2 敗といったところです。

今回は、以下の Ubuntu 20.04 にインストールを試しましたが、OpenSSL まわりでうまくいかず挫折しました。
そのため、この記事は以前成功した Amazon Linux 2 の実績です。

EC2 インスタンス起動

まず、仮想マシンを起動します。
SSH は使わず、セッションマネージャーを利用してアクセスします。
EC2 コンソールから、[インスタンスを起動] ボタンをクリック。

名前とタグ

名前とタグは「my-redmine-amazonlinux2」にしました。

マシンイメージ

AMI は Amazon Linux を選択。Amazon Linux 2 AMI (HVM) - Kernel 5.10, SSD Volume Type が選択されました。

インスタンスタイプ

過去に passenger でメモリ不足のエラーが表示されてインストールできなかった記憶があるため、インスタンスファミリーは t3.small にしました。

キーペア (ログイン)

男らしくキーペアなし。

ネットワーク設定

セッションマネージャーを使うため、パブリックサブネットを指定して、パブリック IP アドレスを付与しました。セキュリティグループは 80 ポート、3000 ポートのみ開放。

ストレージ

gp2 から gp3 に変更しました。

高度な詳細

セッションマネージャーを利用するため、IAM インスタンスプロファイル (IAM ロール) は AmazonEC2RoleforSSM をアタッチしました。
中断あたりにあるクレジット仕様はスタンダードにしました。

2 分程度でステータスチェックを待たずに、セッションマネージャー経由でログインができました。

sh-4.2$ sudo -i
[root@ip-172-29-0-81 ~]# su - ec2-user
[ec2-user@ip-172-29-0-81 ~]$

Apache などミドルウエアのインストール

ひとまず yum update

[ec2-user@ip-172-29-0-81 ~]$ sudo yum update -y
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package curl.x86_64 0:7.79.1-1.amzn2.0.1 will be updated
---> Package curl.x86_64 0:7.79.1-2.amzn2.0.1 will be an update
---> Package libcurl.x86_64 0:7.79.1-1.amzn2.0.1 will be updated
---> Package libcurl.x86_64 0:7.79.1-2.amzn2.0.1 will be an update
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================================================================
Package                      Arch                        Version                                    Repository                       Size
Updating:
curl                         x86_64                      7.79.1-2.amzn2.0.1                         amzn2-core                      359 k
libcurl                      x86_64                      7.79.1-2.amzn2.0.1                         amzn2-core                      321 k
Transaction Summary
===========================================================================================================================================
Upgrade  2 Packages

Total download size: 681 k
Downloading packages:
Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
(1/2): libcurl-7.79.1-2.amzn2.0.1.x86_64.rpm                                                                        | 321 kB  00:00:00
(2/2): curl-7.79.1-2.amzn2.0.1.x86_64.rpm
-------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                      4.7 MB/s | 681 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating   : libcurl-7.79.1-2.amzn2.0.1.x86_64                                                                                       1/4
Updating   : curl-7.79.1-2.amzn2.0.1.x86_64                                                                                          2/4
Cleanup    : curl-7.79.1-1.amzn2.0.1.x86_64                                                                                          3/4
Cleanup    : libcurl-7.79.1-1.amzn2.0.1.x86_64                                                                                       4/4
Verifying  : libcurl-7.79.1-2.amzn2.0.1.x86_64                                                                                       1/4
Verifying  : curl-7.79.1-2.amzn2.0.1.x86_64                                                                                          2/4
Verifying  : libcurl-7.79.1-1.amzn2.0.1.x86_64                                                                                       3/4
Verifying  : curl-7.79.1-1.amzn2.0.1.x86_64                                                                                          4/4

Updated:
curl.x86_64 0:7.79.1-2.amzn2.0.1                                   libcurl.x86_64 0:7.79.1-2.amzn2.0.1

Complete!
[ec2-user@ip-172-29-0-81 ~]$

ホスト名を変更

ホスト名は「my-redmine」にしました。ホスト名はどちらでも結構ですが、ログを確認するときなどに UTC+9 にしておいた方が便利です。

[ec2-user@ip-172-29-0-81 ~]$ hostnamectl
   Static hostname: ip-172-29-0-81.ap-northeast-1.compute.internal
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2d99ef724dce45369047869bf2504a0b
           Boot ID: 5e73fb48635744dd8506752bafba2a99
    Virtualization: amazon
  Operating System: Amazon Linux 2
       CPE OS Name: cpe:2.3:o:amazon:amazon_linux:2
            Kernel: Linux 5.10.109-104.500.amzn2.x86_64
      Architecture: x86-64
[ec2-user@ip-172-29-0-81 ~]$ sudo hostnamectl set-hostname my-redmine
[ec2-user@ip-172-29-0-81 ~]$ sudo timedatectl set-timezone Asia/Tokyo
[ec2-user@ip-172-29-0-81 ~]$ exit
logout
[root@ip-172-29-0-81 ~]# su - ec2-user
Last login: Sat May  7 15:16:28 JST 2022 on pts/0
[ec2-user@my-redmine ~]$

Apache インストール

[ec2-user@my-redmine ~]$ sudo yum install httpd -y
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies

(中略)

Complete!
[ec2-user@my-redmine ~]$

ロケール、キーボードレイアウト設定・確認

[ec2-user@my-redmine ~]$ localectl status
System Locale: LANG=en_US.UTF-8
VC Keymap: n/a
X11 Layout: n/a
[ec2-user@my-redmine ~]$ sudo localectl set-locale LANG=ja_JP.UTF-8
[ec2-user@my-redmine ~]$ sudo localectl set-keymap jp109
[ec2-user@my-redmine ~]$

事前インストール

[ec2-user@my-redmine ~]$ sudo yum install git gcc openssl-devel readline-devel zlib-devel MariaDB-client -y
Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check

(中略)

Complete!
[ec2-user@my-redmine ~]$

MariaDB インストール

[ec2-user@my-redmine ~]$ sudo amazon-linux-extras install -y mariadb10.5
Installing mariadb

(中略)

53  unbound1.13              available    [ =stable ]
54  mariadb10.5=latest       enabled      [ =stable ]
55  kernel-5.10=latest       enabled      [ =stable ]
56  redis6                   available    [ =stable ]
57  ruby3.0                  available    [ =stable ]
58  postgresql12             available    [ =stable ]
59  postgresql13             available    [ =stable ]
60  mock2                    available    [ =stable ]
61  dnsmasq2.85              available    [ =stable ]
[ec2-user@my-redmine ~]$ sudo systemctl enable mariadb
Created symlink from /etc/systemd/system/mysql.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/mysqld.service to /usr/lib/systemd/system/mariadb.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
[ec2-user@my-redmine ~]$ sudo systemctl start mariadb
[ec2-user@my-redmine ~]$

MariaDB のセキュリティ設定

まず、MariaDB の root パスワードを設定します。

[ec2-user@my-redmine ~]$ sudo mysql -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 4
Server version: 10.5.10-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [mysql]> ALTER USER 'root'@'localhost' IDENTIFIED BY "<パスワード>";
Query OK, 0 rows affected (0.001 sec)
MariaDB [mysql]> quit;
Bye
[ec2-user@my-redmine ~]$

その後、mariadb-secure-installation を設定します。

[ec2-user@my-redmine ~]$ mariadb-secure-installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user. If you've just installed MariaDB, and
haven't set the root password yet, you should just press enter here.

Enter current password for root (enter for none):
OK, successfully used password, moving on...

Setting the root password or using the unix_socket ensures that nobody
can log into the MariaDB root user without the proper authorisation.

You already have your root account protected, so you can safely answer 'n'.

Switch to unix_socket authentication [Y/n] n
... skipping.

You already have your root account protected, so you can safely answer 'n'.

Change the root password? [Y/n] n
... skipping.

By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y

 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!
[ec2-user@my-redmine ~]$

Redmine ユーザーを作成

[ec2-user@my-redmine ~]$ sudo useradd redmine
[ec2-user@my-redmine ~]$ sudo passwd redmine
Changing password for user redmine.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[ec2-user@my-redmine ~]$ sudo visudo

100 行目あたりに以下を追加します。

99 ## Allow root to run any commands anywhere
100 root    ALL=(ALL)       ALL
101 redmine    ALL=(ALL)       ALL
102
103 ## Allows members of the 'sys' group to run networking, software,

rbenv をインストール

[ec2-user@my-redmine ~]$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
Cloning into '/home/ec2-user/.rbenv/plugins/ruby-build'...
remote: Enumerating objects: 12199, done.
remote: Counting objects: 100% (892/892), done.
remote: Compressing objects: 100% (307/307), done.
remote: Total 12199 (delta 606), reused 774 (delta 528), pack-reused 11307
Receiving objects: 100% (12199/12199), 2.54 MiB | 3.79 MiB/s, done.
Resolving deltas: 100% (8073/8073), done.
[ec2-user@my-redmine ~]$ cd ~/.rbenv
[ec2-user@my-redmine .rbenv]$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
[ec2-user@my-redmine .rbenv]$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
[ec2-user@my-redmine .rbenv]$ source ~/.bash_profile
[ec2-user@my-redmine .rbenv]$ rbenv install 2.5.8 -v
[ec2-user@my-redmine .rbenv]$ rbenv install 2.5.8 -v
/tmp/ruby-build.20220507160851.3447.AYKeKU ~/.rbenv
Downloading ruby-2.5.8.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.8.tar.bz2
Installing ruby-2.5.8...

5 分程度待機すると終わるので、2.5.8 をアクティブにします。

/tmp/ruby-build.20220507160851.3447.AYKeKU ~/.rbenv
~/.rbenv
[ec2-user@my-redmine .rbenv]$
[ec2-user@my-redmine .rbenv]$ rbenv global 2.5.8
[ec2-user@my-redmine .rbenv]$ ruby -v
ruby 2.5.8p224 (2020-03-31 revision 67882) [x86_64-linux]
[ec2-user@my-redmine .rbenv]$

Redmine をインストール

Ruby をインストールできたら、Redmine をインストールしていきます。
src ディレクトリにソースを展開して、

[ec2-user@my-redmine .rbenv]$ mkdir ~/src
[ec2-user@my-redmine .rbenv]$ cd ~/src/
[ec2-user@my-redmine src]$ wget http://www.redmine.org/releases/redmine-4.2.5.tar.gz
--2022-05-07 16:14:23--  http://www.redmine.org/releases/redmine-4.2.5.tar.gz
Resolving www.redmine.org (www.redmine.org)... 46.4.101.126
Connecting to www.redmine.org (www.redmine.org)|46.4.101.126|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://www.redmine.org/releases/redmine-4.2.5.tar.gz [following]
--2022-05-07 16:14:24--  https://www.redmine.org/releases/redmine-4.2.5.tar.gz
Connecting to www.redmine.org (www.redmine.org)|46.4.101.126|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3041844 (2.9M) [application/x-gzip]
Saving to: ‘redmine-4.2.5.tar.gz’

100%[=================================================================================================>] 3,041,844   1.57MB/s   in 1.8s

2022-05-07 16:14:27 (1.57 MB/s) - ‘redmine-4.2.5.tar.gz’ saved [3041844/3041844]
[ec2-user@my-redmine src]$ tar xzf redmine-4.2.5.tar.gz
[ec2-user@my-redmine src]$

データベースにユーザーとユーザーを作ります。

[ec2-user@my-redmine src]$ mysql -h localhost -u root -p
Enter password:
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 12
Server version: 10.5.10-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> CREATE DATABASE redmine CHARACTER SET utf8mb4;
Query OK, 1 row affected (0.000 sec)

MariaDB [(none)]> CREATE USER 'rdusr'@'%' IDENTIFIED BY '<パスワード>';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> GRANT SELECT, UPDATE, DELETE, INSERT, CREATE, DROP, INDEX, ALTER, LOCK TABLES, EXECUTE, CREATE TEMPORARY TABLES, TRIGGER, CREATE VIEW, SHOW VIEW, EVENT ON redmine.* TO 'rdusr'@'%';
Query OK, 0 rows affected (0.001 sec)

MariaDB [(none)]> exit
Bye
[ec2-user@my-redmine src]$

bundler をインストールして、

[ec2-user@my-redmine src]$ gem install bundler
Fetching: bundler-2.3.13.gem (100%)
Successfully installed bundler-2.3.13
Parsing documentation for bundler-2.3.13
Installing ri documentation for bundler-2.3.13
Done installing documentation for bundler after 0 seconds
1 gem installed
[ec2-user@my-redmine src]$

Redmine をインストールします。

[ec2-user@my-redmine src]$ cd redmine-4.2.5/
[ec2-user@my-redmine redmine-4.2.5]$ bundle install --without development test
Your RubyGems version (2.7.6.2) has a bug that prevents `required_ruby_version` from working for Bundler. Any scripts that use `gem install bundler` will break as soon as Bundler drops support for your Ruby version. Please upgrade RubyGems to avoid future breakage and silence this warning by running `gem update --system 3.2.3`
[DEPRECATED] The `--without` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set --local without 'development test'`, and stop using this flag
Please configure your config/database.yml first

(中略)

Please ensure that your Gemfiles and .gemspecs are suitably restrictive
to avoid an unexpected breakage when 3.0 is released (e.g. ~> 2.3.0).
See https://github.com/rubyzip/rubyzip for details. The Changelog also
lists other enhancements and bugfixes that have been implemented since
version 2.3.0.
[ec2-user@my-redmine redmine-4.2.5]$

こちらも実施。

[ec2-user@my-redmine redmine-4.2.5]$ bundle install --without development test rmagick
Your RubyGems version (2.7.6.2) has a bug that prevents `required_ruby_version` from working for Bundler. Any scripts that use `gem install bundler` will break as soon as Bundler drops support for your Ruby version. Please upgrade RubyGems to avoid future breakage and silence this warning by running `gem update --system 3.2.3`

(中略)

Using rubyzip 2.3.2
Bundle complete! 36 Gemfile dependencies, 62 gems now installed.
Gems in the groups 'development', 'test' and 'rmagick' were not installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
[ec2-user@my-redmine redmine-4.2.5]$

mysql2 は起動時にエラーが出たので入れました。

[ec2-user@my-redmine redmine-4.2.5]$ bundle install --without mysql2
Your RubyGems version (2.7.6.2) has a bug that prevents `required_ruby_version` from working for Bundler. Any scripts that use `gem install bundler` will break as soon as Bundler drops support for your Ruby version. Please upgrade RubyGems to avoid future breakage and silence this warning by running `gem update --system 3.2.3`
[DEPRECATED] The `--without` flag is deprecated because it relies on being remembered across bundler invocations, which bundler will no longer do in future versions. Instead please use `bundle config set --local without 'mysql2'`, and stop using this flag
Fetching gem metadata from https://rubygems.org/.........

(中略)

Using rails 5.2.6.3Bundle complete! 37 Gemfile dependencies, 87 gems now installed.
Gems in the group 'mysql2' were not installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
[ec2-user@my-redmine redmine-4.2.5]$

セッションハイジャックを防ぐために、セッション情報を格納するcookieをエンコードするための鍵を生成

[ec2-user@my-redmine redmine-4.2.5]$ bundle exec rake generate_secret_token
Your RubyGems version (2.7.6.2) has a bug that prevents `required_ruby_version` from working for Bundler. Any scripts that use `gem install bundler` will break as soon as Bundler drops support for your Ruby version. Please upgrade RubyGems to avoid future breakage and silence this warning by running `gem update --system 3.2.3`
[ec2-user@my-redmine redmine-4.2.5]$

database.yml を作成します。

[ec2-user@my-redmine redmine-4.2.5]$
[ec2-user@my-redmine redmine-4.2.5]$ cp config/database.yml.example config/database.yml
[ec2-user@my-redmine redmine-4.2.5]$

production 部分にパスワードを設定します。

[ec2-user@my-redmine redmine-4.2.5]$ vi config/database.yml

production:
  adapter: mysql2
  database: redmine
  host: localhost
  username: root
  password: "<データベースのパスワード>"

テーブル作成

テーブルを作成するクエリーが大量に流れます。

[ec2-user@my-redmine redmine-4.2.5]$ RAILS_ENV=production bundle exec rake db:migrate
Your RubyGems version (2.7.6.2) has a bug that prevents `required_ruby_version` from working for Bundler. Any scripts that use `gem install bundler` will break as soon as Bundler drops support for your Ruby version. Please upgrade RubyGems to avoid future breakage and silence this warning by running `gem update --system 3.2.3`
== 1 Setup: migrating =========================================================
-- create_table("attachments", {:options=>"ENGINE=InnoDB", :force=>true, :id=>:integer})

(中略)

   -> 0.0037s
== 20200826153402 AddTotpToUser: migrated (0.0077s) ===========================

[ec2-user@my-redmine redmine-4.2.5]$

デフォルトデータをデータベースに登録します。ja を指定します。

[ec2-user@my-redmine redmine-4.2.5]$ RAILS_ENV=production bundle exec rake redmine:load_default_data
Your RubyGems version (2.7.6.2) has a bug that prevents `required_ruby_version` from working for Bundler. Any scripts that use `gem install bundler` will break as soon as Bundler drops support for your Ruby version. Please upgrade RubyGems to avoid future breakage and silence this warning by running `gem update --system 3.2.3`
Select language: ar, az, bg, bs, ca, cs, da, de, el, en, en-GB, es, es-PA, et, eu, fa, fi, fr, gl, he, hr, hu, id, it, ja, ko, lt, lv, mk,mn, nl, no, pl, pt, pt-BR, ro, ru, sk, sl, sq, sr, sr-YU, sv, th, tr, uk, vi, zh, zh-TW [en] ja
Default configuration data loaded.
[ec2-user@my-redmine redmine-4.2.5]$

権限設定

[ec2-user@my-redmine redmine-4.2.5]$ sudo chown -R redmine:redmine files log tmp public/plugin_assets
[ec2-user@my-redmine redmine-4.2.5]$ sudo chmod -R 755 files log tmp public/plugin_assets

ソースコードを DocumentRoot に移動

[ec2-user@my-redmine redmine-4.2.5]$ sudo mv ./* /var/www/html/
[ec2-user@my-redmine redmine-4.2.5]$ cd /var/www/html

Passenger をインストール

[ec2-user@my-redmine html]$ gem install passenger
Fetching: passenger-6.0.13.gem (100%)
Building native extensions. This could take a while...
Successfully installed passenger-6.0.13
Parsing documentation for passenger-6.0.13
Installing ri documentation for passenger-6.0.13
Done installing documentation for passenger after 73 seconds
1 gem installed
[ec2-user@my-redmine html]$

権限エラーが出たので、以下を作成。

[ec2-user@my-redmine html]$ sudo mkdir /var/www/html/tmp/pids
[ec2-user@my-redmine html]$ sudo chmod 777 /var/www/html/tmp/pids

Redmine を起動

準備が整いました。以下コマンドを実行すると、

[ec2-user@my-redmine html]$ bundle exec rails server webrick -e production
Your RubyGems version (2.7.6.2) has a bug that prevents `required_ruby_version` from working for Bundler. Any scripts that use `gem install bundler` will break as soon as Bundler drops support for your Ruby version. Please upgrade RubyGems to avoid future breakage and silence this warning by running `gem update --system 3.2.3`
=> Booting WEBrick
=> Rails 5.2.6.3 application starting in production on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
Rails Error: Unable to access log file. Please ensure that /var/www/html/log/production.log exists and is writable (ie, make it writable for user and group: chmod 0664 /var/www/html/log/production.log). The log level has been raised to WARN and the output directed to STDERR until the problem is fixed.
Creating scope :system. Overwriting existing method Enumeration.system.
Creating scope :sorted. Overwriting existing method Group.sorted.
Creating scope :sorted. Overwriting existing method User.sorted.
[2022-05-07 16:48:41] INFO  WEBrick 1.7.0
[2022-05-07 16:48:41] INFO  ruby 2.5.8 (2020-03-31) [x86_64-linux]
[2022-05-07 16:48:41] INFO  WEBrick::HTTPServer#start: pid=20205 port=3000
Started GET "/" for 182.169.12.70 at 2022-05-07 16:49:26 +0900
Processing by WelcomeController#index as HTML

トップページが表示されました。

また、時間が取れるときに Ubuntu 版のインストールを実施します。うまく起動できたら投稿したいと思います!

この記事が気に入ったらサポートをしてみませんか?