Setting up Rails app in Ubuntu machine (RVM + Sidekiq + Nginx + Puma + Capistrano)
After years of working with Ruby on Rails and deploying in production many apps i have isolated the steps that usually i use when deploying new apps. Each project has their own customs but commonly this is what works.
Honestly each time i make a new deployment a small detail comes up and got to double read what a new error is about.
You should also note that there are other fancy solutions like using Docker, Heroku, AWS Beanstalk and so on, but in case you need to go the old reliable custom way here is a guide for it.
Here is a small diagram on how parts integrate:
You should open firewall (if not open already) for:
- SSH (22 by default)
- 80 (non HTTPS)
- 443 (HTTPS)
Depending on your needs you can choose not to enable 80 and 443 at the same time but only just 1 of them depending if your site needs or not SSL connection (Normally you would). Also as a small security improvement you can change default 22 port to another one so you distract a little malicious bots.
In our setup the nginx acts as a web server and puma as application server, that’s why we need both of them working; there are many alternatives but i will focus in this pair. The rails app will off course handle our application code, the Sidekiq will work as our Ruby background processor, Redis as the storage of the last one and finally Capistrano will make us automate the entire deployment process through ssh and other scripts.
Assuming you are using an Ubuntu 18, similar environment or any other linux distro that minimum supports Systemd, first update your index:
sudo apt-get update
sudo apt-get install software-properties-common
Now you should install Ruby, i like using RVM but you can use plain ol’ installation, chruby or rbenv. In case you like rvm:
sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt-get install rvm
After installing usually i log out and restart the session, even if the output says you can type a command to reload it, it never worked for me, so i’m not even posting it in this story 😅.
As the moment of writing this i migrated my most recent projects to 2.7.0 so i recommend it and is stable enough except for a lot of deprecation warnings but lets disregard that.
rvm install 2.7.0
Now you can clone your project wherever you desire but save that path for the future. For our purposes let’s assume the following path:
/path/to/rails . Also you should consider that where you clone your project you should have setup your own permissions and proper access rules.
Proceed into installing bundler gem, so you install all your gems in from your project. First change directory to your app’s path, then install bundler and finally install your gems:
gem install bundler
Normally you will find some errors around installing gems that includes other dependencies, for example if you are installing the
pg gem (because you intend to use postgres as your db engine) you would first have to install the postgres dev libraries:
sudo apt-get install libpq-dev
Same applies if you use other gems with dependencies (For example the mysql2 client gem).
After the bundle install commands success you can now configure nginx, sidekiq and puma, i’ve created independent stories so it’s easier to read:
Now the most exciting part, setting up Capistrano…