LoginSignup
20
20

More than 5 years have passed since last update.

Apache で RubyOnRails のアプリケーションを公開してみる。

Last updated at Posted at 2014-11-30

Apache で RubyOnRails のアプリケーションを公開してみた。

参考にさせて頂いたページ

環境

  • さくらVPS
  • Apache 2.2.15
  • ruby 2.1.2
  • Rails 4.1.5
  • unicorn 4.8.3
  • git 1.7.1

Railsのアプリケーションリポジトリをクローンする。

今回は、Githubに公開されているアプリケーションリポジトリをgit cloneで取得することにする。
Linuxのディレクトリ役割について、調べると管理者用ローカルファイル、プログラム、ライブラリを置くことになっている/usr/local/にクローンすることにする。

# mkdir -p /usr/local/rails/App
# cd /usr/local/rails/App
# git clone http://test/App.git

ApacheにVirtualHostの設定をする。

yum install した場合は、下記のconfファイルを編集。
VirtualHostを設定して、ドメイン名を割り当ててみる。アクセスポートはデフォルトの80番を使用。(その他の設定は、デフォルトのまま)
【変更ファイル】/etc/httpd/conf/httpd.conf

Listen 80

NameVirtualHost *:80

<VirtualHost *:80>
    DocumentRoot /var/www/
    ServerName intheriv.com
    ErrorLog logs/intheriv.com-error_log
    CustomLog logs/intheriv.com-access_log common
</VirtualHost>

<VirtualHost *:80>
    DocumentRoot /usr/local/rails/App
    ServerName app.intheriv.com
    ErrorLog logs/app-error_log
    CustomLog logs/app-access_log common
</VirtualHost>

Appディレクトリのアクセス設定

書き込み可能にして、ページをHTTPのヘッダ情報にあったものをネゴってもらう。
アクセス制御はDeny,Allowの順番に評価し、とりあえずすべてに対してアクセス許可に。
【変更ファイル】/etc/httpd/conf/httpd.conf

<VirtualHost *:80>
    DocumentRoot /usr/local/rails/App/public
      <Directory "/usr/local/rails/App/public">
        AllowOverride All
        Options MultiViews
        Order deny,allow
        Allow from all
      </Directory>
    ServerName app.intheriv.com
    ErrorLog logs/app-error_log
    CustomLog logs/app-access_log common
</VirtualHost>

unicornの設定をする。

下記サイトから、unicornの設定ファイルのフォーマットを入手。

working_direcotryやlisten portなどのパスを環境に合わせて指定。
unicorn.conf.rbは、App/config/配下に保存。
【作成ファイル】/usr/local/rails/App/config/unicorn.conf.rb

working_directory "/usr/local/rails/App"

listen "/usr/local/rails/App/tmp/sockets/.unicorn.sock", :backlog => 64
listen 8080, :tcp_nopush => true

pid "/usr/local/rails/App/tmp/pids/unicorn.pid"

stderr_path "/usr/local/rails/App/log/unicorn.stderr.log"
stdout_path "/usr/local/rails/App/log/unicorn.stdout.log"

Apacheをunicornと連携させる。

ReverseProxyの設定をする。
ReverseProxy先でのアクセス制御はAppディレクトリと同じに。
動的なページ作成はunicornに任せて、precompileしたcssファイル,jsファイルは、Apacheが読み込むようにするためにpublic配下のassetsなどは、ReverseProxyの対象から除外。
【変更ファイル】/etc/httpd/conf/httpd.conf

<VirtualHost *:80>
    DocumentRoot /usr/local/rails/App/public
      <Directory "/usr/local/rails/App/public">
        AllowOverride All
        Options -MultiViews
        Order deny,allow
        Allow from all
      </Directory>
    ServerName app.intheriv.com
    ErrorLog logs/app-error_log
    CustomLog logs/app-access_log common

  <Proxy *>
    Order deny,allow
    Allow from all
  </Proxy>

  ProxyPass /assets/ !
  ProxyPass /fonts/ !
  ProxyPass /robots.txt !
  ProxyPass /favicon.ico !

  ProxyPass / http://localhost:8080/
  ProxyPassReverse / http://localhost:8080/
  ProxyPreserveHost on

</VirtualHost>

本番環境用のデータベースの作成および、cssファイル, jsファイルをprecompileする。

unicornが起動しなかったり、起動したのにページにcssファイル,jsファイルが反映されない様な場合はこれらが足りない。

# cd /usr/local/rails/App
# bundle exec rake db:migrate RAILS_ENV=production
# bundle exec rake assets:production

Apache, unicornを起動する。

unicorn起動時にunicornの設定ファイル、起動環境を本番に指定。デーモンで起動。

# service httpd start
# cd /usr/local/rails/App
# unicorn_rails -c config/unicorn.conf.rb -E production -D

ページにアクセス!

http://app.intheriv.com
20
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
20
20