loading...
Cover image for A friendly guide to what you should do BEFORE deploying your rails application!

A friendly guide to what you should do BEFORE deploying your rails application!

prpe profile image Muhammadreza Haghiri ・3 min read

Deploying a rails app, can be really painful and hard. One of my friends once said deploying a rails app is like founding a company. By the way, it's part of founding your internet company or startup, if you chose RoR as your backend framework.

Recently, I had to deploy an application for one of my friends. To be honest, I wrote a guide on how you can deploy rails app before. You can find it here. In this article, I tell you whatever I didn't tell in the previous post. So, fasten your seat belts and let's dive deep into the ocean of rails apps.

Installing Ruby

I personally use RVM to install ruby. I don't explain how you can install ruby on your machine. Just click on the link and learn how you can do it yourself!

Getting Your Application Ready

Setting your environment up

This part, is actually everything you need to know about pre-deploy stage. Other parts of this article are not as important as this part to be honest.

For deploying our app, we need to set something up. I just list them below :

  • RAILS_ENV : This environment variable actually determines the environment of your choice. If you don't set it up properly, it will put your app in development environment and not production.
  • SECRET_KEY_BASE : This environment variable is also for the secret key that your app uses. For this one, please be careful. Because other frameworks or apps can be using the same variable name, it may make your project less secure to deploy all of those apps with the same user or in the same environment.

Fine! Now you know what we're going to add to our environment. Open ~/.bashrc or ~/.zshrc on your server (in case you use FreeBSD, you may have the option of ~/.cshrc as well) and add this line at the end :

export RAILS_ENV=production
Enter fullscreen mode Exit fullscreen mode

Now navigate to your app's directory (I assume it's on your home folder so it is in ~/myrailsapp) and then run this:

rake secret
Enter fullscreen mode Exit fullscreen mode

It gives you a key. Copy it and back to ~/.bashrc (or ~/.zshrc or ~/.cshrc) and then add this line to that file as well :

export SECRET_KEY_BASE="iamthekeythatrakegaveyoudeardeveloper"
Enter fullscreen mode Exit fullscreen mode

For more information, replace what I have put in double quotes says "I am the key that rake gave you dear developer". So of course you have to put the key you've got from rake here.

Further settings

Now everything is all set. Just remember this part is not that hard to understand because you may have done it before reading my article.

MySQL database settings

I've got a Mac, and surprisingly, when I made an app which used MySQL/MariaDB, my config/database.yml looked like this:

default: &default
  adapter: mysql2
  encoding: utf8mb4
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  socket: /tmp/mysql.sock
Enter fullscreen mode Exit fullscreen mode

But I assume you're a Linux user on the server side as well. So for the love of God, change it to this:

default: &default
  adapter: mysql2
  encoding: utf8
  pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
  username: root
  password:
  host: localhost
  port: 3306
Enter fullscreen mode Exit fullscreen mode

By the way, MySQL port and host may differ in your case. So mind all of differences.

Setting up database and assets

Finally the easiest part! Just run these:

rails db:setup
rails db:migrate 
rails assets:precompile
Enter fullscreen mode Exit fullscreen mode

And you also may need to run this as well:

yarn install --check-files
Enter fullscreen mode Exit fullscreen mode

Now, you can deploy your app and enjoy!

Discussion

pic
Editor guide