nginxとpassengerを連携させ、Railsアプリをnginxのサブフォルダで公開する手順を紹介します。
前提条件
以下、nginxインストールフォルダを/opt/nginx、/home/hiro/workspace/yourproj にRailsプロジェクトが存在すると仮定して説明します。
下準備
ユーザーnginxを追加し、ログインユーザーをnginxグループに追加。
[bash]
$ sudo useradd -s /bin/false -M nginx
$ sudo usermod -aG nginx $USER
[/bash]
グループを反映させるために一旦ログアウトしてください。
/opt/nginx フォルダを作成、権限調整
[bash]
$ sudo mkdir /opt/nginx
$ sudo chown nginx:nginx /opt/nginx -R
$ sudo chmod 775 /opt/nginx
[/bash]
passengerのgemインストール
[bash]
$ cd /home/hiro/workspace/yourproj
$ gem install passenger
[/bash]
passenger 連携のために、nginx再コンパイル
[bash]
$ passenger-install-nginx-module
[/bash]
コンパイル前にチェックが走り、何らかのモジュールが無いよ的なエラーが表示された場合は、ご自身でお調べください。
yumでパッケージを追加し、エラーが消えるまで以下を繰り返してみる。
[bash]
$ passenger-install-nginx-module
[/bash]
無事チェックを通過すれば、コンパイルするかと聞いてくるので、1を選んでコンパイルする
めでたくコンパイルが始まる。(そこそこ待ちます。)
インストールが完了するとnginxの設定ファイルに passengerの場所が書きこまれます。
[bash]
Please specify a prefix directory [/opt/nginx]:
The Nginx configuration file (/opt/nginx/conf/nginx.conf)
must contain the correct configuration options in order for Phusion Passenger
to function correctly.
This installer has already modified the configuration file for you! The
following configuration snippet was inserted:
http {
…
passenger_root /home/hiro/.rvm/gems/ruby-2.0.0-p451/gems/passenger-4.0.25;
passenger_ruby /home/hiro/.rvm/wrappers/ruby-2.0.0-p451/ruby;
…
}
[/bash]
nginxの設定確認と、起動。
-t オプションで設定ファイルをチェックし、OKならば、一旦起動してみて、ブラウザーで http://localhost として、nginxのトップページが開ければOK。
[bash]
$ sudo /opt/nginx/sbin/nginx -t
nginx: the configuration file /opt/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /opt/nginx/conf/nginx.conf test is successful
$ sudo /opt/nginx/sbin/nginx
[/bash]
[bash]
ps aux |grep nginx
[/bash]
として、nginx: master process となるプロセスIDを殺すと止めることができます。
[bash]
$ sudo kill pid
[/bash]
nginx passenger連携設定
passengerの場所は、インストール時に自動的に組み込まれているので、その他の項目の調整。
http://localhost/yourproj で公開する。
[bash]
$ nano /opt/nginx/conf/nginx.conf
[/bash]
以下の部分を追記
[diff]
+ user nginx;
+ location ~ ^/yourproj {
+ root /home/hiro/workspace/yourproj/public;
+ passenger_base_uri /yourproj;
+ passenger_app_root /home/hiro/workspace/yourproj;
+ passenger_enabled on;
+ rails_env production;
+ }
[/diff]
起動スクリプト作成
いちいち、/opt/nginx/sbin/nginx としたり、pidを探してKillするのは面倒なので、起動スクリプトを作成。この起動スクリプトは よそのサイトから拝借したものを、調整したものです。ごめんなさい、閲覧したサイト忘れちゃいました・・・。
調整箇所は nginxのパスとnginx設定ファイルのパス、ロックファイルのパス。すべてフルパスで指定しておきました。
ここからダウンロードできます。
nginx起動スクリプト
[diff mark=”18,21,23″]
#!/bin/sh
#
# nginx – this script starts and stops the nginx daemon
#
# chkconfig: – 85 15
# description: Nginx is an HTTP(S) server, HTTP(S) reverse \
# proxy and IMAP/POP3 proxy server
# processname: nginx
# Source function library.
. /etc/rc.d/init.d/functions
# Source networking configuration.
. /etc/sysconfig/network
# Check that networking is up.
[ “$NETWORKING” = “no” ] && exit 0
nginx=”/opt/nginx/sbin/nginx”
prog=$(basename $nginx)
NGINX_CONF_FILE=”/opt/nginx/conf/nginx.conf”
lockfile=/opt/nginx/logs/nginx.lock
start() {
[ -x $nginx ] || exit 5
[ -f $NGINX_CONF_FILE ] || exit 6
echo -n $”Starting $prog: ”
daemon $nginx -c $NGINX_CONF_FILE
以下省略
[/diff]
起動スクリプトコピー、サービス登録。
[bash]
$ sudo mv nginx.sample /etc/init.d/nginx
$ sudo chmod 755 /etc/init.d/nginx
$ sudo chkconfig –add nginx
[/bash]
これで、
[bash]
$ sudo /etc/rc.d/init.d/nginx start
$ sudo /etc/rc.d/init.d/nginx stop
$ sudo /etc/rc.d/init.d/nginx restart
[/bash]
もしくは
[bash]
$ sudo service nginx start
[/bash]
など使えるようになります。
Railsアプリ動作確認
ブラウザーで http://localhost/yourproj へアクセスし、Railsのアプリが動けばOKです。
Leave a comment