The easiest deploy application on Ruby on Rails
- Tutorial

Six months ago, I wrote a Deploy application post on RoR 4 using Capistrano 3 . Time passed, I received a lot of positive reviews, but there were also negative ones. From them it was possible to understand the following:
- The instruction is too complicated for a beginner
- I have to do a lot of things “with my hands”
I thought and wrote a gem 'capistrano3-ubuntu-server-config' that completely configures your "clean" Ubuntu server. All you need to do with your hands is to create a new user and give him visudo rights (and he does not need to give him rights to passwordless sudo ). He can:
- Configure SSH (Add settings 'PermitRootLogin no', 'UseDNS no', 'AllowUsers username')
- Create and configure swap (size requested)
- Make
andsudo apt-get updatesudo apt-get upgrade - Install from source and configure both pure Nginx and with Pagespeed module
- Install PostgreSQL from the repository, then create the database superuser (username and password are requested)
- Install from source and configure Redis
- Install RVM with the latest version of Ruby and Rails, Bundler gems
- Copy your private ssh key (for example, to access the private git repository) from the local machine to the server and add it to ~ / .ssh / config
- Install imagemagick from the repository (Required for Paperclip, I constantly forget to install it)
- Install any additional packages from the repository (Requests which ones)
You can run the configuration wizard, which finds out what exactly of the above needs to be done and in advance asks for all the settings so that you can then go have a drink of coffee, or you can run individual tasks. This gem will be useful not only to Rails developers, but to everyone who uses Capistrano for deployment.
This article will cover the following topics:
- Using gem'a capistrano3-ubuntu-server-config
- Using capistrano3-git-push gem
- My current miniature configuration is Capistrano
gem 'capistrano3-ubuntu-server-config'
What this gem can do, I already told. We proceed directly to working with him. Imagine we have a clean web server on Ubuntu (I tested on Ubuntu 14.04). We need to do only two simple things ourselves: create a new user with sudo rights and provide password-less login from your local machine to the server via SSH. Let's start from the first one, on the server, logging in as root, execute:
adduser deployer
echo "deployer ALL=(ALL) ALL" >> /etc/sudoers
Instead, deployer can be any username. It would also be nice to change the password for the root user with the passwd command.
Now we provide passwordless login from the local machine to the server via ssh. To do this, on the local machine, execute (where depoyer is the username, 111.111.111.111 is the server address):
ssh-copy-id [email protected]
On this, all server configuration is complete. In an ideal situation, you will no longer have to go ssh to the server. For viewing logs I recommend gem 'tail' .
Let's start using my gem. In the Gemfile add:
group :development do
gem 'capistrano'
gem 'capistrano3-ubuntu-server-prepare'
endWe execute bundle install, cap install, add the line
require 'capistrano3/ubuntu-server-prepare'in capfile. Almost everything is ready to go. Except for one: to configure Nginx and Redis, my script takes .conf files from the config / production / nginx and config / production / redis folders. To quickly copy my configuration files to these folders, simply do:
rake ubuntu_server_prepare:copy_configAs a bonus, you also get a customized Unicorn config. The nginx folder contains two files: nginx.conf and nginx_with_pagespeed.conf. The second is used when choosing pagespeed installation in the configurator.
Attention! my Nginx and Unicorn configuration file! configured on the Rails application, which is located in '/ var / www / application / current'. Change all the paths in these files or just add a line
set :application, 'application'to your deploy.rb for deployment to this folder. In 'config / deploy / production.rb' you need to register your server, and also make sure that the line in the Capfile
require 'capistrano/rvm'was commented out . Now we proceed to the most delicious:
cap production ubuntu_server_prepareThe configurator will ask you many questions, after receiving answers to which, he will be engaged in server configuration. This process is moderately long, so you can go have a drink of coffee.
It is possible to run individual tasks, for example, by doing
cap production ubuntu_server_prepare:nginx_confyou copy the nginx.conf configuration file to the server and reboot nginx. This is convenient to quickly change the config: they changed something directly in the project folder and sent it to the server with one command.gem 'capistrano3-git-push'
A small task for Capistrano 3 performing
git add -A
git commit -m "#{сообщение}"
git push
only if there is a change. If you enter "skip" in the field for requesting a message about a commit, then nothing will be done, which is convenient when you need to deploy, but do not need to upload changes to the repository. Connecting is easy. In the gemfile:
group :development do
gem 'capistrano3-git-push'
end
In Capfile:
require 'capistrano3/git-push'In deploy.rb:
before :deploy, 'git:push'My current Capistrano configuration
If you recall my previous article , then my deploy.rb was just huge. Now my configuration is simple to madness.
group :development do
gem 'capistrano'
gem 'capistrano-rails'
gem 'capistrano-bundler'
gem 'capistrano3-unicorn'
gem 'capistrano-rvm'
gem 'capistrano3-ubuntu-server-prepare'
gem 'capistrano3-delayed-job'
end
group :production do
gem 'unicorn'
end
# Load DSL and set up stages
require 'capistrano/setup'
require 'capistrano/deploy'
require 'capistrano3/ubuntu-server-prepare'
require 'capistrano3/unicorn'
require 'capistrano3/git-push'
require 'capistrano/rvm'
require 'capistrano/bundler'
require 'capistrano/rails'
set :application, 'application'
set :repo_url, "#{ВАШ_АДРЕС_РЕПО}"
set :unicorn_config_path, "#{current_path}/config/production/unicorn/unicorn.rb"
set :linked_dirs, fetch(:linked_dirs, []).push('bin', 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system') # Строчка есть по умолчанию в deploy.rb, ее просто надо откомментировать
namespace :deploy do
task :setup do
before "deploy:migrate", :create_db
invoke :deploy
end
task :create_db do
on roles(:all) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "db:create"
end
end
end
end
task :restart do
invoke 'unicorn:legacy_restart'
end
end
before :deploy, 'git:push'
before 'deploy:setup', 'git:push'
That is, first you need to do everything that I described at the beginning of the article, then do it once
cap production deploy:setup
to create a database. We perform all subsequent timescap production deployIt is probably worth explaining what is happening here.
set :unicorn_config_path, "#{current_path}/config/production/unicorn/unicorn.rb" Specifies the location of the unicorn config for the gem 'capistrano3-unicorn'.set :linked_dirs, fetch(:linked_dirs, []).push('bin', 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system') Creates symlinks to the specified folders from the current folder to the shared folder. task :setup do
before "deploy:migrate", :create_db
invoke :deploy
end It calls task: create_db, before executing 'db: migrate' on the first deploy (deploy: setup).task :create_db do
on roles(:all) do
within release_path do
with rails_env: fetch(:rails_env) do
execute :rake, "db:create"
end
end
end
end The same task: create_db, which calls 'rake db: create' on the first deployment. task :restart do
invoke 'unicorn:legacy_restart'
end restart unicorn on every deploy.PS If you are using resque, you should look at the gem 'capistrano-resque' , if you are using delayedjob, you should look at the gem 'capistrano3-delayed-job' .