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.
- A lot of things have to do "hands"
I thought and wrote
gem 'capistrano3-ubuntu-server-config' , which completely sets up your “clean” Ubuntu server. All you have to do with your hands is to create a new user and give him visudo rights (and
you don’t need to give him the rights to passwordless sudo). He can:
- Configure SSH (Add settings for 'PermitRootLogin no', 'UseDNS no', 'AllowUsers username')
- Create and configure swap (size requested)
- To make
sudo apt-get update
and
sudo apt-get upgrade
- Install from source and configure both pure Nginx and Pagespeed module
- Install PostgreSQL from the repository, then create a superuser database (username and password are requested)
- Install from source and configure Redis
- Install RVM with the latest version of Ruby and Rails gems, Bundler
- 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 will know what needs to be done from the above and will ask all the settings in advance so that you can go and drink 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:
gem 'capistrano3-ubuntu-server-config'
What can do this gem, I have already told. Let's go directly to work with him. Imagine that 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 passwordless access from your local machine to the server via SSH. Let's start with the first, on the server, logged in as root perform:
adduser deployer echo "deployer ALL=(ALL) ALL" >> /etc/sudoers
Instead of deployer there can be any username. It would not hurt to even change the password of the root user with the passwd command.
Now we will provide passwordless login from the local machine to the server via ssh. To do this, execute it on the
local machine (where depoyer is the user name, 111.111.111.111 is the server address):
ssh-copy-id deployer@111.111.111.111
That's where the server setup is complete. In an ideal situation, you no longer have to log in via ssh to the server. To view logs, I recommend
gem 'tail' .
Let's start using my gem'a. Add to Gemfile:
group :development do gem 'capistrano' gem 'capistrano3-ubuntu-server-prepare' end
We execute bundle install, cap install, we add a line
require 'capistrano3/ubuntu-server-prepare'
in capfile.
')
Almost everything is ready to go. Except for one thing: 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, just run:
rake ubuntu_server_prepare:copy_config
Bonus also get customized Unicorn config. There are two files in the nginx folder: nginx.conf and nginx_with_pagespeed.conf. The second is used when selecting the pagespeed installation in the configurator.
Attention! my nginx and unicorn configuration file! configured on a Rails application that is in '/ var / www / application / current'. Change all paths in these files or just add a line.
set :application, 'application'
in 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 Capfile
require 'capistrano/rvm'
was
commented out .
Now we proceed to the most delicious:
cap production ubuntu_server_prepare
The configurator will ask you a lot of questions, having received the answers to which, will be engaged in setting up the server. This process is moderately long, so you can go to drink coffee.
It is possible to run individual tasks, for example, by running
cap production ubuntu_server_prepare:nginx_conf
You copy the nginx.conf configuration file to the server and reboot nginx. This is convenient to quickly change the config: changed something right 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 in case there are changes. If you enter “skip” in the field for requesting a message about a commit, nothing will be done, which is convenient when you need to deploy, but you do not need to upload changes to the repository.
Connect 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 configuration is Capistrano
If you recall
my previous article , then my deploy.rb was just huge. Now my configuration is simple to madness.
Gemfile 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
deploy.rb 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')
That is, you first need to do everything that I described at the beginning of the article, then once
cap production deploy:setup
to create a database. All subsequent times we perform
cap production deploy
It is probably worth explaining what is happening here.
What's going on here? set :unicorn_config_path, "#{current_path}/config/production/unicorn/unicorn.rb"
Sets 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 specified folders from current folder to shared folder.
task :setup do before "deploy:migrate", :create_db invoke :deploy end
Calls: create_db, before executing 'db: migrate' during the first 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 delay.
task :restart do invoke 'unicorn:legacy_restart' end
restart unicorn for every delay.
PS If you use resque, you should look at the
gem 'capistrano-resque' , if you use delayedjob, you should look at the
gem 'capistrano3-delayed-job' .