Skip to content

setup production development

LuoKevin edited this page Mar 25, 2018 · 18 revisions

Linode 主機安裝與 Capistrano

Ubuntu 12.04 + Ruby 2.0.0 with RVM deployment

Update and upgrade system

  • sudo apt-get update
  • sudo apt-get upgrade

Install mysql

  • sudo apt-get install mysql-server
  • sudo apt-get install libmysqlclient-dev

Changing the Time Zone

sudo dpkg-reconfigure tzdata

Install Build Tools and Library for Ruby 2.0.0

sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline5 libyaml-dev

Create user: apps

sudo adduser apps
sudo passwd apps
sudo vim /etc/sudoers

make apps have sudo power

switch to apps

sudo su apps

Install ruby

curl -L https://get.rvm.io | bash -s stable --ruby=2.0.0
source /home/apps/.rvm/scripts/rvm

Install Bundler

gem install bundler

Install libcurl4-openssl-dev for passenger

sudo apt-get install libcurl4-openssl-dev

Install XML parser

sudo apt-get install libxml2 libxml2-dev libxslt1-dev

Nokogiri

gem install nokogiri

Install node.js

sudo apt-get install nodejs

或者手動編譯

https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager

apt-get install python g++ make
mkdir ~/nodejs && cd $_
wget -N http://nodejs.org/dist/node-latest.tar.gz
tar xzvf node-latest.tar.gz && cd `ls -rd node-v*`
./configure
make install

Install passenger

gem install passenger

Install nginx

複製底下的 output

which passenger-install-nginx-module

然後把 output 用 rvmsudo 執行

rvmsudo OUTPUT
# Choose "download, compile, and install Nginx for me"
# Accept defaults for any other questions it asks you

Install imagemagick

  • sudo apt-get remove imagemagick

  • sudo apt-get install libmagickcore-dev libmagickwand-dev

  • 下載 ImageMaigck

  • ./configure

  • make

  • sudo make install

然後 vim .bash_profile 加入

 LD_LIBRARY_PATH=/usr/local/lib

Install rmagick

gem install rmagick

Install git

sudo apt-get install git

setup a script to control Nginx

cd
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
sudo /etc/init.d/nginx restart

set up nginx.conf

vi /opt/nginx/conf/nginx.conf

insert following codes into the /opt/nginx/conf/nginx.conf

server {
      listen 80;
      server_name my_project.cc;
      root /home/apps/my_project/current/public;
      passenger_enabled on;
    }

create folder for the project

sudo su apps
cd ~
mkdir ~/my_project

set up deploy key for apps

ssh-keygen
more ~/.ssh/id_rsa.pub
pbcopy < ~/.ssh/id_rsa.pub

Add the SSH key to GitHub

  • browse https://github.com/rocodev/my_project
  • click "Admin"
  • click "Deploy keys"
  • click "Add deploy key"
  • input "Title"
  • Paste the key into the "Key" field
  • Click "Add key"
  • Confirm the action by entering your GitHub password

Authenticate Github

ssh -T git@github.com

Set up authorized keys for developers

vim /home/apps/.ssh/authorized_keys

Paste the ssh public keys into the "/home/apps/.ssh/authorized_keys" file

(複製本機的id_rsa.pub貼在server的authorized_keys檔案內)

Set up Capistrano

https://gist.github.com/xdite/d1cc3fe7159c336dca68

vi /home/apps/my_project/shared/config/database.yml

production:
  adapter: mysql2
  encoding: utf8
  reconnect: false
  database: my_project
  pool: 5
  username: root
  password: '1xsedqwdqd'

create "/my_project/Capfile"

Paste following codes into the "/my_project/Capfile" file

load 'deploy/assets'
Dir['vendor/gems/*/recipes/*.rb','vendor/plugins/*/recipes/*.rb'].each { |plugin| load(plugin) }
load 'config/deploy' # remove this line to skip loading any of the default tasks

create "/config/deploy.rb"

Paste following codes into the "/config/deploy.rb" file

# -*- encoding : utf-8 -*-
require 'capistrano/ext/multistage'
require 'bundler/capistrano' #Using bundler with Capistrano
require 'cape'

set :stages, %w(staging production)
set :default_stage, "production"


Cape do
  mirror_rake_tasks :dev
end

create /config/deploy/production.rb

Paste following codes into the "/config/deploy/production.rb" file

# -*- encoding : utf-8 -*-
require 'rvm-capistrano'
default_environment["PATH"] = "/opt/ruby/bin:/usr/local/bin:/usr/bin:/bin"

set :application, "my_project"
set :repository,  "git@github.com:rocodev/my_project.git"
set :deploy_to, "/home/apps/my_project"

set :branch, "master"
set :scm, :git

set :user, "apps"
set :group, "apps"

set :deploy_to, "/home/apps/#{application}"
set :runner, "apps"
set :deploy_via, :remote_cache
set :git_shallow_clone, 1

set :use_sudo, false
set :rvm_ruby_string, '2.0.0'

role :web, "my_project.cc"                          # Your HTTP server, Apache/etc
role :app, "my_project.cc"                         # This may be the same as your `Web` server
role :db,  "my_project.cc"   , :primary => true # This is where Rails migrations will run

set :deploy_env, "production"
set :rails_env, "production"
set :scm_verbose, true
set :use_sudo, false


namespace :deploy do

  desc "Restart passenger process"
  task :restart, :roles => [:web], :except => { :no_release => true } do
    run "touch #{current_path}/tmp/restart.txt"
  end
end


namespace :my_tasks do
  task :symlink, :roles => [:web] do
    run "mkdir -p #{deploy_to}/shared/log"
    run "mkdir -p #{deploy_to}/shared/pids"
    
    symlink_hash = {
      "#{shared_path}/config/database.yml"   => "#{release_path}/config/database.yml",
    }

    symlink_hash.each do |source, target|
      run "ln -sf #{source} #{target}"
    end
  end

end



namespace :remote_rake do
  desc "Run a task on remote servers, ex: cap staging rake:invoke task=cache:clear"
  task :invoke do
    run "cd #{deploy_to}/current; RAILS_ENV=#{rails_env} bundle exec rake #{ENV['task']}"
  end
end

after "deploy:finalize_update", "my_tasks:symlink"

add 'rvm-capistrano' to Gemfile Paste following codes into the "/Gemfile" file, and bundle install

  gem "rvm-capistrano"
  ...

  group :development do
    gem "capistrano", "2.15.4"
    gem "capistrano-ext"
    gem 'capistrano-unicorn', :require => false
    gem "cape"
  end

initial deployment cap deploy:setup

deploy cap deploy

remote db migrate cap deploy:migrate

You can’t perform that action at this time.