LoginSignup
0
0

More than 1 year has passed since last update.

nginx+rbenv+Redmine+unicornをサブディレクトリ付きで動かそうとした際に対応しておきたいこと

Last updated at Posted at 2020-10-11

はじめに

もともとは、当方の作業進捗管理をするために、Redmineを導入することにしたのがきっかけです。

Redmine(今回は4.1.1を採用)を当方のVPSサーバー(CentOS7)にインストールするために、このサイトを参考にして構築していました。当方はWebサーバーにnginxを使用しているため、有効活用することにしました。すでにrbenvをインストール済みで、Ruby2.6.4と2.7.2が入っています。

Webアプリのファイルはすべて /usr/share/nginx/html に収めています。

また、当方のサイトはメインサイトが有るため、サブディレクトリ(例:/hoge)でアクセスするようにしました。

そのときに発生した色々な問題と解決方法をここにメモすることにしました。

1. redmine-unicorn.service内ExecStartの修正

1-1. 起動するRubyのパスを指定

当方がrbenv globalしているRubyは2.7.2。しかし、Redmine(4.1.1)は2.7に非対応…そのため、すでにrbenvを使用してインストールしている2.6.4を直接使用するために、redmine-unicorn.serviceのExecStartを修正する必要があります。

Ruby2.6.4をインストールしているディレクトリを/usr/local/rbenv/versions/2.6.4とすると、設定は以下のようになります。

[編集前]
ExecStart= bundle exec "unicorn_rails -c config/unicorn.rb -E production"
[編集後]
ExecStart= /usr/local/rbenv/versions/2.6.4/bin/bundle exec "PATH=$PATH:/usr/local/rbenv/versions/2.6.4/bin /usr/local/rbenv/versions/2.6.4/bin/unicorn_rails -c /usr/share/nginx/html/redmine/config/unicorn.rb -E production"

1-2. Unicornをデーモンで起動する設定を追加

Unicornを使って最新のRedmineを起動する際、Unicornをデーモンとして起動させる必要があります。付けないとエラーになります。
ExecStartに -Dオプションを付けておきます

[変更前]
ExecStart=/usr/local/rbenv/versions/2.6.4/bin/bundle exec "(略) -E production"

[変更後]
ExecStart=/usr/local/rbenv/versions/2.6.4/bin/bundle exec "(略) -E production -D"

参考: https://teratail.com/questions/186552

1-3. オプションにサブディレクトリを指定

redmine-unicorn.serviceのExecStartに --pathオプションでサブディレクトリを指定します。

[変更前]
ExecStart=/usr/local/rbenv/versions/2.6.4/bin/bundle exec "(略) -E production -D"

[変更後]
ExecStart=/usr/local/rbenv/versions/2.6.4/bin/bundle exec "(略) -E production -D --path /hoge"

参考: https://qiita.com/61503891/items/732d9c123c39e35e97eb

2. ソケットは使わずhttpポートを使用

unicorn_redmine.sockでconnection refusedが起きるときは、unixソケットを使わず、httpポートを使うのも手です

  • config/unicorn.rb
[変更前]
listen  File.expand_path('tmp/unicorn_redmine.sock', app_path)

[変更後]
listen 3000
  • nginx/conf.d/(redmine用コンフィグファイル名)
[変更前]
upstream unicorn-redmine {
  server unix:/usr/share/nginx/html/redmine/tmp/unicorn_redmine.sock;
}

[変更後]
upstream unicorn-redmine {
  server 127.0.0.1:3000;
}

一旦サービスを落とした後、tmp/unicorn.pidが残っていればさっさと削除しちゃいましょう。

参考: https://teratail.com/questions/87083

3. config.ruをリネーム

ちゃんとここまで設定しているのにエラーが発生してまうので調べてみると、なんとconfig.ruが悪さしているという記事を見つけました。
config.ruが無くてもunicornがなんとかしてくれるそうなので、削除するかリネームしちゃったほうがいいでしょう。

mv config.ru config.ru.org

参考1: https://higelog.brassworks.jp/1760
参考2: http://im13.hatenablog.com/entry/2016/03/05/221916

4. 静的ファイルにアクセスさせるための設定

サブディレクトリ /hoge が付いているため、そのまま静的ファイルにアクセスしようとすると404エラーが返ってきます。
そのため、nginxのルーティング設定を使って、サーバー側のディレクトリに正しく誘導する必要があります。

        location ~ ^/hoge/.*/.*.(css|js|jpe?g|png|gif|ico|swf|woff2|ttf)$ {
                root    /usr/share/nginx/html/redmine/public;
                try_files $uri @app;
        }

さいごに

これらの設定を経て、無事にRedmineがきれいに起動いたしました。めっちゃ感動…
気持ちえー!

redmine_succeed.png

0
0
1

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
0
0