Created
May 8, 2012 06:41
-
-
Save tehpeh/2633079 to your computer and use it in GitHub Desktop.
RoR Deploy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
General steps for a RoR deployment on linux. | |
For Ubuntu, see ubuntu-deploy.txt | |
For CentOS, see centos-deploy.txt | |
Information: | |
------------ | |
- users | |
cat /etc/passwd | |
- groups | |
cat /etc/group | |
- groups for user | |
id [user] | |
- sudo access | |
visudo | |
- root access | |
sudo -i | |
Add user | |
-------- | |
Debian derivatives: | |
- add user | |
adduser [user] | |
adduser [user] sudo | |
RHEL derivatives: | |
- add group | |
groupadd [group] | |
- add user | |
useradd [user] -g [default-group] -G [additional],[groups] {-s /bin/bash -d /home/[user] -m} | |
passwd [user] | |
Disable root: | |
----- | |
- enable: | |
sudo passwd | |
- disable (lock): | |
sudo passwd -l root | |
Disable PW ssh: | |
---------------- | |
- copy over public key first, then: | |
ssh-keygen | |
cat id_rsa.pub > .ssh/authorized_keys | |
chmod 600 .ssh/authorized_keys | |
cp /etc/ssh/sshd_config /etc/ssh/sshd_config.backup | |
vim /etc/ssh/sshd_config | |
- change to: | |
Protocol 2 | |
PasswordAuthentication no | |
PermitRootLogin no (if root account exists) | |
AllowUsers [user] | |
GSSAPIAuthentication no | |
GSSAPICleanupCredentials no | |
- ubuntu | |
/etc/init.d/ssh restart | |
- centos | |
/etc/init.d/sshd restart | |
.gemrc | |
------ | |
:update_sources: true | |
:bulk_threshold: 1000 | |
:sources: | |
- http://rubygems.org/ | |
- http://gems.github.com | |
gem: --no-ri --no-rdoc | |
:backtrace: false | |
:benchmark: false | |
:verbose: true | |
.vimrc | |
------ | |
syntax on | |
set expandtab | |
set shiftwidth=2 | |
set softtabstop=2 | |
set hlsearch | |
.rvmrc | |
------ | |
rvm_make_flags="-j 2" ### no of cpus, probably 1 | |
rvm_archflags="-arch x86_64" | |
rvm_pretty_print_flag="1" | |
rvm system-wide install: | |
-------------- | |
bash < <(curl -B http://rvm.beginrescueend.com/install/rvm) | |
- /root/.bashrc and /home/[user]/.bashrc and /etc/skel/.bashrc: | |
- if there is a 'return' statement, convert first line to if statement, indent (100>>), and end with fi: | |
if [[ -n "$PS1" ]]; then | |
...blah... | |
fi | |
- in any case add to end: | |
[[ -s "/usr/local/rvm/scripts/rvm" ]] && source "/usr/local/rvm/scripts/rvm" # This loads RVM into a shell session. | |
- Redhat: | |
usermod -a -G rvm [user] (add group rvm to user, or 'adduser [user] rvm') | |
- Debian: | |
adduser [user] rvm | |
- exit shell and log back in | |
chown -R [user]:rvm /usr/local | |
Passenger with nginx: | |
---------- | |
rvm install 1.9.2 | |
(rvm use 1.9.2 --default) | |
rvm use 1.9.2@global | |
gem update --system | |
gem install bundler passenger | |
passenger-install-nginx-module (when prompted install to into /usr/local/nginx) | |
- see ubuntu-deploy.txt or centos-deploy.txt for startup scripts | |
nginx vhosts: | |
------------- | |
- create vhosts: | |
mkdir -p /usr/local/nginx/conf/vhosts/on | |
mkdir -p /usr/local/nginx/conf/vhosts/off | |
- add to /usr/local/nginx/conf/nginx.conf within http block: | |
include vhosts/on/*.conf; | |
- create /usr/local/nginx/conf/vhosts/off/example.conf: | |
server { | |
listen 80; | |
server_name example.com ubuntu-64.local; | |
location / { | |
root /home/tim/sites/example/current/public; | |
passenger_enabled on; | |
} | |
} | |
# 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 ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP; | |
# ssl_prefer_server_ciphers on; | |
# location / { | |
# root html; | |
# index index.html index.htm; | |
# } | |
#} | |
- or create /usr/local/nginx/conf/vhosts/off/sub_uris.conf: | |
server { | |
listen 80; | |
server_name ubuntu-64.local; | |
location / { | |
root html; | |
passenger_enabled on; | |
# Add projects here, symlink public dir to #{root}/project | |
passenger_base_uri /example; | |
} | |
} | |
Setup load paths (rvm and bundler) | |
---------------------------------- | |
- put .rvmrc in project: | |
rvm use 1.9.2@project --create | |
- create config/setup_load_paths.rb: (not required with capistrano) | |
if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm') | |
begin | |
rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME'])) | |
rvm_lib_path = File.join(rvm_path, 'lib') | |
$LOAD_PATH.unshift rvm_lib_path | |
require 'rvm' | |
RVM.use_from_path! File.dirname(File.dirname(__FILE__)) | |
rescue LoadError | |
# RVM is unavailable at this point. | |
raise "RVM ruby lib is currently unavailable." | |
end | |
end | |
# Select the correct item for which you use below. | |
# If you're not using bundler, remove it completely. | |
# | |
# # If we're using a Bundler 1.0 beta | |
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile', File.dirname(__FILE__)) | |
require 'bundler/setup' | |
# | |
# # Or Bundler 0.9... | |
# if File.exist?(".bundle/environment.rb") | |
# require '.bundle/environment' | |
# else | |
# require 'rubygems' | |
# require 'bundler' | |
# Bundler.setup | |
# end | |
- if not using capistrano, don't forget to: | |
bundle install | |
rake db:setup RAILS_ENV=production | |
(or rake db:migrate RAILS_ENV=production) | |
- for centos, check permissions on home directory, needs to be group and other readable and executable (recursive go+rx). | |
Capistrano | |
---------- | |
- add to deploy.rb: | |
require File.dirname(__FILE__) + "/capistrano_database.rb" | |
require "bundler/capistrano" | |
# rvm | |
$:.unshift(File.expand_path('./lib', ENV['rvm_path'])) | |
require "rvm/capistrano" | |
set :rvm_ruby_string, '1.9.2@project' | |
# deploy via ssh gateway (optional): | |
set :gateway, "gateway.server.com" | |
set :bundle_without, [:development, :test] | |
set :user, "user" | |
set :use_sudo, false | |
set :deploy_to, "/home/#{user}/sites/#{application}" | |
set :branch, "master" | |
set :deploy_via, :remote_cache | |
set :git_shallow_clone, 1 # clone latest rev only | |
# following is to scp local repo instead of checkout | |
set :scm, :git | |
set :repository, "." | |
set :deploy_via, :copy | |
namespace :deploy do | |
task :cold do # Overriding the default deploy:cold | |
update | |
load_schema # Custom step, replacing migrations. | |
start | |
end | |
task :load_schema, :roles => :app do | |
run "cd #{current_path}; rake db:schema:load RAILS_ENV=production" | |
end | |
end | |
- add config/capistrano_database.rb and follow instructions (don't worry about deleting database.yml, in template use <%= %> instead of #{}): | |
# | |
# = Capistrano database.yml task | |
# | |
# Provides a couple of tasks for creating the database.yml | |
# configuration file dynamically when deploy:setup is run. | |
# | |
# Category:: Capistrano | |
# Package:: Database | |
# Author:: Simone Carletti <weppos@weppos.net> | |
# Copyright:: 2007-2010 The Authors | |
# License:: MIT License | |
# Link:: http://www.simonecarletti.com/ | |
# Source:: http://gist.github.com/2769 | |
# | |
# | |
# == Requirements | |
# | |
# This extension requires the original <tt>config/database.yml</tt> to be excluded | |
# from source code checkout. You can easily accomplish this by renaming | |
# the file (for example to database.example.yml) and appending <tt>database.yml</tt> | |
# value to your SCM ignore list. | |
# | |
# # Example for Subversion | |
# | |
# $ svn mv config/database.yml config/database.example.yml | |
# $ svn propset svn:ignore 'database.yml' config | |
# | |
# # Example for Git | |
# | |
# $ git mv config/database.yml config/database.example.yml | |
# $ echo 'config/database.yml' >> .gitignore | |
# | |
# | |
# == Usage | |
# | |
# Include this file in your <tt>deploy.rb</tt> configuration file. | |
# Assuming you saved this recipe as capistrano_database_yml.rb: | |
# | |
# require "capistrano_database_yml" | |
# | |
# Now, when <tt>deploy:setup</tt> is called, this script will automatically | |
# create the <tt>database.yml</tt> file in the shared folder. | |
# Each time you run a deploy, this script will also create a symlink | |
# from your application <tt>config/database.yml</tt> pointing to the shared configuration file. | |
# | |
# == Custom template | |
# | |
# By default, this script creates an exact copy of the default | |
# <tt>database.yml</tt> file shipped with a new Rails 2.x application. | |
# If you want to overwrite the default template, simply create a custom Erb template | |
# called <tt>database.yml.erb</tt> and save it into <tt>config/deploy</tt> folder. | |
# | |
# Although the name of the file can't be changed, you can customize the directory | |
# where it is stored defining a variable called <tt>:template_dir</tt>. | |
# | |
# # store your custom template at foo/bar/database.yml.erb | |
# set :template_dir, "foo/bar" | |
# | |
# # example of database template | |
# | |
# base: &base | |
# adapter: sqlite3 | |
# timeout: 5000 | |
# development: | |
# database: #{shared_path}/db/development.sqlite3 | |
# <<: *base | |
# test: | |
# database: #{shared_path}/db/test.sqlite3 | |
# <<: *base | |
# production: | |
# adapter: mysql | |
# database: #{application}_production | |
# username: #{user} | |
# password: #{Capistrano::CLI.ui.ask("Enter MySQL database password: ")} | |
# encoding: utf8 | |
# timeout: 5000 | |
# | |
# Because this is an Erb template, you can place variables and Ruby scripts | |
# within the file. | |
# For instance, the template above takes advantage of Capistrano CLI | |
# to ask for a MySQL database password instead of hard coding it into the template. | |
# | |
# === Password prompt | |
# | |
# For security reasons, in the example below the password is not | |
# hard coded (or stored in a variable) but asked on setup. | |
# I don't like to store passwords in files under version control | |
# because they will live forever in your history. | |
# This is why I use the Capistrano::CLI utility. | |
# | |
unless Capistrano::Configuration.respond_to?(:instance) | |
abort "This extension requires Capistrano 2" | |
end | |
Capistrano::Configuration.instance.load do | |
namespace :deploy do | |
namespace :db do | |
desc <<-DESC | |
Creates the database.yml configuration file in shared path. | |
By default, this task uses a template unless a template \ | |
called database.yml.erb is found either is :template_dir \ | |
or /config/deploy folders. The default template matches \ | |
the template for config/database.yml file shipped with Rails. | |
When this recipe is loaded, db:setup is automatically configured \ | |
to be invoked after deploy:setup. You can skip this task setting \ | |
the variable :skip_db_setup to true. This is especially useful \ | |
if you are using this recipe in combination with \ | |
capistrano-ext/multistaging to avoid multiple db:setup calls \ | |
when running deploy:setup for all stages one by one. | |
DESC | |
task :setup, :except => { :no_release => true } do | |
default_template = <<-EOF | |
base: &base | |
adapter: sqlite3 | |
timeout: 5000 | |
development: | |
database: #{shared_path}/db/development.sqlite3 | |
<<: *base | |
test: | |
database: #{shared_path}/db/test.sqlite3 | |
<<: *base | |
production: | |
database: #{shared_path}/db/production.sqlite3 | |
<<: *base | |
EOF | |
location = fetch(:template_dir, "config/deploy") + '/database.yml.erb' | |
template = File.file?(location) ? File.read(location) : default_template | |
config = ERB.new(template) | |
run "mkdir -p #{shared_path}/db" | |
run "mkdir -p #{shared_path}/config" | |
put config.result(binding), "#{shared_path}/config/database.yml" | |
end | |
desc <<-DESC | |
[internal] Updates the symlink for database.yml file to the just deployed release. | |
DESC | |
task :symlink, :except => { :no_release => true } do | |
run "ln -nfs #{shared_path}/config/database.yml #{release_path}/config/database.yml" | |
end | |
end | |
after "deploy:setup", "deploy:db:setup" unless fetch(:skip_db_setup, false) | |
after "deploy:finalize_update", "deploy:db:symlink" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment