在linux平台,使用capistrano, rvm, passenger来部署 rails 项目

1 篇文章 0 订阅
1 篇文章 0 订阅

1)安装capistrano, rvm, passenger

1.rvm的安装:可以参考(http://beginrescueend.com/rvm/install/

安装给单个用户:

bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
如果你遇到无法安装,或者抱怨没有证书,像是下面的错误信息:

curl: (60) SSL certificate problem, verify that the CA cert is OK. Details:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed
More details here: http://curl.haxx.se/docs/sslcerts.html

你需要使用下面的命令:

bash < <(curl -sk https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )
如果还是有相同的问题。你可以在当前用户的根目录下创建 '.curlrc'文件,里面写上 insecure

#~/.curlrc
insecure
rvm安装好以后安装需要的ruby版本
rvm install 1.9.2
这样rvm先搞一段落了。

2.Capistrano,passenger的安装都放在项目的Gemfile文件里面

gem 'passenger'
group :development do
  gem 'capistrano'#in production environment, don't need this gem package.
end

passenger的gem包安装好以后

运行passenger-install-apache2-module 我们用apache做我们的http服务器。

按提示一步一步做,最后会生成一段文字,叫你加入到apache的配置文件里。

类似:

   LoadModule passenger_module /rvm_path/gems/rvm_gemset/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
   PassengerRoot /rvm_path/gems/rvm_gemset/gems/passenger-3.0.11
   PassengerRuby /rvm_path/wrappers/rvm_gemset/ruby

按提示,把这段加到apache的配置文件中去。

apache的配置文件一般在/etc/httpd/conf/httpd.conf

然后你要配置一下,你这个项目的服务信息,添加一个xxx.conf的文件到/etc/httpd/conf.d

Listen port

<VirtualHost *:port>
    ServerName www.example.com
    DocumentRoot /project_root/public
    <Directory /project_root/public>
        AllowOverride all
        Options Indexes -MultiViews
        RailsEnv production#or development, the environment name
    </Directory>
</VirtualHost>
这时passenger配置也告一段落。

1,2都是production机器上安装配置

3)Capistrano的安装和配置。

由gem来安装,在开发机器上,到项目的根目录下运行

capify . 
这个命令会创建2个文件,

Capfile

config/deploy.rb

我们要做的是编辑config/deploy.rb这个文件

# RVM bootstrap
$:.unshift(File.expand_path("~/.rvm/lib"))
require 'rvm/capistrano'
set :rvm_ruby_string, 'ruby-1.9.2-p290@rails313'#这个值是你要用rvm的gemset。名字要和系统里有的要一样。
set :rvm_type, :user # Don't use system-wide RVM
#这个不能少,否则部署不了。

#下面的部署信息,应该都很清楚吧。
#some parameter setting
  set :host,server_name#set your server name
  set :port_number, server_port#set your server port
  set :rails_env, rails_env
  
  # main details
  set :application, application_name
  role :app, "#{host}"
  role :web, "#{host}"
  role :db,  "#{host}", :primary => true

  # server details
#  default_run_options[:pty] = true
  set :use_sudo, false
  set :user, user_name
  if user_pw.nil?
    ssh_options[:keys] = %w(./lib/dsa_ssh_key)
  else
    set :password, user_pw
  end
  set :deploy_to, "#{deploy}/#{application}"#set your application path
  
  
  # repo details use svn to manage code
  set :repository,  svn_repo
  set :scm, :subversion
  set :deploy_via, :export
  set :scm_username, 'username'
  set :scm_password, 'password'
  set :runner, nil
  
  # runtime dependencies
  depend :remote, :command, "monit"
  depend :local, :command, "svn"
  depend :remote, :gem, "bundler"

  # tasks
  namespace :deploy do
    task :start, :roles => :app do
      run "touch #{current_path}/tmp/restart.txt"
    end
  
    task :stop, :roles => :app do
      # Do nothing.
    end
  
    desc "Restart Application"
    task :restart, :roles => :app do
      run "touch #{current_path}/tmp/restart.txt"
    end
  end
  
  namespace :bundler do
    desc "Install for production"
    task :install, :roles => :app do
      run "cd #{release_path} && bundle install"
    end
  
  end
  after 'deploy:update_code', 'bundler:install'

部署文件搞定了。

开始部署。

cap deploy:setup
初始化一些目录,在production服务器上。

cap deploy 
可能出现的问题,如果用mysql做数据库的话,可能你有些跟mysql相关的gem包没有安装,根据提示安装就可以了。

cap deploy:migrate
数据迁移,可能出现的问题,你的production数据库并没有创建,所以在服务器上先把数据库创建好了。

Capistrano的配置也搞一段落。

这时候,会有一些问题出现。

1)如果在项目的根目录下存在'.rvmrc'文件

这个文件的作用就是每次进去项目的根目录,自动切换到对应的rvm gemset环境

文件的内容:

#!/usr/bin/env bash
ruby_string="ruby-1.9.2-p290"
gemset_name="rails313"
if rvm list strings | grep -q "${ruby_string}" ; then
  # Load or create the specified environment
  if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
    && -s "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}" ]] ; then
    \. "${rvm_path:-$HOME/.rvm}/environments/${ruby_string}@${gemset_name}"
  else
    rvm --create  "${ruby_string}@${gemset_name}"
  fi
else
  # Notify the user to install the desired interpreter before proceeding.
  echo "${ruby_string} was not found, please run 'rvm install ${ruby_string}' and then cd back into the project directory."
fi
这个文件,在进入根目录后会自动执行,不过需要添加信任,

所以我们要在用户的根目录下添加 .rvmrc 文件

内容:

rvm_trust_rvmrcs_flag=1

如果不添加这个文件,访问服务器的时候passenger会报个错误。说是.rvmrc需要添加信任。


2)因为使用rvm所以要在项目的config目录下添加一个load的配置文件

名字:setup_load_paths.rb

if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm')
  begin
    rvm_path     = File.dirname(File.dirname(ENV['MY_RUBY_HOME']))
    rvm_lib_path = File.join(rvm_path, 'lib')
    $LOAD_PATH.unshift rvm_lib_path
    require 'rvm'
    RVM.use_from_path! File.dirname(File.dirname(__FILE__))
  rescue LoadError
    # RVM is unavailable at this point.
    raise "RVM ruby lib is currently unavailable."
  end
end

# Pick the lines for your version of Bundler
# If you're not using Bundler at all, remove all of them

# Require Bundler 1.0 
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__))
require 'bundler/setup'

这样基本上所有的配置都全了,可以访问服务器了。如果有错误,应该是一些gem包没有装全,安装一下就好。参考其他人写的一些配置信息,我做了一下总结,和可能出现的错误。

enjoy it!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值