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

Vagrant + Ruby on Rails + Mysql 環境構築

目的

  • ローカル環境で Rails 環境を起動させて開発を始めたい時に気軽に起動できるようにする

事前準備(適宜ご用意)

実施

Vagrant ファイルの用意

$ vagrant init bento/centos-6.7
$ vim Vagrantfile
===== 下記に書き換え =====
Vagrant.configure(2) do |config|
  config.vm.box = "bento/centos-6.7"
  config.vm.network :"forwarded_port", guest: 3000, host: 30000
  config.vm.provision "shell", path: "provision.sh"
end
==========================

Provision 用のコマンド実行スクリプトの用意

$ vim provision.sh
===== 下記に書き換え =====
#!/usr/bin/env bash
yum -y install git
yum -y install gcc-c++ glibc-headers openssl-devel readline libyaml-devel readline-devel zlib zlib-devel libxml2 libxslt libxml2-devel libxslt-devel
chmod 777 /var/local/
git clone https://github.com/sstephenson/rbenv.git /usr/local/rbenv
chmod -R 777 /var/local/
echo 'export RBENV_ROOT="/usr/local/rbenv"' >> /etc/profile
git clone https://github.com/sstephenson/ruby-build.git /usr/local/rbenv/plugins/ruby-build
chmod -R 777 /usr/local/rbenv/
/usr/local/rbenv/bin/rbenv install -v 2.3.1
/usr/local/rbenv/bin/rbenv rehash
/usr/local/rbenv/bin/rbenv global 2.3.1
echo 'export PATH="${RBENV_ROOT}/bin:${PATH}"' >> /etc/profile
echo 'eval "$(rbenv init -)"' >> /etc/profile
source /etc/profile
cp -p /usr/share/zoneinfo/Japan /etc/localtime
/usr/local/rbenv/shims/gem update --system
/usr/local/rbenv/shims/gem install nokogiri -- --use-system-libraries
/usr/local/rbenv/shims/gem install rails -v 4.2.6
yum install -y sqlite-devel
yum -y install http://dev.mysql.com/get/mysql-community-release-el6-5.noarch.rpm
yum -y install mysql-community-server
chkconfig mysqld on
service mysqld start
yum -y install mysql-devel
==========================

環境構築 + ログイン

$ vagrant up
$ vagrant ssh

ログインして rails などインストールできてない場合
$ vagrant provision
を実行
  • あとはログインして、rails 開発したいように設定していってください。

参考図書