关于Rails和Apache的协作

我正在使用Vagrant、VirtualBox和CentOS6进行学习,目的是通过apache使rails能够运行。

問題の投稿はありましたが、うまく機能したため、修正された内容に変更します。
エラーの原因は、passengerをsudoでインストールしたため、systemのruby 1.8.7に対応したapacheがビルドされてしまったことでした。

所有的操作。

> vagrant box add centos664 http://developer.nrel.gov/downloads/vagrant-boxes/CentOS-6.4-x86_64-v20130731.box
> mkdir centos6
> cd centos6
> vagrant init centos664

Vagrantfileを編集
# using a specific IP.
 config.vm.network :private_network, ip: "192.168.33.30"

> vagrant up
> vagrant ssh

更新yum并安装vim。

$ sudo yum -y update
$ sudo yum -y install vim

将文本转为日语

$ sudo vim /etc/sysconfig/i18n

LANG="ja_JP.UTF-8"

$ sudo reboot

创建具有root权限的用户

$ sudo usermod -G wheel vagrant
$ sudo visudo

## Allows people in group wheel to run all commands

%wheel  ALL=(ALL)       ALL
wheelグループを有効化する

安装apache

$ sudo yum -y install httpd
$ sudo service httpd start
$ sudo chkconfig httpd on

安装必要的工具

$ sudo yum -y install gcc gcc-c++ openssl-devel zlib-devel make patch git gettext perl rpm-build
$ sudo yum -y install httpd-devel curl-devel ncurses-devel gdbm-devel readline-devel sqlite-devel ruby-devel

安装rbenv

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ exec $SHELL -l

$ git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
$ sudo .rbenv/plugins/ruby-build/install.sh

安装Ruby 1.9.3

$ rbenv install -v 1.9.3-p484
$ rbenv rehash

将Ruby 1.9.3设置为全局。

$ rbenv global 1.9.3-p484
$ rbenv versions
$ sudo chmod o+x /home/vagrant

乘客的安装

$ gem install passenger --no-ri --no-rdoc -V
$ rbenv rehash

在Apache中安装Passenger模块。

$ passenger-install-apache2-module
The Apache 2 module was successfully installed.

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

   LoadModule passenger_module /home/vagrant/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25/buildout/apache2/mod_passenger.so
   PassengerRoot /home/vagrant/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25
   PassengerDefaultRuby /home/vagrant/.rbenv/versions/1.9.3-p484/bin/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

Press ENTER to continue.


--------------------------------------------
Deploying a Ruby on Rails application: an example

Suppose you have a Rails 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/minao/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25/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.

首先,执行rbenv rehash。

$ rbenv rehash

创建乘客配置文件.passenger.conf

$ sudo vim /etc/httpd/conf.d/passenger.conf

请将以下内容写入

LoadModule passenger_module /home/vagrant/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25/buildout/apache2/mod_passenger.so
PassengerRoot /home/vagrant/.rbenv/versions/1.9.3-p484/lib/ruby/gems/1.9.1/gems/passenger-4.0.25
PassengerDefaultRuby /home/vagrant/.rbenv/versions/1.9.3-p484/bin/ruby

Header always unset "X-Powered-By"
Header always unset "X-Rack-Cache"
Header always unset "X-Content-Digest"
Header always unset "X-Runtime"

修改httpd.conf

$ sudo cp /etc/httpd/conf/httpd.conf /etc/httpd/conf/httpd.conf.org
$ sudo vim /etc/httpd/conf/httpd.conf

修改项目

# 44行目 OS情報を表示しないようにする
ServerTokens Prod

#277行目
ServerName 192.168.33.30:80

# 331行目 -(ハイフン)を追加
Options -Indexes FollowSymLinks

# 536行目 OS情報を表示しないようにする
ServerSignature Off

# キャッシュ強化(以下の行を追加)
# Apache Expire Header
ExpiresActive On
ExpiresByType application/javascript "access plus 1 days"
ExpiresByType application/x-javascript "access plus 1 days"
ExpiresByType text/javascript "access plus 1 days"
ExpiresByType text/css "access plus 1 days"
ExpiresByType image/jpeg "access plus 3 days"
ExpiresByType image/png "access plus 3 days"
ExpiresByType image/gif "access plus 3 days"
ExpiresByType image/x-icon "access plus 3 days"

# ETags
FileETag MTime Size

检查Apache配置

$ apachectl configtest
> Syntax OK

启动Apache并设置自动启动。

$ sudo service httpd restart
$ sudo chkconfig httpd on

$ sudo chkconfig --list httpd
httpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off

使用Bundler进行安装

$ sudo gem install bundler --no-ri --no-rdoc -V
$ rbenv rehash

安装Rails

$ gem install rails -v 3.2.15 --no-ri --no-rdoc -V
$ rbenv rehash

用Rails生成一个示例

$ mkdir rails_app
$ cd rails_app
$ rails new sample

修改Gemfile

$ cd sample
$ vim Gemfile

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


$ bundle install
$ rbenv rehash

使用 scaffold 来创建一个简单的 MVC 示例。

$ rails g scaffold foo name:string
$ rake db:migrate

在httpd.conf文件中添加VirtualHost配置

$ sudo vim /etc/httpd/conf/httpd.conf

<VirtualHost *:80>
      RailsEnv development
      PassengerEnabled on
      ServerName 192.168.33.30
      # !!! Be sure to point DocumentRoot to 'public'!
      DocumentRoot /home/vagrant/rails_app/sample/public
      <Directory /home/vagrant/rails_app/sample/public>
         # This relaxes Apache security settings.
         AllowOverride all
         # MultiViews must be turned off.
         Options -MultiViews
      </Directory>
</VirtualHost>

Apache配置的检查

$ apachectl configtest
> Syntax OK
$ sudo service httpd restart

将routes.rb文件进行修改,使其从public/index.html调用foos。

$ vim config/routes.rb
  root :to => 'foos#index'

$ mv public/index.html public/index.html.bak

暂时停止防火墙

$ sudo service iptables stop
$ sudo chkconfig iptables off
$ sudo chkconfig --list iptables
iptables        0:off   1:off   2:off   3:off   4:off   5:off   6:off

修改Rails应用程序下的执行权限。

$ cd ~
$ chmod -R o+x rails_app

访问 http://192.168.33.30 后,成功运行。

bannerAds