How to point Nginx to serve Capistrano deployment location (/home/rails/apps/appname/current) on Rails one click application?

November 4, 2018 656 views
Ruby on Rails Nginx Ubuntu 18.04

Hello,

I have set up a Rails one click application and have configured Capistrano to deploy to /home/rails/apps/appname/current. All of the files are present and there are no errors during deployment. Before I setup Capistrano I explored the Nginx files in /etc/nginx/sites-enabled and /etc/nginx/sites-available. Following this tutorial (https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma), I have created a symlink to an nginx.conf file stored at /home/rails/apps/appname/current/config.nginx.conf to exist at /etc/nginx/sites-enabled/sites-enabled/appname. However, when I visit my site via its IP or domain, I get a simple “connected refused” error, telling me that the nginx server isn’t pointing to the right place.

My question is this: given that the Rails one click app originally launches using /etc/nginx/sites-enabled/rails (the configuration there is what makes the example Rails app work), is this somehow interfering with the symlink to the nginx.conf file I’ve created with Capistrano? I am quite confident that everything is correct on the Capistrano side (it is an exact copy of the nginx.conf from the tutorial linked above with my app and server details in the relevant places) and that the issue is with the pre-popualated files at various places within /etc/nginx/*.

There is another issue which I think might play a role: the Rails one click app boots via /etc/systemd/system/rails.service, and within that file there’s a call to puma. Is this a case of nginx not recognising or reading the .conf file defined within the app I’ve deployed, an issue with rails.service pre-defined by the one-click app, or something else? In short, I am wondering whether the defaults setup in the one-click are taking precedence over the Capistrano files I’ve deployed.

Many thanks for any help.

1 Answer
LProof November 4, 2018
Accepted Answer

Update: it turns out I needed to reboot the droplet and puma. I ran sudo reboot whilst SSHed into the droplet, followed by cap production puma:restart locally, and my site suddenly appeared. I couldn’t find this information related to my question so I’ll leave it here in case it helps anyone else.

It seems the combination of restarting the droplet and puma is what is needed to allow the new nginx configuration to take hold.

For clarity, these are the steps I took to move from the default installation one-click Rails application with the example app at /home/rails/example to a capistrano-based deployment running from /home/rails/apps/appname, where appname is your app:

  1. Setup capistrano locally using this tutorial (https://www.digitalocean.com/community/tutorials/deploying-a-rails-app-on-ubuntu-14-04-with-capistrano-nginx-and-puma). Make sure everything is configured properly and both ssh keys work (i.e. the ssh key that allows you to ssh locally into your droplet, and the ssh key that allows your droplet to pull from your remote git repo).
  2. Remove/move the file /etc/nginx/sites-enabled/rails. (I hid mine at .rails to preserve it in case I needed it later.)
  3. Create symlink between capistrano-defined nginx.conf to replace the default configuration: sudo ln -nfs "/home/rails/apps/appname/current/nginx.conf" "/etc/nginx/sites-enabled/appname". (Apparently it’s important to use the absolute path for this.)
  4. Run sudo reboot on the droplet.
  5. Run cap production puma:restart locally.

This should allow you to deploy from Capistrano locally with cap production deploy and not have to fiddle with the nginx files on the droplet. This workflow is compiled from probably a dozen different tutorials and posts elsewhere, so I hope this helps someone trying to deploy a Rails app to production on DigitalOcean using Capistrano.

Have another answer? Share your knowledge.