torutkのブログ

ソフトウェア・エンジニアのブログ

さくらVPSのOS更新

さくらVPSのOSをCentOS 6からCentOS 7へ更新

さくらVPSのサーバーは利用開始時から長らくCentOS 6でしたが、このGWで時間が取れたのでやっとCentOS 7へ更新してみました。

OSの再インストールをするので、データのバックアップ、設定のバックアップを取ってから作業に着手します。Let's EncryptのSSL証明書(無料)を利用していたので、再インストール時には、スタートアップスクリプトとして用意されているLetsEncryptを追加しています。

とくに問題なくCentOS 7になりました。IPv6が無効なので有効にして、root以外の作業用アカウントを作成し、SSHポートを標準から変更し、rootのsshログインを無効にしました。

Redmineの稼働にむけて

Ruby 2.4のインストール

RedmineはOS更新前はバージョン3.4系で稼働していましたが、そろそろ4.0系も動かしたいところです。Redmine 4.0は、Ruby on Rails 5.2を用いているため、rubyのバージョンは2.2.2以降となります。しかし、CentOS 7の標準rubyは2.0のため、rubyを別途更新する必要があります。

rubyenvは開発者個人の環境で使うのはまだしも本番環境で使うには気持ち悪いので、以前は自前でRPMビルドしていました。

今回は手元にCentOS 7環境がないので、他の手段を探してみたところ、Red Hat Software Collections というyumリポジトリRed Hatから提供されており、これを利用するのがよさそうです。

$ sudo yum install centos-release-scl-rh 

rubyについては、rh-ruby22、rh-ruby23、rh-ruby24、rh-ruby25とrubyの新しバージョンが用意されています。今回は、Redmine 3.4が対応する最新のrh-ruby24を入れます。

$ sudo yum install rh-ruby24

Red Hat Software Collectionsパッケージは、/opt/rh以下にインストールされます。結構パスが深いのですが、パスを通すenableというファイルが提供されているので、これを/etc/profile.d下にリンクします。

$ sudo ln -s /opt/rh/rh-ruby24/enable /etc/profile.d/rh-ruby24.sh
ruby develのインストール
$ sudo yum --enablerepo=centos-sclo-rh install rh-ruby24-ruby-devel

rubygem bundlerのインストール
$ sudo yum --enablerepo=centos-sclo-rh install rh-ruby24-rubygem-bundler
rubygem rakeのインストール
$ sudo yum --enablerepo=centos-sclo-rh install rh-ruby24-rubygem-rake
  • メモ)rakeは事前インストール不要

Development Toolsのインストール

Redmineインストール手順では、yum groupinstallでDevelopment Toolsをインストールしますが、これが次の結果となりました。

$ sudo yum groupinstall "Development Tools"
Loaded plugins: fastestmirror, langpacks
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: ftp.iij.ad.jp
 * epel: ftp.iij.ad.jp
 * extras: ftp.iij.ad.jp
 * updates: ftp.iij.ad.jp
Warning: Group development does not have any packages to install.
Maybe run: yum groups mark install (see man yum)
No packages in any requested group available to install or update

これは最初エラーかなと思いましたが、実は必要なパッケージはインストール済みであるときに出るようです。

MariaDBのインストール

最新のRedmineインストール手順(redmine.jp)は、データベースにPostgreSQLを使うものになっています。今回は、MySQLMariaDB)のダンプを使うので、MariaDBをインストールします。

$ sudo yum install mariadb-server mariadb-devel
  :

MySQL/MariaDBでutf8を指定した場合、UTF-8のうち4バイト文字を扱えません。例えば絵文字が該当します。そこで、UTF-8の4バイト文字を扱えるようにするにはutf8mb4を指定します。

/etc/my.cnf.d/server.cnf
[mysqld]
character-set-server = utf8mb4
/etc/my.cnf.d/mysql-clients.cnf
[mysql]
default-character-set = utf8mb4
show-warnings
MariaDB自動起動設定
$ sudo systemctl enable mariadb.service
Created symlink from /etc/systemd/system/multi-user.target.wants/mariadb.service to /usr/lib/systemd/system/mariadb.service.
mysql_secure_installation実行

セキュアな初期設定を行うツール mysql_secure_installationを実施します。ツールを実行すると次の有無を尋ねられ、基本Yesを設定していきます。

  • MariaDBのrootユーザーのパスワードを設定
  • 匿名ユーザーを削除
  • MariaDBのrootユーザーのリモート接続禁止
  • testデータベースとそのアクセス設定を削除
redmineのユーザーアカウントとデータベース作成
$ mysql -uroot -p
MariaDB [(none)]> CREATE DATABASE redmine CHARACTER SET utf8mb4;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> CREATE USER 'redmine'@'localhost' IDENTIFIED BY '********';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> GRANT ALL PRIVILEGES ON redmine.* TO 'redmine'@'localhost';
Query OK, 0 rows affected (0.00 sec)

ImageMagickのインストール

$ sudo yum install ImageMagick ImageMagick-devel ipa-pgothic-fonts
  :

Redmine実行ユーザー作成

Redmineを実行する際、root権限ではなくredmine実行ユーザー権限とします。

$ sudo groupadd -g 1000 redmine
$ sudo useradd -u 1000 -g redmine redmine
$ sudo passwd redmine
  :

Redmineのダウンロード

$ sudo mkdir /var/lib/redmine-3.4-stable
$ sudo chwon redmine:redmine /var/lib/redmine-3.4-stable
$ su redmine
$ cd /var/lib
$ svn co http://svn.redmine.org/redmine/branches/3.4-stable redmine-3.4-stable
  : 

database.ymlの作成

production:
  adapter: mysql2
  database: redmine
  host: localhost
  username: redmine
  password: "********"
  encoding: utf8mb4

configuration.ymlの作成

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


imagemagick_convert_command: /usr/bin/convert
rmagick_font_path: /usr/share/fonts/ipa-pgothic/ipagp.ttf

Redmineが依存するgemパッケージのインストール

$ bundle install --without development test --path vendor/bundle
  :

セッション秘密鍵生成

$ bundle exec rake generate_secret_token

バックアップしたデータベースを復元

$ mysql -uredmine -p redmine < /tmp/redmine_mysql_www.torutk.com-20190430T110540.dump
Enter password:********
データベースのマイグレート
$ RAILS_ENV=production bundle exec rake db:migrate

unicornのインストール

Gemfile.local新規作成
  • /var/lib/redmine-3.4-stable/Gemfile.local
gem "unicorn"
gemパッケージのunicornインストール
$ bundle update
unicorn.rb

動作確認
$ bundle exec unicorn_rails -c config/unicorn.rb -E production

エラー発生

I, [2019-04-30T21:28:36.880592 #27919]  INFO -- : Refreshing Gem list
ArgumentError: wrong number of arguments (given 0, expected 2)
  /var/lib/redmine-3.4-stable/vendor/bundle/ruby/2.4.0/gems/unicorn-5.5.0/lib/unicorn.rb:49:in `block in builder'

unicorn 5.5.0だと発生するとのこと。
stackoverflow.com

unicorn 5.4.1で止めおくのが回避策とあるので、

redmine-unicorn.service
[Unit]
Description=Redmine Unicorn Server
After=mariadb.service

[Service]
WorkingDirectory=/var/lib/redmine
Environment=RAILS_ENV=production
SyslogIdentifier=redmine-unicorn
PIDFile=/var/lib/redmine/tmp/pids/unicorn.pid

ExecStart=/opt/rh/rh-ruby24/root/bin/bundle exec "unicorn_rails -c config/unicorn.rb -E production"
ExecStop=/usr/bin/kill -QUIT $MAINPID
ExecReload=/bin/kill -USR2 $MAINPID

User=redmine
Group=redmine

[Install]
WantedBy=multi-user.target

実行するとエラー

$ sudo systemctl start redmine-unicorn.service

原因を調べるため、suコマンドでユーザーを切り替えてunicornを実行するとエラーとなりました。
エラー内容は

/opt/rh/rh-ruby24/root/usr/bin/ruby: error while loading shared libraries: libruby.so.2.4: cannot open shared object file: No such file or directory

suコマンドで(-なしに)ユーザーを切り替える場合は、profile.dの設定を読み込まないので、rubyのパスが参照できないためです。
これは、systemdで実行ユーザーを指定して実行する場合と同じと考えられます。

systemdのunitファイル記述では、EnvironmentFile指定が可能ですが、このファイル内では変数展開ができないため、先に設置した/etc/profile.d/rh-ruby24.shを指定することができません。

回避策として、実行コマンドをsclコマンドで指定します。

  • /usr/lib/systemd/system/redmine-unicorn.service (修正箇所)
ExecStart=/usr/bin/scl enable rh-ruby24 -- bash -c 'bundle exec unicorn_rails -c config/unicorn.rb -E production'

Red Hat Software Collectionsのコマンドsclを使ってコマンドを実行すると、必要な環境設定が反映されます。

nginxとの連携

  • バックアップしていたredmine.confを/etc/nginx/conf.d/に配置
  • /etc/nginx/conf.d/default.confを削除、空のdefault.confに置き換え
  • /etc/nginx/conf.d/https.confを削除、空のhttpd.confに置き換え

nginxのアップデート時にdefault.confとhttps.confが生成されないよう、削除だけではなく空のファイルを置いておきます。

テーマのインストール

gitmike
$ cd /var/lib/redmine/public/themes
$ git clone git://github.com/makotokw/redmine-theme-gitmike.git gitmike

プラグインのインストール

$ cd /var/lib/redmine/plugins
Issue Template

現時点でmasterブランチはredmine 4.0以降のみ対応となっています。redmine 3.xで使うときは、ブランチ名にv0.2.x-support-Redmine3 を指定します。

$ git clone -b v0.2.x-support-Redmine3 https://github.com/akiko-pusu/redmine_issue_templates.git
Banner

現時点でmasterブランチはredmine 4.0以降のみ対応となっています。redmine 3.xで使うときは、ブランチ名にv0.1.x-support-Redmine3 を指定します。

$ git clone -b v0.1.x-support-Redmine3 https://github.com/akiko-pusu/redmine_banner.git
Wiki Extensions

現時点で存在するブランチdevelopおよびmasterはともにredmine 4.0以降のみ対応となっています。redmine 3.xで使うときは、タグ名に0.8.2を指定します。

$ git clone https://github.com/haru/redmine_wiki_extensions
XLS Export
$ git clone https://github.com/two-pack/redmine_xls_export.git
$ bundle install

gemパッケージのspreadsheet を必要とするので、bundle installを実行します。

Clipboard Image Paste
$ git clone https://github.com/peclik/clipboard_image_paste.git
Wiki Lists
$ git clone https://github.com/tkusukawa/redmine_wiki_lists.git
LaTeX MathJax Macro
$ git clone https://github.com/mboratko/redmine_latex_mathjax.git
$ bundle install
  • gemパッケージのrender_parentを必要とするので、bundle installを実行します。
Sidebar Hide
$ git clone https://gitlab.com/bdemirkir/sidebar_hide.git
Glossary
$ git clone https://github.com/torutk/redmine_glossary.git
Github hook
$ git clone https://github.com/koppen/redmine_github_hook.git

設定の復旧は後日実施。

View Customize
$ git clone https://github.com/onozaty/redmine-view-customize.git view_customize
$ bundle install
  • ディレクトリ名はview_customizeでないといけない
  • gemパッケージのactiverecord-compatible_legacy_migrationを必要とするので、bundle installを実行します
Startpage
$ git clone https://github.com/gatATAC/redmine_startpage.git

添付ファイルの復元

バックアップを取っていたマシンからrsyncで転送します。

リポジトリSVN、Git)の復元

SVNは、ダンプファイルをインポートします。
Gitは、git clone --mirror でクローンしたリポジトリから配置したい場所へpushします。

NginxからUnicorn使用

Nginxの設定ファイルを戻します。

およそ復元完了

4月30日の昼頃から作業を開始して、5月1日の昼頃までかかってしまいました。
平成のうちには更新が終わらなかったです。

次はRedmine 4.0へのアップデートをする予定です。