Skip to content

Instantly share code, notes, and snippets.

@defp
Created September 24, 2012 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save defp/3774965 to your computer and use it in GitHub Desktop.
Save defp/3774965 to your computer and use it in GitHub Desktop.
deploy with capistrano

准备工作

byobu 基于screen或tmux的终端工作

sudo apt-get install byobu curl

在终端启动

byobu  # F6 可以让进程在后台运行`

安装 rvm

curl -L https://get.rvm.io | sudo bash -s stable
source /etc/profile.d/rvm.sh

添加rails用户

sudo useradd -m -G  rvm rails

将rails用户加到sudo用户组(可选)

sudo gpasswd -a rails sudo

更改密码

passwd rails

安装依赖

sudo apt-get install -y build-essential openssl curl libcurl3-dev libreadline6 libreadline6-dev git zlib1g zlib1g-dev libssl-dev libyaml-dev libxml2-dev libxslt-dev autoconf automake libtool imagemagick libmagickwand-dev libpcre3-dev libsqlite3-dev

也可以执行,会列出需要的包

rvm requirements 

example

sudo apt-get install build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

安装curl openssl 库

sudo apt-get install libcurl4-openssl-dev libopenssl-ruby libssl-dev

在rails这个用户下执行操作-安装ruby

rvmsudo rvm install ruby

源代码安装

rvmsudo rvm install 1.9.3 --disable-binary

设置ruby版本

rvm use --default 1.9.3

安装Nginx + Passenger

gem install passenger

执行 passenger nginx 模块的安装脚本

rvmsudo passenger-install-nginx-module

添加一个 nginx 的启动脚本

cd /etc/init.d
sudo wget https://raw.github.com/chloerei/nginx-init-ubuntu-passenger/master/nginx
sudo update-rc.d nginx defaults
sudo chmod +x nginx
sudo service nginx start
# Deploy with Capistrano
gem 'capistrano'
# 这个gem可以设置多个部署环境
gem 'capistrano-ext'
# 添加rvm的一些支持
gem 'rvm-capistrano'
load 'deploy'
# Uncomment if you are using Rails' asset pipeline
load 'deploy/assets'
load 'config/deploy' # remove this line to skip loading any of the default tasks
#config/deploy.rb
require 'bundler/capistrano'
set :use_sudo, false
set :rvm_type, :system
require "rvm/capistrano" # Load RVM's capistrano plugin.
set :stages, %w(staging production)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
set :application, "ziya-news"
# ssh to the deploy server
default_run_options[:pty] = true
# setup scm:
set :repository, "git@bitbucket.org:larryzhao/ziya-news.git"
set :deploy_via, :remote_cache
set :scm_username, "larry"
set :scm, :git
set :scm_verbose, "true"
ssh_options[:forward_agent] = true
# set path
set(:releases_path) { File.join(deploy_to, version_dir) }
set(:shared_path) { File.join(deploy_to, shared_dir) }
set(:current_path) { File.join(deploy_to, current_dir) }
set(:release_path) { File.join(releases_path, release_name) }
# set gems
set :bundle_without, [:development,:test]
namespace :rvm do
task :trust_rvmrc do
run "rvm rvmrc trust #{current_release}"
end
end
namespace :deploy do
task :start do ; end
task :stop do ; end
task :restart, :roles => :app, :except => { :no_release => true } do
run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
end
task :db_create do
run "cd #{release_path}; bundle exec rake db:create RAILS_ENV=#{rails_env}"
end
end
#config/deploy/staging.rb
server "chicago.baijii.com", :app, :web, :db, :primary => true
set :use_sudo, false
set :branch, "master"
set :rails_env, "production"
set :rvm_ruby_string, 'ruby-1.9.2-p320@ziya-news'
set :deploy_to, "/opt/app/deploy/#{application}"
after "bundle:install", "deploy:db_create"
after "deploy:db_create", "deploy:migrate"
after "deploy:create_symlink", "deploy:cleanup"
#config/deploy/production.rb
server "chicago.baijii.com", :app, :web, :db, :primary => true
set :use_sudo, false
set :branch, "master"
set :rails_env, "staging"
set :rvm_ruby_string, 'ruby-1.9.2-p320@ziya-news'
set :deploy_to, "/opt/app/deploy/#{application}"
after "bundle:install", "deploy:db_create"
after "deploy:db_create", "deploy:migrate"
after "deploy:create_symlink", "deploy:cleanup"
## Can't install RMagick 2.13.1. Can't find Magick-config
sudo apt-get install libmagickwand-dev
## nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
passenger_root /usr/local/rvm/gems/ruby-1.9.3-p194/gems/passenger-3.0.17;
passenger_ruby /usr/local/rvm/wrappers/ruby-1.9.3-p194/ruby;
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80 default_server;
server_name vps.sdutlinux.org;
root /home/rails/sdutlinux/current/public;
passenger_enabled on;
rails_env production;
#charset koi8-r;
#access_log logs/host.access.log main;
#location / {
# root html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
include /opt/nginx/conf/sites/*;
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443;
# server_name localhost;
# ssl on;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_timeout 5m;
# ssl_protocols SSLv2 SSLv3 TLSv1;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
## sdutlinux.org example
server {
client_max_body_size 20M;
listen 80;
server_name sdutlinux.org;
root /home/rails/sdutlinux/current/public;
rails_env production;
passenger_enabled on;
#access_log logs/host.access.log main;
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
gzip_static on;
expires 1y;
add_header Cache-Control public;
#root html;
#index index.html index.htm;
}
#location / {
# root html;
# index index.html index.htm;
#}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment