nginxとpassengerを連携させ、Railsアプリをnginxのサブフォルダで公開する手順を紹介します。
前提条件
以下、nginxインストールフォルダを/opt/nginx、/home/hiro/workspace/yourproj にRailsプロジェクトが存在すると仮定して説明します。
下準備
ユーザーnginxを追加し、ログインユーザーをnginxグループに追加。
|
$ sudo useradd -s /bin/false -M nginx $ sudo usermod -aG nginx $USER |
グループを反映させるために一旦ログアウトしてください。
/opt/nginx フォルダを作成、権限調整
|
$ sudo mkdir /opt/nginx $ sudo chown nginx:nginx /opt/nginx -R $ sudo chmod 775 /opt/nginx |
passengerのgemインストール
|
$ cd /home/hiro/workspace/yourproj $ gem install passenger |
passenger 連携のために、nginx再コンパイル
|
$ passenger-install-nginx-module |
コンパイル前にチェックが走り、何らかのモジュールが無いよ的なエラーが表示された場合は、ご自身でお調べください。
yumでパッケージを追加し、エラーが消えるまで以下を繰り返してみる。
|
$ passenger-install-nginx-module |
無事チェックを通過すれば、コンパイルするかと聞いてくるので、1を選んでコンパイルする
めでたくコンパイルが始まる。(そこそこ待ちます。)
インストールが完了するとnginxの設定ファイルに passengerの場所が書きこまれます。
|
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; ... } |
nginxの設定確認と、起動。
-t オプションで設定ファイルをチェックし、OKならば、一旦起動してみて、ブラウザーで http://localhost として、nginxのトップページが開ければOK。
|
$ 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 |
として、nginx: master process となるプロセスIDを殺すと止めることができます。
nginx passenger連携設定
passengerの場所は、インストール時に自動的に組み込まれているので、その他の項目の調整。
http://localhost/yourproj で公開する。
|
$ nano /opt/nginx/conf/nginx.conf |
以下の部分を追記
|
+ 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; + } |
起動スクリプト作成
いちいち、/opt/nginx/sbin/nginx としたり、pidを探してKillするのは面倒なので、起動スクリプトを作成。この起動スクリプトは よそのサイトから拝借したものを、調整したものです。ごめんなさい、閲覧したサイト忘れちゃいました・・・。
調整箇所は nginxのパスとnginx設定ファイルのパス、ロックファイルのパス。すべてフルパスで指定しておきました。
ここからダウンロードできます。
nginx起動スクリプト
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
#!/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 以下省略 |
起動スクリプトコピー、サービス登録。
|
$ sudo mv nginx.sample /etc/init.d/nginx $ sudo chmod 755 /etc/init.d/nginx $ sudo chkconfig --add nginx |
これで、
|
$ sudo /etc/rc.d/init.d/nginx start $ sudo /etc/rc.d/init.d/nginx stop $ sudo /etc/rc.d/init.d/nginx restart |
もしくは
|
$ sudo service nginx start |
など使えるようになります。
Railsアプリ動作確認
ブラウザーで http://localhost/yourproj へアクセスし、Railsのアプリが動けばOKです。
Leave a comment