Rails 5: Puma + Capistrano + NGINX + Jungle Upstart

Article presents how to setup production server to run basic application on NGINX web server and Puma app server.

Rails:            5.0.0
Ruby:            2.3.0
Ubuntu:         16.04
Capistrano:   3.6

Introduction

Capistrano is used to deploy application to many servers, in our case we will deploy from development machine to production server. Puma is a Rails app server and NGINX is a web server.

  • NGINX – web server – receives the request from users and pass it to app server (Puma)
  • Puma – app server – runs Rails application. It receives requests from NGINX (user requests) and push them to Rails app (eg. to controller). Take the respones from Rails app i return it to NGINX which passes it futher to client. Puma and NGINX run independently and are connected together by socket – this is how they communicate.

 

Development server

Add to Gemfile:

Run following commands:

Add following to Capfile:

 

Edit config/deploy.rb:

 

Run following command to generate template files:

Optionally, you can edit created by previous command files.

 

Production server

On production server, install all required components, required by you app. Mostly it is: Rails, Ruby, MySQL (or Postgresql) and nodejs – instructions to install this elments you can find in this article.

Install NGINX:

And remove default NGINX site:

 

Install Jungle Upstart – it allows to easily start, stop and restart Puma servers. Whatsmore, it starts the Puma servers on system boot.

Open downloaded puma.conf file and set your system’s user account for setuid and setguid. (in our case we use root account, but it’s recommended to use a less-priviliged account):

Move downloaded upstart files to /etc/init and create another puma.conf file in /etc:

 

 

Running…

Verify that ssh-agent is running and SSH key is added to it:

If it’s not, please refer for more details to following article.

 

Command to run deploy:

 

Last thing to do is add path to application on production server. Open /etc/puma.conf and add path to app:

Remember about adding at the end the current – it is folder name of current release.

 

Simple tests:

  1. Check if application starts after system rebooting.
    Reboot your production server

    and then open the app in web browser. If your app is available, everything is fine. If not, please refer to section Common Problems.
  2. Check logs
    Go to: /var/www/name_of_your_app/shared/log. Open production.log:

    Next, open app in web browser and check if requests are logged in log file. You can do the same for other log files.

Common Problems:

Jungle Upstart

If you are running your production server on Ubuntu 15.04 or higher (eg. 16.04) you can get a problem with a Jungle Upstart. To verify if you get a the same problem which we want to resolve here, go to production server and use following command:

You can get following error: “Unable to connect to Upstart: Failed to connect to socket /com/ubuntu/upstart: Connection refused“. The reason is that Ubuntu for version >= 15.04 changed the upstart service to systemd (more details here). You can make that your Ubuntu returns to using upstart, instead of systemd:

You have to reboot your machine after all.

 

Useful commands:

Puma-manager (to run on production site):

Capistrano-puma (on local machine, affects on production):

Capistrano-nginx (on local machine, affects on production):

Capistrano:

 

 

 

Tomasz Antas

Ruby on Rails developer and Web designer.
The area of his interest includes Popular Science, Internet of Things, Wearables, AI and Virtual/Augmented Reality.

Latest posts by Tomasz Antas (see all)

Thanks for reading!

  • Thanks for sharing!

  • rahul kumar

    I am getting this error on runing ‘ start puma-manager’:-
    ‘start: Rejected send message, 1 matched rules; type=”method_call”, sender=”:1.1″ (uid=500 pid=2737 comm=”start puma-manager “) interface=”com.ubuntu.Upstart0_6.Job” member=”Start” error name=”(unset)” requested_reply=”0″ destination=”com.ubuntu.Upstart” (uid=0 pid=1 comm=”/sbin/init “)’