uoz 作業日記

様々な作業の記録を共有するブログです。

railsをapacheでうごかすpassengerをいれてみた centos 6.4 sakuraのVPS

いちいちwebrick立ち上げるのもめんどくさいなあとおもい、やってみる。

http://redmine.jp/tech_note/apache-passenger/
この手順に従った。

ちなみに、私の環境ではrbenvを使ってます。

まずやってみる

$ gem install passenger

できた

$ passenger-install-apache2-module

これがコマンド見つからない。
環境変数が反映されてないせいだと見当がついたが、どこに書いてあるか分からんし面倒だからいったんログアウトした。
再度チャレンジしたらできた。


対話式なのか!こんなのでてきたので、エンターを押す

Welcome to the Phusion Passenger Apache 2 module installer, v4.0.40.

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.


次は言語

Railsを動かしたいのでRubyを選んでOK
--------------------------------------------

Which languages are you interested in?

Use <space> to select.
If the menu doesn't display correctly, press '!'

 ‣ ⬢  Ruby
   ⬢  Python
   ⬡  Node.js
   ⬡  Meteor


こんな感じで、要件をチェックしてくれた

--------------------------------------------

Checking for required software...

 * Checking for C compiler...
      Found: yes
      Location: /usr/bin/cc
 * Checking for C++ compiler...
      Found: yes
      Location: /usr/bin/c++
 * Checking for Curl development headers with SSL support...
      Found: no
      Error: Cannot find the `curl-config` command.
 * Checking for OpenSSL development headers...
      Found: yes
      Location: true
 * Checking for Zlib development headers...
      Found: yes
      Location: true
 * Checking for Apache 2...
      Found: yes
      Location of httpd: /usr/sbin/httpd
      Apache version: 2.2.15
 * Checking for Apache 2 development headers...
      Found: no
 * Checking for Rake (associated with /home/XXXXX/.rbenv/versions/1.9.3-p484/bin/ruby)...
      Found: yes
      Location: /home/XXXXX/.rbenv/versions/1.9.3-p484/bin/ruby /home/XXXXX/.rbenv/versions/1.9.3-p484/bin/rake
 * Checking for OpenSSL support for Ruby...
      Found: yes
 * Checking for RubyGems...
      Found: yes
 * Checking for Ruby development headers...
      Found: yes
      Location: /home/XXXXX/.rbenv/versions/1.9.3-p484/include/ruby-1.9.1/ruby.h
 * Checking for rack...
      Found: yes
 * Checking for Apache Portable Runtime (APR) development headers...
      Found: no
 * Checking for Apache Portable Runtime Utility (APU) development headers...
      Found: no

Some required software is not installed.
But don't worry, this installer will tell you how to install them.
Press Enter to continue, or Ctrl-C to abort.


Curl development headers with SSL support
Apache 2 development headers
Apache Portable Runtime (APR) development headers
Apache Portable Runtime Utility (APU) development headers


この4つが入ってないが、インストール方法を教えてくれるらしい。
Enterを押すとこんなん出てきた。

--------------------------------------------

Installation instructions for required software

 * To install Curl development headers with SSL support:
   Please install it with yum install curl-devel

 * To install Apache 2 development headers:
   Please install it with yum install httpd-devel

 * To install Apache Portable Runtime (APR) development headers:
   Please install it with yum install apr-devel

 * To install Apache Portable Runtime Utility (APU) development headers:
   Please install it with yum install apr-util-devel

If the aforementioned instructions didn't solve your problem, then please take
a look at the Users Guide:

  /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.40/doc/Users guide Apache.html
  http://www.modrails.com/documentation/Users%20guide%20Apache.html

足りないものをインストール

以下のようにインストール

$ sudo yum install curl-devel httpd-devel apr-devel apr-util-devel

パスワード聞かれます。
何をインストールするかリストがでてきて、これでいいか?ってきかれるのでy押してenter.

再チャレンジ

$ passenger-install-apache2-module

Enter2回でapacheコンパイルが始まる。
結構長い。
字がじゃんじゃん出てきます。


終わるとこんなのがでてくる。
httpd.confに追記しないといけないんだろう。

--------------------------------------------
Almost there!

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

   LoadModule passenger_module /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.40/buildout/apache2/mod_passenger.so
   <IfModule mod_passenger.c>
     PassengerRoot /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.40
     PassengerDefaultRuby /home/XXXXX.rbenv/versions/1.9.3-p484/bin/ruby
   </IfModule>

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.


エンター押すと、バーチャルホストの設定もでてきた。

--------------------------------------------

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:

   <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/XXXXX.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.40/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.

設定

apacheの設定ファイル httpd.confの場所を忘れたので、locateで探す

$ locate httpd.conf
/etc/httpd/conf/httpd.conf


これをvimで開いて上のを書き加える

sudo vi /etc/httpd/conf/httpd.conf

viコマンドで開いたら、/でLoadModuleという文字列を探す。

/LoadModule


たくさんLoadModuleって書いてあるところがあるから、その末尾に上の文字列をいれよう。
私の環境ではこんなんになった。

/etc/httpd/conf/httpd.conf

*略
LoadModule cgi_module modules/mod_cgi.so
LoadModule version_module modules/mod_version.soLoadModule passenger_module /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.40/buildout/apache2/mod_passenger.
so   
<IfModule mod_passenger.c>
    PassengerRoot /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.40
    PassengerDefaultRuby /home/XXXXX.rbenv/versions/1.9.3-p484/bin/ruby
</IfModule>


さらに、バーチャルホストの設定。
同じファイルの一番下に書き込む。

/etc/httpd/conf/httpd.conf

略

<VirtualHost *>
DocumentRoot /home/XXXX/Tameshi/public
ServerName あなたのほすと
HostNameLookups double
UseCanonicalName off
    <Directory /home/XXXX/Tameshi/public>
        Allow from all
        Options -MultiViews
    </Directory>
</VirtualHost>


DocumentRootには、railsアプリのディレクトリ中のpublicディレクトリを指定する。
上の例では、自分のユーザのhomeにアプリをつくることにしてる。


最後に、apacheの再起動

$ sudo service httpd restart

試しにアプリを動かす

さて、これでインストールは終了したはず。
/var/www/html以下にRailsのファイルを置いて、動かしてみる。

http://d.hatena.ne.jp/maeharin/20130212/rails_generate
ここを参考に、scaffoldで空のrailsアプリを作る。


$ cd /var/www/html
$ rails new Tameshi
$ cd Tameshi
$ rails g scaffold hito name:string

と思ったらエラー。
javascriptエンジン関連だろう。
本筋と関係ないが、全部のせる。

/home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/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)
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/execjs-2.0.2/lib/execjs.rb:5:in `<module:ExecJS>'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/execjs-2.0.2/lib/execjs.rb:4:in `<top (required)>'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/uglifier-2.5.0/lib/uglifier.rb:3:in `require'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/uglifier-2.5.0/lib/uglifier.rb:3:in `<top (required)>'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/bundler-1.5.1/lib/bundler/runtime.rb:76:in `require'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/bundler-1.5.1/lib/bundler/runtime.rb:76:in `block (2 levels) in require'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/bundler-1.5.1/lib/bundler/runtime.rb:72:in `each'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/bundler-1.5.1/lib/bundler/runtime.rb:72:in `block in require'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/bundler-1.5.1/lib/bundler/runtime.rb:61:in `each'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/bundler-1.5.1/lib/bundler/runtime.rb:61:in `require'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/bundler-1.5.1/lib/bundler.rb:131:in `require'
	from /home/XXXXX/Tameshi/config/application.rb:7:in `<top (required)>'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/commands.rb:43:in `require'
	from /home/XXXXX/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/railties-4.0.2/lib/rails/commands.rb:43:in `<top (required)>'
	from bin/rails:4:in `require'
	from bin/rails:4:in `<main>'


Gemflieを書き換えて、therubyracerのコメントアウトを外す

$ vi Gemfile

略
 # See https://github.com/sstephenson/execjs#readme for more supported runtimes
 gem 'therubyracer', platforms: :ruby
略

$ bundle install


これで行けたはず。
http://あなたのほすと
にアクセスして、railsの画面が見えたらOK

できるPRO Apache Webサーバー 改訂版 Version 2.4/2.2/2.0対応 (できるPROシリーズ)

できるPRO Apache Webサーバー 改訂版 Version 2.4/2.2/2.0対応 (できるPROシリーズ)

Apacheクックブック 第2版 ―Webサーバ管理者のためのレシピ集

Apacheクックブック 第2版 ―Webサーバ管理者のためのレシピ集