Happy Growth Engineer

成長と幸せな働き方の両立をするために役に立つ

Apache + Passenger + Ruby on Rails 環境構築

目的

  • Apache 上で Passenger を利用して、 Ruby on Rails アプリケーションを動かす
  • 少し目的とはそれますが、AWS の ELB でバランシングできるよう設定する

環境

事前準備

導入

Passenger

Gemfile に追記
gem 'passenger'
インストール
$ bundle install

Apache

インストール
$ cd /etc/yum.repos.d
$ sudo wget https://repos.fedorapeople.org/repos/jkaluza/httpd24/epel-httpd24.repo
$ sudo yum install --enablerepo=epel-httpd24 httpd24

$ sudo yum install libcurl-devel httpd httpd-devel apr-devel apr-util-devel
Apache 用モジュールインストール
$ passenger-install-apache2-module
/etc/httpd/conf.d/zzz_application.conf 作成
<VirtualHost *:80>
  ServerName [適切なホスト名]
  DocumentRoot /var/www/html/application_rails/public
  ErrorLog /var/log/httpd/application_error.log
  TransferLog /var/log/httpd/application_access.log
  <Directory /var/www/html/application_rails/public>
   AllowOverride all
   Options -MultiViews
   Order allow,deny
   allow from all
  </Directory>
</VirtualHost>

<VirtualHost *:8080>
  <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteLog /var/log/httpd/rewrite.log
    RewriteLogLevel 1
    RewriteCond %{HTTP:X-Forwarded-Port} !^443$
    RewriteCond %{HTTP_USER_AGENT} !^ELB-HealthChecker
    RewriteRule ^(.*)?$ https://%{HTTP_HOST}$1 [R=301,L]
  </IfModule>
</VirtualHost>
/etc/httpd/conf/httpd.conf 追記
ServerName localhost:80
Listen 8080 -> ここの追記忘れずに

LoadModule passenger_module /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/passenger-5.0.29/buildout/apache2/mod_passenger.so
<IfModule mod_passenger.c>
  PassengerRoot /usr/local/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/passenger-5.0.29
  PassengerDefaultRuby /usr/local/rbenv/versions/2.3.1/bin/ruby
</IfModule>

起動

$ sudo /etc/init.d/httpd start

番外編

  • もしAWS イメージとかで起動とか可能性があるなら下記を実施しておくのは良いかもしれません
  • 起動する時に Passenger のプロセスが邪魔して、httpd が起動しない
$ sudo chkconfig passenger off
$ sudo chkconfig httpd on