プログラマーのメモ書き

伊勢在住のプログラマーが気になることを気ままにメモったブログです

Redmine のアップグレード 4.1.1 -> 6.0.5 (その2)

ここからは自分独自の環境の話なので、 Redmine の移行そのものにはあまり関係ないのですが、自分用のメモなのでまとめて載せています。

Redmine のアップグレード自体はできたので、サーバーに関するその他の設定を行っていきます。また、旧サーバーには redmine のバックアップを定期的にとるためなど、いくつかの処理を自前で追加しています。このあたりの設定も移行しておきます。

サーバーの設定

毎回やりますが、微妙に違っているので一応書いておきます。

システムアップデート

bitnami@ip-172-30-3-238:~$ sudo apt update
bitnami@ip-172-30-3-238:~$ sudo apt upgrade
bitnami@ip-172-30-3-238:~$ sudo apt full-upgrade
bitnami@ip-172-30-3-238:~$ sudo apt autoremove

一度、再起動しておきます。

bitnami@ip-172-30-3-238:~$ sudo reboot

タイムゾーン

Debian 12 Bookworm : システムのタイムゾーンを設定する : Server World

現在の設定を確認します。

bitnami@ip-172-30-3-238:~$ timedatectl
               Local time: Mon 2025-07-07 05:46:52 UTC
           Universal time: Mon 2025-07-07 05:46:52 UTC
                 RTC time: Mon 2025-07-07 05:46:51
                Time zone: Etc/UTC (UTC, +0000)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
bitnami@ip-172-30-3-238:~$ 

タイムゾーンを UTC から Asia/Tokyo に切り替えます。

bitnami@ip-172-30-3-238:~$ sudo timedatectl set-timezone Asia/Tokyo
bitnami@ip-172-30-3-238:~$ timedatectl
               Local time: Mon 2025-07-07 14:47:32 JST
           Universal time: Mon 2025-07-07 05:47:32 UTC
                 RTC time: Mon 2025-07-07 05:47:32
                Time zone: Asia/Tokyo (JST, +0900)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
bitnami@ip-172-30-3-238:~$ 

ロケールの設定と日本語環境

Debian 12 Bookworm : システムの文字コードを設定する : Server World

初期設定を確認します。

bitnami@ip-172-30-3-238:~$ localectl status
System Locale: LANG=en_US.UTF-8
    VC Keymap: (unset)         
   X11 Layout: (unset)         
bitnami@ip-172-30-3-238:~$ 

必要なものをンストールします。

bitnami@ip-172-30-3-238:~$ sudo apt install task-japanese locales-all

ja-JP.UTF-8 に切り替えます。

bitnami@ip-172-30-3-238:~$ sudo localectl set-locale LANG=ja_JP.UTF-8
bitnami@ip-172-30-3-238:~$ 
bitnami@ip-172-30-3-238:~$ localectl
System Locale: LANG=ja_JP.UTF-8
    VC Keymap: (unset)         
   X11 Layout: (unset)         
bitnami@ip-172-30-3-238:~$ 

task-japanese パッケージをインストールすれば、一通りの日本語環境で必要なものもインストールしてくれるようです。

SSH 設定の変更

SSH で接続する際のポート番号を変更しておきます。

bitnami@ip-172-30-3-238:~$ cd /etc/ssh/
bitnami@ip-172-30-3-238:/etc/ssh$ sudo cp -p sshd_config sshd_config.org
bitnami@ip-172-30-3-238:/etc/ssh$ sudo vi sshd_config
bitnami@ip-172-30-3-238:/etc/ssh$ diff sshd_config.org sshd_config
14a15
> Port 新ポート番号
bitnami@ip-172-30-3-238:/etc/ssh$ 

SSHD を再起動します。

bitnami@ip-172-30-3-238:/etc/ssh$ sudo systemctl restart sshd

一応確認しておきます。

bitnami@ip-172-30-3-238:/etc/ssh$ sudo systemctl status sshd
● ssh.service - OpenBSD Secure Shell server
     Loaded: loaded (/lib/systemd/system/ssh.service; enabled; preset: enabled)
     Active: active (running) since Mon 2025-07-07 14:54:48 JST; 4s ago
       Docs: man:sshd(8)
             man:sshd_config(5)
    Process: 2444 ExecStartPre=/usr/sbin/sshd -t (code=exited, status=0/SUCCESS)
   Main PID: 2445 (sshd)
      Tasks: 1 (limit: 510)
     Memory: 1.4M
        CPU: 22ms
     CGroup: /system.slice/ssh.service
             └─2445 "sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups"

Jul 07 14:54:48 ip-172-30-3-238 systemd[1]: Starting ssh.service - OpenBSD Secure Shell server...
Jul 07 14:54:48 ip-172-30-3-238 sshd[2445]: Server listening on 0.0.0.0 port 新ポート番号.
Jul 07 14:54:48 ip-172-30-3-238 sshd[2445]: Server listening on :: port 新ポート番号.
Jul 07 14:54:48 ip-172-30-3-238 systemd[1]: Started ssh.service - OpenBSD Secure Shell server.
bitnami@ip-172-30-3-238:/etc/ssh$ 

大丈夫そうですね。実際に接続しても確認します。このとき EC2 のセキュリティグループで、変更したポート番号を通すようにインバウンドのルールを追加・編集しておきます。接続確認ができれば OK です。

ポート番号の変更が完了したら、セキュリティグループで通していた 22 番ポートを閉じておきます。

NTP クライアント

Debian 12 Bookworm : NTP クライアントの設定 : Server World

systemd-timesyncd が動いていますね。

bitnami@ip-172-30-3-238:/etc/systemd$ sudo systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; preset: enabled)
     Active: active (running) since Mon 2025-07-07 14:45:56 JST; 32min ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 368 (systemd-timesyn)
     Status: "Contacted time server 169.254.169.123:123 (169.254.169.123)."
      Tasks: 2 (limit: 510)
     Memory: 1.2M
        CPU: 47ms
     CGroup: /system.slice/systemd-timesyncd.service
             └─368 /lib/systemd/systemd-timesyncd

 707 14:45:56 ip-172-30-3-238 systemd[1]: Starting systemd-timesyncd.service - Network Time Synchronization...
 707 14:45:56 ip-172-30-3-238 systemd[1]: Started systemd-timesyncd.service - Network Time Synchronization.
 707 14:45:56 ip-172-30-3-238 systemd-timesyncd[368]: Network configuration changed, trying to establish connection.
 707 14:45:56 ip-172-30-3-238 systemd-timesyncd[368]: Network configuration changed, trying to establish connection.
 707 14:45:57 ip-172-30-3-238 systemd-timesyncd[368]: Network configuration changed, trying to establish connection.
 707 14:45:57 ip-172-30-3-238 systemd-timesyncd[368]: Network configuration changed, trying to establish connection.
 707 14:45:57 ip-172-30-3-238 systemd-timesyncd[368]: Contacted time server 169.254.169.123:123 (169.254.169.123).
 707 14:45:57 ip-172-30-3-238 systemd-timesyncd[368]: Initial clock synchronization to Mon 2025-07-07 05:45:57.549545 UTC.
 707 14:50:44 ip-172-30-3-238 systemd-timesyncd[368]: Contacted time server 169.254.169.123:123 (169.254.169.123).
bitnami@ip-172-30-3-238:/etc/systemd$ 

なので、このままでもいいのですが、一応、 NTP サーバーだけ国内のものを指すようにしておきます。 /etc/systemd/timesyncd.conf を編集して

[Time]
#NTP=
NTP=ntp.nict.jp
#FallbackNTP=0.debian.pool.ntp.org 1.debian.pool.ntp.org 2.debian.pool.ntp.org 3.debian.pool.ntp.org

のように NTP の行を追加します。timesyncd を再起動します。

bitnami@ip-172-30-3-238:/etc/systemd$ sudo systemctl restart systemd-timesyncd

NTP サーバーが変わっていることを確認します。

bitnami@ip-172-30-3-238:/etc/systemd$ timedatectl timesync-status
       Server: 133.243.238.163 (ntp.nict.jp)
Poll interval: 1min 4s (min: 32s; max 34min 8s)
         Leap: normal
      Version: 4
      Stratum: 1
    Reference: NICT
    Precision: 1us (-20)
Root distance: 0 (max: 5s)
       Offset: +1.656ms
        Delay: 2.429ms
       Jitter: 0
 Packet count: 1
    Frequency: +13.281ppm
bitnami@ip-172-30-3-238:/etc/systemd$ 

unattended-upgrades

この Bitnami Redmine スタックにも最初から unattended-upgrades が入っていました。なので、設定変更だけ行います。

いまは、 unattended-upgrades の設定ファイル /etc/apt/apt.conf.d/50unattended-upgrads を直接編集するのではなく、ローカル設定を別ファイルに書いておくことが推奨されているとのことです。

これに従って、 52unattended-upgrades-local を作ってみました。

bitnami@ip-172-30-3-238:/etc/apt/apt.conf.d$ cat 52unattended-upgrades-local 

// local settings for unattended-upgrades
//   created by Junichi MORI, 2025/7/7

// Send email to this address for problems or packages upgrades
// If empty or unset then no email is sent, make sure that you
// have a working mail setup on your system. A package that provides
// 'mailx' must be installed. E.g. "user@example.com"
Unattended-Upgrade::Mail "通知先のメールアドレス";

// Set this value to one of:
//    "always", "only-on-error" or "on-change"
// If this is not set, then any legacy MailOnlyOnError (boolean) value
// is used to chose between "only-on-error" and "on-change"
Unattended-Upgrade::MailReport "always";

// Do automatic removal of unused packages after the upgrade
// (equivalent to apt-get autoremove)
Unattended-Upgrade::Remove-Unused-Dependencies "true";

// Automatically reboot *WITHOUT CONFIRMATION* if
//  the file /var/run/reboot-required is found after the upgrade
Unattended-Upgrade::Automatic-Reboot "true";

// If automatic reboot is enabled and needed, reboot at the specific
// time instead of immediately
//  Default: "now"
Unattended-Upgrade::Automatic-Reboot-Time "04:00";
bitnami@ip-172-30-3-238:/etc/apt/apt.conf.d$ 

Remove-Unused-Dependencies を true にしておくことで、 apt autoremove と似たような動作になります。

あと、以前のアップグレードの時の記事でも参考にした下記記事より、

Debian 系で unattended-upgrades を有効にする場合の追加設定 (メール通知, autoremove, autoclean, 再起動) #Ubuntu - Qiita

20auto-upgrades を編集して、

bitnami@ip-172-30-3-238:/etc/apt/apt.conf.d$ cat 20auto-upgrades 
APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";
APT::Periodic::AutocleanInterval "6";
bitnami@ip-172-30-3-238:/etc/apt/apt.conf.d$ 

APT::Periodic::AutocleanInterval を追加しておきます。こうすることで apt autoclean と同等の処理を定期的に行うようにします。

メール送信

メールは Amazon SES 経由で送るようにします。こちらも以前のアップグレード時の記事とおなじですが、msmtpを入れておきます。

bitnami@ip-172-30-3-238:/etc$ sudo apt install msmtp msmtp-mta
bitnami@ip-172-30-3-238:/etc$ sudo cp -p /usr/share/doc/msmtp/examples/msmtprc-user.example msmtprc

msmtp のインストール時に AppArmor を有効にするか聞かれましたが、デフォルトの『いいえ』を選びました。msmtprc の内容は下記のような感じです。

bitnami@ip-172-30-3-238:/etc$ cat msmtprc
# Set default values: use the mail submission port 587, and always use TLS.
# On this port, TLS is activated via STARTTLS.
defaults
port 587
tls on
tls_starttls on

# Define a mail account via amazon SES
account sakura_via_ses
host SES のSMTPサーバー名
from (メールアドレス)
# Authentication
auth on
user (SES のアカウント名)

# You can also store the password directly in this file or have msmtp ask you
# for it each time you send a mail, but one of the above methods is preferred.
password (SES のパスワード)

# Set a default account
account default : sakura_via_ses
bitnami@ip-172-30-3-238:/etc$ 

テストします。送信先アドレスに無事にメールが届いていればOKですね。

bitnami@ip-172-30-3-238:~$ echo "mail test" | sudo msmtp -a default メールアドレス

unattended-upgrades の 50unattended-upgrads を見ると mailx が必要と書いてあります。ということで、 mailx を使うために、以下も入れておきます。

bitnami@ip-172-30-3-238:/etc$ sudo apt install bsd-mailx

これで OK ですね。

独自の追加処理

サーバーの基本的な設定ができたら、あとは独自で入れてる追加の処理(定期的に実行するもの)をコピーしておきます。処理としては、

  • redmine のバックアップ取得(DBおよび添付ファイル)と S3 への保存
  • EBS のスナップショットの自動作成
  • Security Group の IP アドレス更新

になります。スクリプト本体と cron の起動用ファイルをコピーして、各種バイナリの場所や EBS のボリューム ID などを調整すれば OK です。あと aws の認証情報もコピーしておきます。

ちなみに、 cron.daily などへ登録して実行する場合、このディレクトリにあるスクリプトの呼び出しが /etc/crontab から行われるのですが、この内容が、

bitnami@ip-172-30-3-238:/etc$ cat crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.daily; }
47 6    * * 7   root    test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.weekly; }
52 6    1 * *   root    test -x /usr/sbin/anacron || { cd / && run-parts --report /etc/cron.monthly; }
#
27 * * * * bitnami cd /opt/bitnami/stats && ./agent.bin --run -D
bitnami@ip-172-30-3-238:/etc$ 

となったおり、 run-parts 経由で実行されることになります。このとき run-parts の制限として、ファイル名に拡張子を含めないようにする必要があるのでご注意ください(忘れてて、ちょっとだけはまりました)。

また、以前は、

などもやっていたのですが、前者は不要となったので廃止、後者は正確ないきさつは覚えてないのですが前回のアップグレード(4.1.1に上げたとき)に利用していたツールが使えなくなったか何だかそのような経緯でやめたような気がします。

サーバーの切り替え

さて、ここまでできたら、やっとサーバーの切り替えができます。

Elastic IP の付け替え

旧サーバーについている Elastic IP を新サーバーのほうへ付け替えます。これは、 EC2 のコンソールから操作すればすぐに完了です。

Let's Encrypt を設定

基本的には、下記のアップグレードの時の作業と同じです。

Redmine のアップデート 3.3.2 -> 4.1.1 - プログラマーのメモ書き

前の記事でも書いていますが、もともとのセキュリティグループでは、 https でのアクセスは仕事場などの限られたアドレスからに制限を行っています。このため、セットアップ時は、 https でのアクセスに制限を行わないようにしておきます。

次に、設定完了後、セキュリティグループの https の IP アドレス制限を行います。

これで問題ないことをテストします。 crontab -l で表示される証明書の更新のコマンドを表示させます。

bitnami@ip-172-30-3-238:/etc/cron.d$ crontab -l

5 16 * * * sudo /opt/bitnami/letsencrypt/lego --path /opt/bitnami/letsencrypt --email="メールアドレス" --http --http-timeout 30 --http.webroot /opt/bitnami/apps/letsencrypt --domains=ドメイン名 --user-agent bitnami-bncert/2.0.1 renew && sudo /opt/bitnami/apache/bin/httpd -f /opt/bitnami/apache/conf/httpd.conf -k graceful # bncert-autorenew
bitnami@ip-172-30-3-238:/etc/cron.d$ 

これをコマンドラインから呼んでみると、

bitnami@ip-172-30-3-238:/etc/cron.d$ sudo /opt/bitnami/letsencrypt/lego --path /opt/bitnami/letsencrypt --email="メールアドレス" --http --http-timeout 30 --http.webroot /opt/bitnami/apps/letsencrypt --domains=ドメイン名 --user-agent bitnami-bncert/2.0.1 renew
2025/07/07 19:25:06 [ドメイン名] The certificate expires in 89 days, the number of days defined to perform the renewal is 30: no renewal.
bitnami@ip-172-30-3-238:/etc/cron.d$ 

のように問題なく、通信が行えていることが確認できました。

旧サーバーの停止と削除

これで移行作業が一通り完了したはずなので、この時点で旧サーバーのインスタンスを停止しておきます。問題なく redmine 使えますね。

あとは、数日様子を見て、新サーバーのログにエラー等が無いことが確認できたら、旧サーバーを削除すれば完了です。

まとめ

Redmine 自体の移行よりもそのあとのサーバー設定のほうが時間かかってしまいましたね。このあたり、もうちょっとなんとかしたいものですね。