目新しいものではありませんが、Railsアプリケーションを構築した際にやっぱりApache経由でインターネット上に公開したいですよね。・・・ね?
てことで、方法をまとめておこうと思います。
passengerの導入
今回利用したものはgemライブラリのPhusion Passengerです。(へぇー、Phusionって付くんだ。)
てことでGemfileに追加してインストールします。
gem 'passenger'
bundle install
ライブラリのインストールが終わったら、passenger-install-apache2-moduleを実行して、Apache用のモジュールをインストールします。
./vendor/bundler/ruby/1.9.1/gems/passenger-4.0.5/bin/passenger-install-apache2-module (パスは自分の環境に読み変えてね)
そうするとわらわらと文字が流れていき
The Apache 2 module was successfully installed.
Please edit your Apache configuration file, and add these lines:
LoadModule passenger_module /Documents/rails_work/vendor/bundler/ruby/1.9.1/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
PassengerRoot /Documents/rails_work/vendor/bundler/ruby/1.9.1/gems/passenger-4.0.5
PassengerDefaultRuby /.rbenv/versions/1.9.3-p327/bin/ruby
After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!
Press ENTER to continue.
Deploying a Ruby on Rails application: an example
Suppose you have a Rails application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:
<VirtualHost *:80>
ServerName www.yourhost.com
# !!! Be sure to point DocumentRoot to 'public'!
DocumentRoot /somewhere/public
<Directory /somewhere/public>
# This relaxes Apache security settings.
AllowOverride all
# MultiViews must be turned off.
Options -MultiViews
</Directory>
</VirtualHost>
And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:
/Documents/rails_work/vendor/bundler/ruby/1.9.1/gems/passenger-4.0.5/doc/Users guide Apache.html
Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
https://www.phusionpassenger.com
Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.等と出力されます。これをそのままApacheの設定ファイルに追記すればOKです。
httpd.confの設定
僕の環境である/etc/apache2/httpd.confを設定していきます。(と言ってもほとんどコピペ。)
<VirtualHost *:80>
ServerName localhost #サーバ名
DocumentRoot /Documents/rails_work/public #Railsアプリケーションルート/publicを指定
ErrorLog /var/log/apache2/error.log #Railsアプリケーションに対するApacheのエラーログ
TransferLog /var/log/apache2/access.log #Railsアプリケーションに対するApacheのアクセスログ
RackEnv
<Directory /Documents/rails_work/public> #DocumentRootと同じ
AllowOverride all #DocumentRoot/public/.htaccessで個別の設定を行えるようにする
Options -MultiViews
Order allow,deny
allow from all
</Directory>
</VirtualHost>
</pre>
VirtualHostの設定項目については[http://httpd.apache.org/docs/2.2/ja/vhosts/examples.html:title]を参照。
さらにhttpd.confの一番下にモジュールの読み込みを追記。
<pre>
Include /private/etc/apache2/other/*.conf
LoadModule passenger_module /Documents/rails_work/vendor/bundler/ruby/1.9.1/gems/passenger-4.0.5/libout/apache2/mod_passenger.so
PassengerRoot /Documents/rails_work/vendor/bundler/ruby/1.9.1/gems/passenger-4.0.5
PassengerDefaultRuby /.rbenv/versions/1.9.3-p327/bin/ruby
DocumentRoot/public/.htaccessに、動作環境を定義しておきます。
RackEnv "development" #peoductionにすれば本番環境
ここまでやったらApacheを再起動。これでOK。
...動作環境定義とか色々苦労した気がするんですがもう忘れてしまいました。。。
8月25日追記
上記の通り設定を行いトップページへアクセスしたところ、何故かRailsのトップページが表示された。
原因はDocumentRoot(/Documents/rails_work/public)の直下にindex.htmlがあること。
これを消したら今度はトップページが無いよ!というエラーが表示された。
script/rails serverで動かしていた時は自動でindexアクションが処理されていましたが、passengerにした途端にそれが効かなくなってしまったようです。
そのため、config/routes.rbにindexアクションをデフォルトとする設定を行いました。
root :to => 'searches#index' #これを追記 >||