Tutorial for installing Ruby on Rails on a VPS
This tutorials shows which steps there are to take for making a VPS suitable for production ready websites, build in Ruby on Rails. There are many guides for setting up a VPS, but they seem outdated or not particular suited for me. This is why I created this tutorial for future reference. In this tutorial i’m making some assumptions:
- I use Git
- I use PostgreSQL as database
- I use ImageMagick
- I use Passenger as an application server with Nginx as webserver
- I use Capistrano for deployments
Last but not least, the VPS needs to be secure
Getting a VPS
There are many hosters on the web, like Digital Ocean, AWS, Linode, etc. I’ve very good experience with Digital Ocean.
Add SSH key
Always be sure to connect with SSH to your VPS. To setup and add an SSH key to the server, follow the steps on this great tutorial: https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys--2
Update VPS
Just need to check if your system and dependencies are running on the latest versions.
apt-get update && apt-get upgrade -y
Directly fix locale errors
When you get some annoying warnings about locales not being found or set. Just fix it with this with these commands.
sudo locale-gen en_US en_US.UTF-8
sudo dpkg-reconfigure locales
With the last command, you need to follow the instruction on screen, but en_US is already selected, so you can tab
and enter
those screens, unless you need another language.
Install needed dependencies
To make the VPS ready, install the following dependencies. Be aware of the following because we'll install:
- PostgreSQL database
- ImageMagick
- NodeJS (needed for precompiling assets, instead of ‘therubyracer’ gem)
- Git (needed for deployments)
- Ufw (firewall)
- HTop for system monitoring
- Vim for editing files directly
To make the VPS ready:
apt-get install vim htop build-essential git-core python-software-properties openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev autoconf libc6-dev libgdbm-dev libncurses5-dev automake libtool bison subversion pkg-config libffi-dev libcurl4-openssl-dev libpq-dev imagemagick ufw postgresql postgresql-contrib libpq-dev nodejs
Rbenv
I prefer Rbenv over RVM (another Ruby environment manager). So I stick with Rbenv for this tutorial. To install Rbenv:
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
Now we need to export the PATH, to make Rubies accessible in the command line.
echo 'export PATH="$HOME/.rbenv/bin:$PATH"'>> ~/.profile
echo 'eval "$(rbenv init -)"' >> ~/.profile
exec $SHELL -l
That’s it, the Ruby environment manager is set-up!
Ruby-build
There is a way to simply download and install (new) ruby versions and it can be accomplished with ruby-build. In the future you may want to update to a newer Ruby version, only thing to do is to update (git pull) in this folder
cd ~/.rbenv/plugins/ruby-build
.
And than you can install a version as described below.
To install it the first time. Clone ruby-build to its folder in “~/.rbenv/plugins”
Do this as followed:
cd ~/.rbenv/plugins && git clone https://github.com/sstephenson/ruby-build.git
After this we can continue with install ruby!
Install a Ruby version
To install a new Ruby version, we simply can do rbenv install 2.3.1
.
The version ‘2.3.1’ stands for the Ruby version to be installed and is the latest version at the time of writing this tutorial.
No documentation, please!
I never read the docs from gems on my server, so i want to disable this (also faster when installing the bundle).
echo 'gem: --no-document' >> ~/.gemrc
Setup the firewall
By default I want to deny access to this VPS. But allow SSH and HTTP(S). So thats what we are going to do with multiple commands at once..
ufw default deny && ufw allow ssh && ufw allow http && ufw allow https
And enable it, confirm it with an ‘y’..
ufw enable
.. done!
Setup PostgreSQL
By installing all the dependencies we already installed PostgreSQL. Now there is only need to activate it and make it accessible from this server.
The version number could be different with your installation. To activate PostgreSQL do:
pg_createcluster 9.4 main -start
Install Passenger with Nginx
The last thing we need to do is setting up passenger (the application server) with Nginx (handles the requests, sends it to passenger). To do this, follow the instructions on this tutorial: https://www.phusionpassenger.com/library/install/nginx/install/oss/trusty/
Phusion has made an official ubuntu repository for the passenger package, which is makes installing and updating a breeze. So i would definitely recommend that. After this, you're done setting up the server!
Party hard! You can now enjoy your freshly installed server and begin deploying an app with Capistrano.
Things left for you to do:
Before you can view your app in production, there are some other things to do:
Create user and database in PostgreSQL for your app
PostgreSQL add or create a user account and grant permission for database, which if found here: https://www.cyberciti.biz/faq/howto-add-postgresql-user-account/Point Nginx config to your app
Pointing the Nginx config to your application: https://www.phusionpassenger.com/library/walkthroughs/deploy/ruby/ownserver/nginx/oss/trusty/deploy_app.html#rails_edit-nginx-configuration-file
Let me know if this tutorial was any useful to you! Don’t be shy to ask for help if you encounter a problem.
installing Ruby on Rails nice explanation .
Thanks! Did you run into any problems?