プロジェクト

全般

プロフィール

RedmineをRocky Linux 9上で動かすーUnicornとNginx編

RedmineをRocky Linux 9上で動かすためのセットアップメモです。

動作環境

Windows 11のHyper-V上で動作する Rocky Linux 9 で、vCPU 2個、4096MBメモリのリソースを割り当てています。

事前準備

Redmineの動作に必要なOSパッケージのインストール

開発用パッケージ

標準のパッケージリポジトリでは提供されていないパッケージがいくつか必要になります。

  • libyaml-devel
  • ImageMagick関連

1つ目は、CodeReady Linux Builder (crb) リポジトリをアクセスできるように設定し、ここからインストールします。
crb レポジトリは、/etc/yum.repos.d/rocky.repo に定義が記載されています。デフォルトでは無効なので使用するときは、--enablerepo=crb オプションを指定します。
2つ目は、Extra Packages for Enterprise Linux(EPEL)リポジトリをアクセスできるように設定し、ここからインストールします。
epelリポジトリ定義をインストールする設定を提供するパッケージをインストールします。

開発ツール(Development Tools)グループに定義されるパッケージをインストール
~$ sudo dnf groupinstall "Development Tools" 
  : (多数のパッケージがインストールされる)
開発ツール一式に含まれない開発パッケージをインストール(その1)
~$ sudo dnf install openssl-devel readline-devel zlib-devel curl-devel
  :
開発ツール一式に含まれない開発パッケージをインストール(その2)
~$ sudo dnf install --enablerepo=crb libyaml-devel
  :
開発ツール一式に含まれない開発パッケージをインストール(その3)
~$ sudo dnf install epel-release
  :

次のリポジトリ定義ファイルがインストールされます。

  • /etc/yum.repos.d/
    • epel.repo
    • epel-testing.repo
    • epel-cisco-openh264.repo

epel.repoには、enabled=1 と記述されているので、オプションなしにepelリポジトリからパッケージのインストールが可能となります。

~$ sudo dnf install ImageMagick-devel

ImageMagick-develを指定すると、依存関係である ImageMagickほかのパッケージが併せてインストールされます。

MariaDB

~$ sudo dnf install mariadb-server
  :
~$ sudo dnf install --enablerepo=crb mariadb-devel
  :
  • mariadb-develパッケージは、CRBリポジトリから提供されるので、--enablerepo=crbオプションを指定
/etc/my.cnf.d/mariadb-server.cnf
 [mysqld]
 datadir=/var/lib/mysql
 socket=/var/lib/mysql/mysql.sock
 log-error=/var/log/mariadb/mariadb.log
 pid-file=/run/mariadb/mariadb.pid

+character_set_server = utf8mb4

+innodb_buffer_pool_size = 256M
+innodb_log_file_size = 64M
+innodb_flush_method = O_DIRECT
/etc/my.cnf.d/mysql-clients.cnf
 [mysql]
+default_character_set = utf8mb4
+show-warnings
MariaDBの自動起動設定と起動
~$ sudo systemctl enable --now mariadb
  :
MariaDBへの接続
~$ sudo mysql
MariaDB [(none)]> SELECT user,host,password FROM mysql.user;
+-------------+-----------+----------+
| User        | Host      | Password |
+-------------+-----------+----------+
| mariadb.sys | localhost |          |
| root        | localhost | invalid  |
| mysql       | localhost | invalid  |
+-------------+-----------+----------+

sudo なしに一般ユーザーで、mysql -uroot とアクセスすると、ERROR 1698 (28000): Access denied for user 'root''localhost'@ とエラーになります。

  • 最初にunix_socket認証が適用されるため、ローカルマシン上のroot権限を持つアカウントで(sudo含め)オプションのない mysql コマンドを実行すると、パスワードなしにアクセスできます。
  • unix_socket認証が失敗すると、パスワード認証をしようとするが、Password設定がinvalidのためエラーとなります。

Ruby

Rocky Linux 9では、次のRubyバージョンが提供されます。

~$ dnf module list ruby
Rocky Linux 9 - AppStream
Name   Stream   Profiles     Summary
ruby   3.1      common [d]   An interpreter of object-oriented scripting language
ruby   3.3      common [d]   An interpreter of object-oriented scripting language

Redmine 6.0.0では、Ruby 3.1/3.2/3.3 に対応しているので、ここでは 3.3をインストールします。

~$ sudo dnf module enable ruby:3.3
 :
~$ dnf module list ruby
Rocky Linux 9 - AppStream
Name   Stream    Profiles    Summary
ruby   3.1       common [d]  An interpreter of object-oriented scripting language
ruby   3.3 [e]   common [d]  An interpreter of object-oriented scripting language
 :
~$ sudo dnf module install ruby
 :
~$ sudo dnf install ruby-devel 
 :

Redmineのインストール

OSにredmineアカウント作成

redmineの実行アカウントを作成します。

~$ sudo useradd redmine
~$ sudo passwd redmine
Changing password for user redmine.
New password: ********
Retype new password: ********
passwd: all authentication tokens updated successfully.

Redmineのコードを展開

/var/libの下に、Redmineのgit cloneを展開します。

~$ cd /var/lib
lib$ sudo git clone -b 6.0-stable https://github.com/redmine/redmine.git redmine-6.0-stable
lib$ sudo chown -R redmine.redmine redmine-6.0-stable

データベース設定ファイル

redmineアカウントでデータベース設定ファイル(/var/lib/redmine-6.0-stable/config/database.yml)を記述します。database.yml.sampleをコピーし、passwordの定義を記述します。

production:
  adapter: mysql2
  database: redmine
  host: localhost
  username: root
  password: "xxxxxxxx" 
  encoding: utf8mb4
  variables:
    tx_isolation: "READ-COMMITTED" 
  • データベース接続のパスワードを記載したファイルなので、パーミッションを厳しく chmod 600 database.yml とします。
variablesの記述について

redmine 6.0のリリースに含まれる config/database.yml.sample には、コメントで次の記述があります。

    # Recommended `transaction_isolation` for MySQL to avoid concurrency issues is
    # `READ-COMMITTED`.
    # In case of MySQL lower than 8, the variable name is `tx_isolation`.
    # See https://www.redmine.org/projects/redmine/wiki/MySQL_configuration

今回、Rocky Linux 9のリポジトリからインストールした MariaDB のバージョンは、10.5.27です。
次のドキュメントによると、MySQL 8.0のtransaction_isolation変数は、MariaDBではtx_isolationであり、MariaDB 11.1以降で transaction_isolationに置き換わるとのことです。
System Variable Differences Between MariaDB 10.5 and MySQL 8.0

メール接続設定ファイル

redmineアカウントでメール接続設定ファイル(/var/lib/redmine-6.0-stable/config/configuration.yml)を記述します。configuration.yml.sampleをコピーします。

default:
  email_delivery:
    delivery_method: :smtp
    smtp_settings:
      address: "localhost" 
      port: 25

rubyモジュール(gems)のインストール

Redmine本体が使用するRubyモジュールをbundlerを使ってインストールします。

redmine-6.0-stable$ bundle config set --local without development test
redmine-6.0-stable$ bundle install
  :

bundle config set で指定した設定は、プロジェクトディレクトリの.bundle/configに記載されます。

初期設定

MariaDBにRedmineアカウント・データベース作成

~$ sudo mysql
MariaDB [(none)]> CREATE DATABASE redmine;
MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| redmine            |
+--------------------+
MariaDB [(none)]> GRANT ALL ON redmine.* TO 'redmine'@'localhost' IDENTIFIED BY 'xxxxxxxx' WITH GRANT OPTION;

パスワードは、先に database.yml に記載のものと一致させます。

セッション格納秘密キーの生成

redmine-6.0-stable$ bundle exec rails generate_secret_token
/usr/share/ruby/json/common.rb:3: warning: ostruct was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.5.0.
You can add ostruct to your Gemfile or gemspec to silence this warning.

警告が出ていますが、ruby 3.5以降での話なので今回はこのままとします。

データベースにスキーマ生成

$ RAILS_ENV=production bundle exec rails db:migrate
  :

database.ymlの記述で、variableにtransaction_isolationを使用すると次のエラーとなります。

/usr/share/ruby/json/common.rb:3: warning: ostruct was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.5.0.
You can add ostruct to your Gemfile or gemspec to silence this warning.
bin/rails aborted!
ActiveRecord::StatementInvalid: Mysql2::Error: Unknown system variable 'transaction_isolation' (ActiveRecord::StatementInvalid)

Caused by:
Mysql2::Error: Unknown system variable 'transaction_isolation' (Mysql2::Error)

Tasks: TOP => db:migrate
(See full trace by running task with --trace)

tmp/pdfディレクトリ作成

公式サイトのインストールページに記載があったので実施します。
https://www.redmine.org/projects/redmine/wiki/RedmineInstall の Step 8

redmine-6.0-stable$ mkdir tmp/pdf

アプリケーションサーバー Unicorn の設定

Unicornの追加インストール

Redmineルートディレクトリに Gemfile.local ファイルを作成します。

gem "unicorn" 

unicornを追加でインストールしたいので、bundle install を実行します。

redmine-6.0-stable$ bundle install
Fetching gem metadata from https://rubygems.org/.........
Resolving dependencies...
Fetching kgio 2.11.4
Fetching raindrops 0.20.1
Installing kgio 2.11.4 with native extensions
Installing raindrops 0.20.1 with native extensions
Fetching unicorn 6.1.0
Installing unicorn 6.1.0 with native extensions
Bundle complete! 45 Gemfile dependencies, 90 gems now installed.
Gems in the groups 'development' and 'test' were not installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
  • kgioとraindropsは、unicornが依存するgem

一時的な動作確認

firewalldで一時的にポート3000を解放し、Unicornをポート3000で起動し、Redmineのアプリケーション画面が表示されることを確認します。
(簡単な確認であれば、curlでローカルホストのコンソール上で http://localhost:3000 でRedmineのHTMLが返却されるかを見る方法もあります)

  • もともとポートが開かれている80をUnicornで使用するには、Unicornを root権限で動かす必要があり、環境設定が変わるので、一時的な確認手段としては除外
~$ sudo firewall-cmd --add-port=3000/tcp
success

Unicornをコンソールから実行します。

redmine-6.0-stable$ bundle exec unicorn_rails -l 3000 -E production
I, [2025-03-31T13:21:38.987524 #29057]  INFO -- : listening on addr=0.0.0.0:3000 fd=7
I, [2025-03-31T13:21:38.987597 #29057]  INFO -- : worker=0 spawning...
I, [2025-03-31T13:21:38.988238 #29057]  INFO -- : master process ready
I, [2025-03-31T13:21:38.988364 #29061]  INFO -- : worker=0 spawned pid=29061
I, [2025-03-31T13:21:38.988461 #29061]  INFO -- : Refreshing Gem list
/usr/share/ruby/json/common.rb:3: warning: ostruct was loaded from the standard library, but will no longer be part of the default gems starting from Ruby 3.5.0.
You can add ostruct to your Gemfile or gemspec to silence this warning.
I, [2025-03-31T13:21:41.015973 #29061]  INFO -- : worker=0 ready

ブラウザから、ポート3000にアクセスし、Redmine画面が表示されることを確認します。

参考)コマンドラインオプションは次です。

    --listen -l [アドレス:]ポート
        ソケットのエンドポイントを指定 
    --config-file -c ファイル
        設定ファイルを指定 
    -D
        デーモンプロセス起動指定 
    -E <RAILS_ENV>
        production等を指定 

unicorn設定

Unicornは、マスタープロセス1つにワーカープロセスが複数という構成で実行します。
ワーカープロセス数、公開するポート、プレロード有無、ログファイルの場所、ワーカープロセスのフォーク前後の挙動などを設定ファイルに定義します。

T.B.D.

調査メモ

OSパッケージ 開発ツールの代替

  • 必要なもの
    git gcc

デフォルトのアプリケーションサーバー

Redmine 6.0では、アプリケーションサーバー(webrick)が除外されているので、何らかのアプリケーションサーバーのインストールが必要になります。

bundle install と bundle update

bundle installは、Gemfile.lockファイルの有無で次の動作をします。
  • Gemfile.lockが存在しなければ、GemfileおよびGemfile.localの定義を参照してgemをインストールし、インストールしたgemとそのバージョンをGemfile.lockファイルに生成します。
  • Gemfile.lockが存在すれば、GemfileおよびGemfile.localに記載のgemで Gemfile.lockに記載があるgemについては、そのgemが未インストールであればそれをインストールします。Gemfile.lockに記載のないgemについてはインストールして、そのgemとバージョンをGemfile.lockに追記します。

bundle updateは、Gemfile.lockを無視して、GemfileおよびGemfile.localの定義を参照してgemをインストールし、インストールしたgemとそのバージョンを新たにGemfile.lockファイルに生成します。

なので、運用環境でbundle updateをうかつに実施してしまうと、GemfileおよびGemfile.localにバージョン固定記述がなければ最新のgemに置き換えてしまうので動作が不安定になる可能性があります。


3ヶ月前に更新