nginx passenger 連携(Debian/Ubuntu編)

nginx passenger 連携(Debian/Ubuntu編)

nginx passenger 連携(Debian/Ubuntu編)

nginx passenger 連携(Debian/Ubuntu編) へのコメントはまだありません

nginxとpassengerを連携させ、Railsアプリをnginxのサブフォルダで公開する手順を紹介します。

前提条件

以下、nginxインストールフォルダを/opt/nginx、/home/hiro/workspace/yourproj にRailsプロジェクトが存在すると仮定して説明します。debian wheezy7.3とUbuntu12.04で試しました。

下準備

nginxを ユーザー www-data で走らせるので、ログインユーザーをwww-dataグループに追加。

[bash]
$ sudo usermod -aG www-data $USER
[/bash]

グループを反映させるために一旦ログアウトしてください。

/opt/nginx フォルダを作成、権限調整

[bash]
$ sudo mkdir /opt/nginx
$ sudo chown www-data:www-data /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]

コンパイル前にチェックが走り、curl-config が無いよ的なエラーが表示された場合は、

[bash]
$ sudo apt-get install libcurl4-openssl-dev
[/bash]

他のエラーの場合は、ご自身でお調べください。
エラーが消えるまで以下を繰り返してみる。
[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]

以下の部分を追記
注意点!!! pidの行は、pidと /opt/nginx/logs/nginx.pid の間はタブで区切ってください!
(後に設定する、起動スクリプトでこの部分の設定を読み込むため)

[diff mark=”2″]
+ user www-data;
+ pid /opt/nginx/logs/nginx.pid;

〜省略〜
server {
listen 80;
server_name localhost;

〜省略〜
+ 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するのは面倒なので、起動スクリプトを作成。この起動スクリプトは apt-get でインストールされたものを、調整したものです。

調整箇所は DEAMONのパスとnginx設定ファイルのパス。両方フルパスで指定しておきました。

ここからダウンロードできます。
nginx起動スクリプト

[diff mark=”14,28″]
#!/bin/sh

### BEGIN INIT INFO
# Provides: nginx
# Required-Start: $local_fs $remote_fs $network $syslog $named
# Required-Stop: $local_fs $remote_fs $network $syslog $named
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the nginx web server
# Description: starts nginx using start-stop-daemon
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/opt/nginx/sbin/nginx
NAME=nginx
DESC=nginx

# Include nginx defaults if available
if [ -r /etc/default/nginx ]; then
. /etc/default/nginx
fi

test -x $DAEMON || exit 0

. /lib/init/vars.sh
. /lib/lsb/init-functions

PID=$(awk -F'[ \t;]’ ‘$1 !~ /^#/ && /pid/ {print $2}’ /opt/nginx/conf/nginx.conf)

if [ -z “$PID” ]
then
PID=/run/nginx.pid
fi

# Check if the ULIMIT is set in /etc/default/nginx
if [ -n “$ULIMIT” ]; then
# Set the ulimits
ulimit $ULIMIT
fi

#
# Function that starts the daemon/service
#
do_start()
{

以下省略
[/diff]

起動スクリプトコピー、サービス登録。

[bash]

$ sudo mv nginx /etc/init.d/
$ sudo chmod 755 /etc/init.d/nginx
$ sudo chown root:root /etc/init.d/nginx

# Debianの場合
$ sudo insserv nginx

# Ubuntuの場合
$ sudo sysv-rc-conf nginx on

[/bash]

これで、

[bash]

$ sudo /etc/init.d/nginx start
$ sudo /etc/init.d/nginx stop
$ sudo /etc/init.d/nginx restart
[/bash]

もしくは

[bash]

$ sudo service nginx start
[/bash]

など使えるようになります。

Railsアプリ動作確認

ブラウザーで http://localhost/yourproj へアクセスし、Railsのアプリが動けばOKです。

About the author:

Related Posts

Leave a comment

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)

Back to Top