您的位置:首页 > 运维架构 > Nginx

Rails Production Server @ Nginx + Postgresql + Ubuntu 使用git远程部署 配置

2012-07-26 11:02 841 查看
转自linux公社

貌似Heroku在国内访问既慢又不稳定,所以不得已需要自己搭建一台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  

2,修改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'  

确保如下信息,否则rails会出错

/etc/postgresql/9.1/main/pg_hba.conf:  
# "local" is for Unix domain socket connections only   
local   all         all                               md5  

配置好数据库以后记得在config/database.yml里修改对应配置


给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本地的过程  

tips,记得修改一下nginx的目录,因为Capistrano会添加一个current作为本项目的当前部署版本的根目录,所以应该指向类似于

/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  

准备部署

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  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息