转自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
装好以后需要做2件事,1,搞到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>;
- 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
- # 检测是否有文件,如果没有的话创建一个dsa的key
- $test -e ~/.ssh/id_dsa.pub || ssh-keygen -t dsa
- # 设置本服务器接收使用id_dsa的公钥连接
- $ cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys2
- #这一句其实很重要,因为后面实际上是本地连接这个库拖到部署目录上,所以其实有一个本地ssh本地的过程
- /webhome/project/current/public
- 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 .
配置如下:
- 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
- set :normalize_asset_timestamps, false
- bundle exec rake assets:precompile RAILS_ENV=production