Practice deploying with Capistrano

Tram Ho

Hello everyone, today I will write about a familiar topic: Deploy 1 Rails app with Capistrano.

Entering the world of Ruby and Rails, you will probably read through Ruby on Rails Tutorial, a book that helps you approach Ruby on Rails from zero to deploy.

You will be familiar with commands like git push heroku master , heroku run rails db:migrate , … and your Rails app will run smoothly on Heroku’s server.

Step into real life projects, you will start a bit further with Heroku, instead will be the AWS server, the physical server, and how to deploy your Rails app onto those servers. There are quite a few ways, from using Docker, Codedeploy, Capistrano, … or even you can directly access the server and do everything with rice.

Today’s article I will guide you to build a server on a local machine, simulate the steps to deploy a Rails app to that server using Capistrano – a deployable support library that can be said to be the best today of Rails.

Build a deploy server

To deploy, for sure, first you need to have a server for Rails app to run on

Quite easy, you can spend money to buy a certain physical server, or sign up for an AWS account, GCP creates an instance. The ultimate goal is to have a complete environment setup machine on which your Rails app can run.

However, if you do not have the conditions, you can follow yourself to build a virtual server right on your own PC.

I will guide you to install Docker and run a container with Ubuntu 18.04 installed

What is that docker? This is quite a wide knowledge, so I will not explain in this article. If you still want to be curious to know why only a few of the commands above have 1 server, you can research Docker. You can also see it as a virtual machine, but in fact it is not a virtual machine. Well, if you do not want to use Docker, you can also install a virtual machine running on your OS, but make sure your hardware is guaranteed to run without lag.

OK, so we have a server running Ubuntu 18.04

Install the environment

Imagine you just bought a brand-new computer, installed Ubuntu 18.04. Now, what to do if I want to run a Rails app on it? Do the same thing with the server I just built above.

Let’s get back to our terminal, when you are standing

At this point you are accessing the machine named 3788a1e70189 as root .

Well, if you turned off the terminal or quit the deploy machine above, you can re-enable it with the command

In fact, you will have to create more users, grant sufficient permissions to deploy, not deploy as root. Try creating a user called deploy

Ok now let’s install the environment following this familiar guide https://gorails.com/setup/ubuntu/18.04

Config deploy

Now add Capistrano and support gems to your project. You just need to add these Gems to group development. Because in essence it will stand in your local to deploy the code to the server staging, production, …

bundle install to install the Capistrano libraries on your local machine.

bundle exec cap install and Capistrano will generate the following config files

When you do deploy, Capistrano will do require libraries from Capfile , read the config/deploy.rb from config/deploy.rb and then read the config/deploy.rb from config/deploy/{env}.rb . Same config but different value between 2 files deploy.rb and {env}.rb , the value in {env}.rb is the final value.

For now, let’s dabble in some basic configurations:

Capistrano is an automated deploy tool built on Ruby, Rake and SSH. It will stand in your local machine SSH to the server and execute Rake tasks in sequence. So the first condition to deploy is that your local machine can access the server via SSH.

To be able to access the server via SSH, it is very simple to simply provide SSH public key to the deploy server. From your local machine type cat ~/.ssh/id_rsa.pub to get the SSH public key

Let’s go back to our Terminal, where we are now accessing the deploy server with the deploy user. Make sure you are on the deploy user

Now open the authorized_keys file and paste your SSH public key into it, and you can access the server from local access using the command ssh {user}@{host} . In which the main user is deploy , the host is the address of the server. To get the location of the server you built earlier, type docker inspect ubuntu_server | grep IPAddress , the IPAddress value is the IP of the server above. Assuming this value is 172.17.0.2, try to connect to the server using the command ssh [email protected]

So the preparation is almost done, now let’s say you set the environment for the other deploy server to production. Please config/deploy.rb the values ​​for the config/deploy.rb and config/deploy/production.rb . Standing in your project folder on local machine type the command cap production deploy , Cap will help you automatically get the code from the Github repo, and deploy it on your deploy server. Read all the generated log lines to understand what steps Capistrano will do when deploying your code on a server.

However, life will not be a dream, the first deployments you will encounter many errors, such as not creating .env file, installing Ruby is not the correct version, no DB, missing RAILS_MASTER_KEY, …. Do not be discouraged, try to read the log (Capistrano it records log quite clearly) and fix each error one by one.

The most troublesome was to turn on the puma. On the server, the capistrano will not turn on puma at port 3000 like our local machine, it will run the puma as a socket through the config file stored at #{your_project_path}/shared/puma.rb on the server machine. SSH into your server, creating the file #{your_project_path}/shared/puma.rb

Try deploying again, fix minor errors until Capistrano notices that all your tasks success means your code has been successfully deployed on the deploy server.

Public results

Since the puma on the deploy server is running under the socket, your application cannot be accessed using the public URL. As a final step, install and configure nginx to allow external viewing of your product

Access to the deploy server using SSH, install nginx sudo apt-get install nginx . Delete the default config file of nginx sudo rm /etc/nginx/sites-enabled/default Create a config file sudo touch /etc/nginx/conf.d/default.conf Open the config file and try the following config. explain about the config in this article)

Save and restart nginx sudo service nginx restart

Open your browser and access your server, like our example is 172.17.0.2 and see the result

Wish you success and through that hope you have an overview of how to deploy a Rails application on the server of Capistrano.

Share the news now

Source : Viblo