This is part 4 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
We need to set up the Rails secrets security feature. It’s a relatively simple step although it does require us to jump through a few hoops.
1. Run rails credentials:edit
First we need to give permission to the current user, ubuntu
, so we can make changes.
cd /var/www/hello_world
sudo chown -R ubuntu:ubuntu .
When we run the rails credentials:edit
command, it will have us edit a credential file. We need to specify the editor that should be used for this action. In this case I’ll specify Vim.
export EDITOR=vim
Now we need to delete the existing config/credentials.yml.enc
or else there will be a conflict.
rm config/credentials.yml.enc
With all these things out of the way, we can finally edit our credential file. No changes to the file are necessary. Just save and exit.
rails credentials:edit
Lastly, we need to give permissions back to the nginx user, www-data
. Restart nginx afterward.
sudo chown -R www-data:www-data .
sudo service nginx restart
2. Verify success
Now, if you visit your EC2 instance’s URL in the browser, you should get this error:
The significant thing about this error is that it’s coming from Rails, not nginx. So we’ve made it all the way “to Rails”.
If you run tail -f log/production.log
before refreshing the page, you should be able to see the exact error that’s occurring. It should be something like this:
This is telling us there’s no PostgreSQL server running, which is true. We can fix this problem in the next step: setting up our RDS database.
Just for those who follow me! I had an issue where I could not see the Ruby error, but remained on the nginx welcome page. This was because I had not updated the root directory in the stage before – i.e. the following line needs to be updated in your nginx server information:
root /var/www/hello_world/public;
The line is given in the example file on the step before, but you aren’t explicitly told to change it so I originally missed it. This will then fix the issue!
Great tutorial