貌似Heroku的herokuapp.com在国内访问既慢又不稳定,所以不得已需要自己搭建一台production服务器。我们的目标是和Heroku类似,直接能够从开发环境部署并运行。虽然貌似文档很多,但是还是有很多问题。
------------------------------------------------------
警告:似乎如果目录权限配置不正确一下设置会导致rails 一直报404
安装RVM
#我是在root下装的全局rvm,使用基于账户的rvm请自行修改对应设置
curl -L get.rvm.io | bash -s stable
#add user to rvm group
vi /etc/group
source /etc/profile.d/rvm.sh
#成功的话跑这个会说rvm是function
type rvm |head n1
安装ruby
apt-get install build-essential bison openssl curl git-core zlib1g zlib1g-dev libssl-dev
rvm install 1.9.2
rvm use 1.9.2 --default
安装passenger
rvm 1.9.2 --passenger
gem install passenger
rvmsudo passenger-install-nginx-module
会提示你下载安装nginx,按照配置来即可
装好以后需要做2件事,1,搞到nginx启动脚本
git clone git://github.com/jnstq/rails-nginx-passenger-ubuntu.git
sudo mv rails-nginx-passenger-ubuntu/nginx/nginx /etc/init.d/nginx
sudo chown root:root /etc/init.d/nginx
server {
listen 80; #the server will be running on this port
server_name www.yourhost.com;
root /home/deployer/your_rails_project/public; # <--- be sure to point to 'public'!
passenger_enabled on;
}
Postgresql 安装配置
安装
sudo apt-get install postgresql postgresql-client libpq-dev
# libpq-dev is required for gem 'pq'
确定运行
netstat -atn | grep -v tcp6 | grep 5432
sudo -u postgres createuser --superuser <username>
sudo -u postgres psql postgres
\password <username>
建数据库
create database <database_name>;
grant all privileges on database <database_name> to <user_name>;
修改配置文件使得本地连接ok
vi /etc/postgresql/9.1/main/postgresql.conf
#uncomment listen_addresses = 'localhost'
/etc/postgresql/9.1/main/pg_hba.conf:
# "local" is for Unix domain socket connections only
local all all md5
给rails添加Capistrano
在服务器上找个用来签入的库的地方
mkdir -p ~/gitrepo/projectname.git
cd ~/gitrepo/projectname.git
git --bare init
配置一个服务器上的本地的ssh key
# 检测是否有文件,如果没有的话创建一个dsa的key
$test -e ~/.ssh/id_dsa.pub || ssh-keygen -t dsa
# 设置本服务器接收使用id_dsa的公钥连接
$ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys2
#这一句其实很重要,因为后面实际上是本地连接这个库拖到部署目录上,所以其实有一个本地ssh本地的过程
注意,这个弄完以后你可以在服务器本地ssh -T ssh://username@server/repo_path/projectname.git
之后yes添加key, 否则会出现key verification failed
/webhome/project/current/public
去掉Gemfile的
gem 'capistrano'
的注释
到开发机
把程序添加到git
cd rails_app_dir
git init
git add .
git commit -m 'init commit'
bundle install
bundle pack
git add Gemfile.lock vendor/cache
git commit -m 'bundle gems'
添加远程库
git remote add <repo_name> ssh://user@host/dir_to_repo_in_the_server
#这个例子里就是
#ssh://user@host/~/gitrepo/projectname.git
推上库
git push <repo_name> master
#这里出错大多是因为前面key没配好,或者你忘了把你的公钥传到服务器上去变成authorized_keys2
远程部署
capify .
编辑./config/deploy.rb
配置如下:
set :user, '<username>'
set :domain, '<host_name>'
set :application, "<app_name>"
set :repository, "#{user}@#{domain}:/opt/webapp/project_depo/depo.git"
set :deploy_to, "<dir_setted_in_the_nginx>" #in this example /webhome/project
set :scm, "git"
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, domain # Your HTTP server, Apache/etc
role :app, domain # This may be the same as your `Web` server
role :db, domain, :primary => true # This is where Rails migrations will run
# role :db, "your slave db-server here"
set :deploy_via, :remote_cache # this let the server clone from the git repo in the server
set :branch, 'master'
set :scm_verbose, true
set :use_sudo, false
require 'rvm/capistrano' # this is useful when you use rvm
set :rvm_ruby_string, "1.9.2"
set :rvm_type, :system # if you are not use system wide rvm then change it to :user
# if you're still using the script/reaper helper you will need
# these http://github.com/rails/irs_process_scripts
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :seed do
run "cd #{current_path}; rake db:seed RAILS_ENV=production"
end
end
after "deploy:update_code", :bundle_install
desc 'install prerequisites'
task :bundle_install, :roles => :app do
run "cd #{release_path} && bundle install"
end
------------------------------------------------------------------------------------------------------------------
4/15更新更好的版本,无需手动配置自动编译assets
require 'rvm/capistrano' #add rvm integration
require 'bundler/capistrano' # add bundler integration
load 'deploy/assets' # make sure assets are precompiled
set :user, '<username>'
set :domain, '<domain>'
set :application, "<appname>"
set :repository, "#{user}@#{domain}:/repopath/repo.git"
set :deploy_to, "/opt/webpath"
set :scm, "git"
set :branch, 'master'
# Or: `accurev`, `bzr`, `cvs`, `darcs`, `git`, `mercurial`, `perforce`, `subversion` or `none`
role :web, domain # Your HTTP server, Apache/etc
role :app, domain # This may be the same as your `Web` server
role :db, domain, :primary => true # This is where Rails migrations will run
# role :db, "your slave db-server here"
set :deploy_via, :remote_cache
set :scm_verbose, true
set :use_sudo, false
set :bundle_dir, ""
set :bundle_flags, ""
# If you are using Passenger mod_rails uncomment this:
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :seed do
run "cd #{current_path}; rake db:seed RAILS_ENV=production"
end
end
cap deploy:setup
cap deploy:check
cap deploy:migrations
#如果最后一步有错试试
#cap deploy:cold
git add .
git commit -m 'comments'
git push <repo_name>
cap deploy
# 如果需要取消部署
# cap deploy:rollback
注意这一步如果出现 .../public/image 文件没找到等错误需要在deploy.rb里设置
set :normalize_asset_timestamps, false
bundle exec rake assets:precompile RAILS_ENV=production
参考
http://web-l.nl/posts/5 :这是一篇非常好的Apache + Passenger的文章