LoginSignup
9
10

More than 5 years have passed since last update.

Apache+WEBrickでとりあえずRailsアプリを動かすための設定

Last updated at Posted at 2016-01-17

はじめに

ローカル環境では、rails sでWEBrickサーバを立ち上げてlocalhost:3000にアクセスすれば良いのですが、VPSなどネットワーク先で開発している場合にはちょっと一手間かかります。

そこで、(おそらく最初から動いているであろう)ApacheとRails標準のアプリケーションサーバのWEBrickで、とりあえずRailsアプリが動くようにするための設定をまとめます。

環境情報

% inxi -SM
System:    Host: hakone.vps.sakura.ne.jp Kernel: 2.6.32-573.8.1.el6.x86_64 x86_64 (64 bit)
           Console: tty 5 Distro: CentOS release 6.7 (Final)
Machine:   System: Red Hat product: KVM v: RHEL 6.4.0 PC
           Mobo: N/A model: N/A Bios: Sea v: 0.5.1 date: 01/01/2007
% rake about
About your application's environment
Rails version      4.2.5
Ruby version       2.2.1-p85 (x86_64-linux)
RubyGems version   2.5.1
Rack version       1.6.4
% rails s --binding=127.0.0.1 --daemon
=> Booting WEBrick
=> Rails 4.2.5 application starting in development on http://127.0.0.1:3000
=> Run `rails server -h` for more startup options
% yum info httpd
インストール済みパッケージ
名前                : httpd
アーキテクチャ      : x86_64
バージョン          : 2.2.15
リリース            : 47.el6.centos.1
容量                : 2.9 M
リポジトリー        : installed
提供元リポジトリー  : updates
要約                : Apache HTTP Server
URL                 : http://httpd.apache.org/
ライセンス          : ASL 2.0
説明                : The Apache HTTP Server is a powerful, efficient, and extensible
                    : web server.

なお、Railsアプリを開発しているサーバのIPアドレスへDNSでドメインが設定されている、またはHostsなどで疑似的にドメインが設定できることを前提にしています。

Apache+WEBrickでRailsアプリを動かす設定

ApacheのVirtualHostProxy設定、WEBrickの起動オプションを設定することでアプリにアクセスできるようにします。

Apacheのバーチャルホストとプロキシ設定

YOUR_APP_DOMAINをあなたのアプリのドメイン名で置換し、Apacheの設定ファイルを作成します。

/etc/httpd/conf.d/railsapp.conf
NameVirtualHost *:80

##
# YOUR_APP_DOMAIN
#
<VirtualHost *:80>
    ServerName YOUR_APP_DOMAIN

    CustomLog logs/YOUR_APP_DOMAIN-access_log combined env=!no_log

    SetEnv RACK_ENV development

    ProxyRequests Off
    <Proxy *>
          Order deny,allow
          Allow from all
    </Proxy>
    ProxyPass / http://localhost:3000/
    ProxyPassReverse / http://localhost:3000/
</VirtualHost>

その後、Apacheを再起動します。

% sudo /etc/init.d/httpd restart

WEBrickの起動オプション

Apacheの設定と再起動が完了したら、RailsアプリのディレクトリでWEBrickを起動します。

% rails s --binding=127.0.0.1 --daemon

--bindingで待ち受けるIPアドレスを指定しています。また--daemonでWEBrickサーバをデーモンとして稼働させるよう指定しています。

ちなみに、デーモンとして起動したWEBrickを停止するには同様にRailsアプリのディレクトリで以下のコマンドを実行します。

% cat tmp/pids/server.pid | xargs kill

それでは、簡単に環境を設定して、楽しいRailsアプリ開発を!

9
10
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
9
10