on
Italy
- Get link
- X
- Other Apps
deploy with sudo privileges (Initial Server Setup with Ubuntu 14.04 explains how to set this up.)deploy user and use sudo for this user before closing the root SSH session you opened to make these changes.deploy user. If root access is required for the command, it will be preceded by sudo.
- sudo apt-get update
Then, install Nginx:
- sudo apt-get install curl git-core nginx -y
- gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
Then install RVM to manage our Rubies:
- curl -sSL https://get.rvm.io | bash -s stable
curl to download the RVM Installation script from https://get.rvm.io. The -sSL option is composed of three flags: -s tells curl to download the file in 'silent mode'-S tells curl to show an error message if it fails-L tells curl to follow all HTTP redirects while retrieving the installation scriptbash. The -s option passes stable as an argument to the RVM Installation script to download and install the stable release of RVM.requirements command to automatically install required dependencies and files for RVM and Ruby to function properly:
- source ~/.rvm/scripts/rvm
- rvm requirements
We can now install the Ruby of our choice. We'll be installing the latest Ruby 2.2.1 (at the time of writing) as our default Ruby:
- rvm install 2.2.1
- rvm use 2.2.1 --default
bundler which can read your app's Gemfile and automatically install all required gems.
- gem install rails -V --no-ri --no-rdoc
- gem install bundler -V --no-ri --no-rdoc
Three flags were used:-V (Verbose Output): Prints detailed information about Gem installation--no-ri - (Skips Ri Documentation): Doesn't install Ri Docs, saving space and making installation fast--no-rdoc - (Skips RDocs): Doesn't install RDocs, saving space and speeding up installation-v flag:
- gem install rails -v '4.2.0' -V --no-ri --no-rdoc
- ssh -T git@github.com
- ssh -T git@bitbucket.org
Don't worry if you get a Permission denied (publickey) message. Now, generate a SSH key (a Public/Private Key Pair) for your server:
- ssh-keygen -t rsa
Add the newly created public key (~/.ssh/id_rsa.pub) to your repository's deployment keys:clone your git repository (over the SSH Protocol, not HTTP) without entering your password:
- git clone
git@example.com:username/appname.git
If you need a sample app for testing, you can fork the following test app specifically created for this tutorial: Sample Rails App on GitHubgit clone command will create a directory with the same name as your app. For example, a directory named testapp_rails will be created.
- ssh-keygen -t rsa
Add your local SSH Key to your Droplet's Authorized Keys file (remember to replace the port number with your customized port number):
- cat ~/.ssh/id_rsa.pub | ssh -p your_port_num deploy@your_server_ip 'cat >> ~/.ssh/authorized_keys'
Gemfile in the Rails App:
group :development do
gem 'capistrano', require: false
gem 'capistrano-rvm', require: false
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano3-puma', require: false
end
gem 'puma'
Use bundler to install the gems you just specified in your Gemfile. Enter the following command to bundle your Rails app:
- bundle
- cap install
This will create:Capfile in the root directory of your Rails appdeploy.rb file in the config directorydeploy directory in the config directoryCapfile with the following:# Load DSL and Setup Up Stages
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano/rails'
require 'capistrano/bundler'
require 'capistrano/rvm'
require 'capistrano/puma'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.rake').each { |r| import r }
This Capfile loads some pre-defined tasks in to your
Capistrano configuration files to make your deployments hassle-free,
such as automatically:config/deploy.rb with the following, updating fields marked in red with your app and Droplet parameters:
# Change these
server 'your_server_ip', port: your_port_num, roles: [:web, :app, :db], primary: true
set :repo_url,
'git@example.com:username/appname.git'
set :application, 'appname'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 0
# Don't change these unless you know what you're doing
set :pty, true
set :use_sudo, false
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
# set :keep_releases, 5
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/master`
puts "WARNING: HEAD is not the same as origin/master"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
This deploy.rb file contains some sane defaults that
work out-of-the-box to help you manage your app releases and
automatically perform some tasks when you make a deployment:production as the default environment for your Rails appconfig/nginx.conf in your Rails project directory, and add the following to it (again, replacing with your parameters):
upstream puma {
server unix:///home/deploy/apps/appname/shared/tmp/sockets/appname-puma.sock;
}
server {
listen 80 default_server deferred;
# server_name example.com;
root /home/deploy/apps/appname/current/public;
access_log /home/deploy/apps/appname/current/log/nginx.access.log;
error_log /home/deploy/apps/appname/current/log/nginx.error.log info;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
try_files $uri/index.html $uri @puma;
location @puma {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_pass http://puma;
}
error_page 500 502 503 504 /500.html;
client_max_body_size 10M;
keepalive_timeout 10;
}
Like the previous file, this nginx.conf contains defaults that work out-of-the-box with the configurations in your deploy.rb
file. This listens for traffic on port 80 and passes on the request to
your Puma socket, writes nginx logs to the 'current' release of your
app, compresses all assets and caches them in the browser with maximum
expiry, serves the HTML pages in the public folder as static files, and
sets default maximum Client Body Size and Request Timeout values.
- git add -A
- git commit -m "Set up Puma, Nginx & Capistrano"
- git push origin master
Note: If this is the first time using GitHub from this
system, you might have to issue the following commands with your GitHub
username and email address:
- git config --global user.name 'Your Name'
- git config --global user.email you@example.com
Again, from your local machine, make your first deployment:
- cap production deploy:initial
nginx.conf to the sites-enabled directory:
- sudo rm /etc/nginx/sites-enabled/default
- sudo ln -nfs "/home/deploy/apps/appname/current/config/nginx.conf" "/etc/nginx/sites-enabled/appname"
Restart the Nginx service:
- sudo service nginx restart
You should now be able to point your web browser to your server IP and see your Rails app in action!deploy command:
- git add -A
- git commit -m "Deploy Message"
- git push origin master
- cap production deploy
config/nginx.conf file, you'll have to reload or restart your Nginx service on the server after deploying your app:
- sudo service nginx restart
Comments
Post a Comment