Program LOG

勉強した内容をまとめ中。。。間違ってたら教えて。。。

Windowsでruby開発 - apache連携編

前回に引き続き、Railsの環境構築をやっていこうと思います。
今回は、Railsのプロジェクトの立上げと、Apache連動の設定関連をやりたいと思います。

ここまでの流れは以下の記事を確認してください。
Windowsでruby開発 - 開発環境構築編 - Program LOG

Railsプロジェクトを作ってみる

さて、せっかくインストールしたので、Railsプロジェクトを作成したいと思います。
今回は簡単な掲示板のようなものを作成して基本的なCRUDの習得やバリデートなんかをやってみようかと思います。
ですので、プロジェクト名は「simplebbs」としました。

[vagrant@localhost /]$ rails new simplebbs

作成には多少時間がかかります。。。
次に、rails serverを起動してします。

[vagrant@localhost /]$ rails server
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle ifyou use bundle --path) will improve the startup performance of Spring.
/home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/execjs-2.0.2/lib/execjs/runtimes.rb:51:in `autodetect': Could not find a JavaScript runtime. See https://github.com/sstephenson/execjs for a list of available runtimes. (ExecJS::RuntimeUnavailable)

エラーが出現しました。。。どうやら、JavaScript runtimeが入ってないとの事なので、
以下のように入れました。

[vagrant@localhost /]$ cd /simplebbs
[vagrant@localhost /simplebbs]$ vi Gemfile 
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
# gem 'therubyracer',  platforms: :ruby
↓以下のように修正
# See https://github.com/sstephenson/execjs#readme for more supported runtimes
 gem 'therubyracer',  platforms: :ruby

[vagrant@localhost /simplebbs]$ bundle install

そうしたら、起動するのですが、まだWarningが表示される。。。

[vagrant@localhost simplebbs]$ rails s
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.
=> Booting WEBrick
=> Rails 4.1.0 application starting in development on http://0.0.0.0:3000
=> Run `rails server -h` for more startup options
=> Notice: server is listening on all interfaces (0.0.0.0). Consider using 127.0.0.1 (--binding option)
=> Ctrl-C to shutdown server
[2014-04-20 07:17:52] INFO  WEBrick 1.3.1
[2014-04-20 07:17:52] INFO  ruby 2.0.0 (2014-02-24) [x86_64-linux]
[2014-04-20 07:17:52] INFO  WEBrick::HTTPServer#start: pid=3774 port=3000

どうやらgemのupdateをした際に以下のコマンドを実行していなかったのが原因のようです。

[vagrant@localhost simplebbs]$ gem pristine --all

その後Warningが消えました。。。
まぁ、無事に起動を確認出来ました。
今回は、private_networkを192.168.33.20に設定したので、「http://192.168.33.20:3000」とアクセスしたら、
ブラウザで確認できるはずです。

Apacheとの連携

このまま開発を進めても問題ないのですが、本番環境ではApacheと連携する必要があるので、
連携するようにしていきましょう。
まず、apacheと連携するには、passengerというツールを使うのがいいという情報が多数あった為、
今回はpassengerを使用したいと思います。

[vagrant@localhost simplebbs]$ gem install passenger --no-ri --no-rdoc -V
[vagrant@localhost simplebbs]$ rbenv rehash

このboxには、既にhttpdのインストールが済んでいるので、その他で必要になるツールをyum
インストールしていきます。

[vagrant@localhost simplebbs]$ sudo yum install gcc-c++ httpd-devel apr-devel -y
[vagrant@localhost simplebbs]$ sudo yum install -y libcurl-devel openssl-devel zlib-devel apr-util-devel

次にApaccheとのモジュールをインストールします。
以下のコマンドを実行すると、丁寧に手順を教えてくれます。

[vagrant@localhost simplebbs]$ passenger-install-apache2-module

「Enter」を押下すると、モジュールが作られます。

Which languages are you interested in?
Use  to select.

と聞かれるので、「ruby」を選択して実行

Warning: some directories may be inaccessible by the web server!

The web server typically runs under a separate user account for security
reasons. That user must be able to access the Phusion Passenger files.
However, it appears that some directories have too strict permissions. This
may prevent the web server user from accessing Phusion Passenger files.

It is recommended that you relax permissions as follows:

  sudo chmod o+x "/home/vagrant"

Press Ctrl-C to return to the shell. (Recommended)
After relaxing permissions, re-run this installer.
  -OR-
Press Enter to continue anyway.

warning登場・・・!でも叩くコマンドが出とる!
一回Ctrl+Cでインストールを抜けて、実行後、再度インストールを開始。
おぉ・・・・・インストールが実行されていきます。

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

   LoadModule passenger_module /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/passenger-4.0.41/buildout/apache2/mod_passenger.so
   
     PassengerRoot /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/passenger-4.0.41
     PassengerDefaultRuby /home/vagrant/.rbenv/versions/2.0.0-p451/bin/ruby
   

After you restart Apache, you are ready to deploy any number of web
applications on Apache, with a minimum amount of configuration!

Press ENTER to continue.

多分apacheにこのmoduleを書き込めって事でしょうか・・・。
さらにEnterを押すと、次のステップへ・・・

Deploying a web application: an example

Suppose you have a web application in /somewhere. Add a virtual host to your
Apache configuration file and set its DocumentRoot to /somewhere/public:

   
      ServerName www.yourhost.com
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /somewhere/public
      
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
      
   

And that's it! You may also want to check the Users Guide for security and
optimization tips, troubleshooting and other useful information:

  /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/passenger-4.0.41/doc/Users guide Apache.html
  http://www.modrails.com/documentation/Users%20guide%20Apache.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.

Virtualhostの設定が出て来ました。
この情報を元に、apacheのconfファイルを修正していきます。
今回は以下のように設定しました。

NameVirtualHost *:80

  ServerName simplebbs.net
  DocumentRoot /vagrant/docroot/simplebbs/public
  ErrorLog /var/log/ruby_error.log
  TransferLog /var/log/ruby_access.log
  
    AllowOverride all
    Options -MultiViews
    Order allow,deny
    allow from all
  

LoadModule passenger_module /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/passenger-4.0.41/buildout/apache2/mod_passenger.so

PassengerRoot /home/vagrant/.rbenv/versions/2.0.0-p451/lib/ruby/gems/2.0.0/gems/passenger-4.0.41
PassengerDefaultRuby /home/vagrant/.rbenv/versions/2.0.0-p451/bin/ruby

httpdのリスタート

[vagrant@localhost conf]$ sudo service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

これで、hostsの設定を修正して、「simplebbs.net」ってドメインにブラウザからアクセスすれば、
railsプロジェクトにつながるはずです。
・・・・500エラー!!!
エラーが何も出ているようではないのですが・・・ん~。。。
[/var/log/ruby_access.log]と[cat /var/log/ruby_error.log]を確認したら、
ちゃんと実行はされているようです。調査開始!
どうやら、「.htaccess」で以下のように指定しないとダメらしいです。

[vagrant@localhost /]$ cd /vagrant/docroot/simplebbs/public/
[vagrant@localhost public]$ vi .htaccess
RailsEnv "development"
[vagrant@localhost conf]$ sudo service httpd restart
Stopping httpd:                                            [  OK  ]
Starting httpd: httpd: Could not reliably determine the server's fully qualified domain name, using localhost.localdomain for ServerName
                                                           [  OK  ]

再度アクセスしたところ、「Welcome aboard」のページが表示されました!
まだ何も開発していませんが、このページも長くなってきてしまったので、
今回はこの辺で終わりたいと思います。
次回は、IDENetBeans)の設定関連を進めたいと思います。

使用環境