How to add a Rails application to an nginx server

by Jason Swett,

This is part 3 of my series on how to deploy a Ruby on Rails application to AWS. If you found this page via search, I recommend starting from the beginning.

Overview of this step

In this step we’re going to clone our Rails application, make sure the server’s Ruby version matches the application’s Ruby version, and install the application’s dependencies.

1. Clone the application

For the rest of this tutorial I’m going to use a certain Rails application of mine called hello_world. Its repo is public, so feel free to use my app instead of yours for practice if you want.

2. Install the right version of Ruby

When we set up nginx and Passenger in the previous step, we configured the server with Ruby 2.5.

Unfortunately, my hello_world application uses Ruby 2.6.5, so Ruby 2.5 isn’t going to work. We could have configured Ruby 2.6.5 from the start but I didn’t want to add more steps and make things more confusing.

We could install Ruby any way we want but I’m going to use RVM.

3. Configure nginx to use the new Ruby version

Now we need to get the path of the Ruby we just installed.

Copy and paste the Ruby path (for me it was /home/ubuntu/.rvm/gems/ruby-2.6.5/wrappers/ruby) into /etc/nginx/sites-enabled/default.

Here’s what my full /etc/nginx/sites-enabled/default looks like for reference.

4. Bundle install

Before we can serve our Rails app we need to install its dependencies using bundle install. Before we can do that we need to install Bundler.

Also, I’m using PostgreSQL, and in order to successfully install the pg gem, I need to have libpq-dev installed.

Now we can bundle install.

5. Set proper permissions

In order to do its business, the nginx user, www-data, needs to have ownership of our project directory.

6. Verify that this step worked

Lastly, we’ll verify that everything we’ve done so far has worked.

The app can’t be served yet because we haven’t yet done all the necessary steps, so we can’t verify the success of this step by checking to see if the app can be served. All we can do is check to see that the error message we get when we try to serve the app is the error message we expect.

Let’s tail the nginx log file so we can see whatever errors that come across.

Now visit your EC2 instance’s URL in the browser. What will almost certainly happen is you’ll get some sort of error. Below is the expected error.

Error: The application encountered the following error: Missing secret_key_base for 'production' environment

If you see the above error regarding secret_key_base, you’re all set for this step. If you get a different error, there’s a problem.

Now we can move onto the next step, setting up Rails secrets.

  •  
  •  
  •  
  •  

2 thoughts on “How to add a Rails application to an nginx server

  1. Alberto

    I followed the steps and I managed to run bundle install without problems, but there are no errors on /var/log/nginx/error.log and the nginx page is still displayed. Nginx and passenger are working but It looks like the rails application is not running.

    Reply
    1. Jason Swett Post author

      The next thing I would check in that case is whether the root directive in /etc/nginx/sites-enabled/default matches where your Rails application is located on the server’s filesystem.

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *