2012年4月20日金曜日

Sinatraをapacheで動かす

sinatraをapacheで動かしてみましょう。
Apacheが入ってることが前提で、passenger のインストールを行います。
先に作成した config.ru が必要になります。

下記を参考にしました!ありがとうございます!
http://d.hatena.ne.jp/foosin/20090619/1245426335

passengerのインストール
2つのモジュールが必要です。
1つ目はあっという間。
$ gem install passenger
Fetching: fastthread-1.0.7.gem (100%)
Building native extensions.  This could take a while...
Fetching: daemon_controller-1.0.0.gem (100%)
Fetching: passenger-3.0.12.gem (100%)
Successfully installed fastthread-1.0.7
Successfully installed daemon_controller-1.0.0
Successfully installed passenger-3.0.12
3 gems installed
Installing ri documentation for fastthread-1.0.7...
Installing ri documentation for daemon_controller-1.0.0...
Installing ri documentation for passenger-3.0.12...
Installing RDoc documentation for fastthread-1.0.7...
Installing RDoc documentation for daemon_controller-1.0.0...
Installing RDoc documentation for passenger-3.0.12...

2つ目はちょっと長いです。
$ passenger-install-apache2-module
Welcome to the Phusion Passenger Apache 2 module installer, v3.0.12.

This installer will guide you through the entire installation process. It
shouldn't take more than 3 minutes in total.

Here's what you can expect from the installation process:

 1. The Apache 2 module will be installed for you.
 2. You'll learn how to configure Apache.
 3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.
ここではとりあえずEnter押下。
しばし待つと
 1. The Apache 2 module will be installed for you.
が終わって、
 2. You'll learn how to configure Apache.

の説明が表示されます。

下記3行は環境によって異なりますので、メモしておきます。
あとで必要になります。
Load xxxxx
PassengerRoot xxxxx
PassengerRuby xxxxx

--------------------------------------------
The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12/ext/apache2/mod_passenger.so
   PassengerRoot /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12
   PassengerRuby /home/tiffany/.rvm/wrappers/ruby-1.9.2-p290@rails3/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.
無事インストールされたようなのでEnter押下。
最後に
 3. You'll learn how to deploy a Ruby on Rails application.

で、こういうメッセージが表示されます。
--------------------------------------------
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:

  /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12/doc/Users guide Apache.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
http://www.modrails.com/

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.


Passengerのインストールはこれで終了です。


apacheの設定
環境によって異なると思いますが、、下記のファイルに上記3を参考に設定を行います。
$ sudo vi /etc/apache2/sites-available/xxx
そのファイルに下記を追加します。
上記でメモした3行

   LoadModule passenger_module /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12/ext/apache2/mod_passenger.so
   PassengerRoot /home/tiffany/.rvm/gems/ruby-1.9.2-p290@rails3/gems/passenger-3.0.12
   PassengerRuby /home/tiffany/.rvm/wrappers/ruby-1.9.2-p290@rails3/ruby
モジュールのパス設定
<VirtualHost *:80>
    ServerName www.yourhost.com
    DocumentRoot /home/tiffany/sinatra/public
    <Directory  /home/tiffany/sinatra/public>
        AllowOverride all
        Options -MultiViews
    </Directory>

     ErrorLog /var/log/apache2/error.log
     CustomLog /var/log/apache2/access.log combined
</VirtualHost>

これで、apacheを再起動すればOK!
RACK_ENV=productionでアプリが起動します。
もし、development環境でapache を動かしたい場合は、下記のようにRackEnv development をapacheの設定ファイルに追加してください。
    DocumentRoot /home/tiffany/sinatra/public
    RackEnv development

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。