Mina Setup

Let’s take a look at setting up Mina with Puma. First, you’ll need to add Mina and mina-puma in Gemfile.

Then install gems and execute the initial Mina Command for generating a config/deploy.rb.

1
2
bundle
mina init

Detailed Explanations for the Mina deploy file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Set the domain or ip address of the remote server.
set :domain, 'yourdomain'

# Set the folder of the remote server where Mina will deploy your app.
set :deploy_to, 'path/to/directory'

# Set a link to the repository. Better git protocol.
set :repository, 'git@...'

# Set the name of a branch you plan to deploy as default master.
set :branch, 'master'

# Fill in the names of the files and directories that will be symlinks to the shared directory.
# All folders will be created automatically on Mina Setup.
# Don't forget to add a path to the uploads folder if you are using Dragonfly or Carrierwaves.
# Otherwise, you will lose your uploads on each deploy.
set :shared_dirs, fetch(:shared_dirs, []).push('log', 'tmp/pids', 'tmp/sockets', 'public/uploads')
set :shared_files, fetch(:shared_files, []).push('config/database.yml', 'config/secrets.yml', 'config/puma.rb')

# Username of ssh for access to the remote server.
set :user, 'root'

# This is not a required field, you can use it to set an app name for easy recognition.
set :application_name, 'MyApp'

# Set ruby version. If you have RVM installed globally, you'll also need to set an RVM path,
# like set:rvm_use_path, '/usr/local/rvm/scripts/rvm'.
# You can find the RVM location with rvm info command.
task :environment do
invoke :'rvm:use', 'ruby-2.5.1@default'
end

By default, Mina will create all folders mentioned in shared_dirs and shared_files.

You deploy section in deploy.rb should look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
task :deploy do
deploy do
comment "Deploying #{fetch(:application_name)} to #{fetch(:domain)}:#{fetch(:deploy_to)}"
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'rvm:load_env_vars'
invoke :'bundle:install'
invoke :'rails:db_migrate'
command %{#{fetch(:rails) db:seed}}
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'

on :launch do
invoke :'puma:phased_restart'
end
end
end

Puma Setup

Create or fill a puma.rb file in a config folder

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
environment "productino"

bind "unix:///{path_to_your_app}/shared/tmp/sockets/puma.sock"
pidfile "/{path_to_your_app}/shared/tmp/pids/puma.pid"
state_path "/{path_to_your_app}/shared/tmp/sockets/puma.state"
directory "/{path_to_your_app}/current"

workers 2
threads 1,2

daemonize true

activate_control_app 'unix:///{path_to_your_app}/shared/tmp/sockets/pumactl.sock'

prune_bundler

Fill database.yml and secrets.yml

Setup nginx

Create file myapp.conf in a /nginx/etc/conf.d folder with similar content.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
upstream mysite {
server unix:///home/admin/mysite/shared/tmp/sockets/puma.sock fail_timeout=0;
}

server {
listen 80;
listen [::]:80;

server_name mysite.com;
root /home/admin/mysite/current/public;

location ~ ^/assets/ {
expires max;
gzip_static on;
gzip_vary on;
add_header Cache-Control public;
break;
}


location / {
proxy_pass http://mysite;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

location ~ ^/(500|404|422).html {
root /home/admin/mysite/current/public;
}

error_page 500 502 503 504 /500.html;
error_page 404 /404.html;
error_page 422 /422.html;

client_max_body_size 4G;
keepalive_timeout 10;
}