Creating a rails app on Digital Ocean in 15minutes
Learn how to migrate your ruby-on-rails app from Heroku to Digital Ocean.
UPDATED VERSION IS HERE
Even thought this post is not directly related to IOT technologies, running a webserver/webapplication is often required. Running on Heroku can be quite expensive especially if the app is not getting as much traffic/revenues as expected.
Simple way:
First we need to add more swap memory:
sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
You can check the swap space now (Optional):
free -h
Now let's install everything:
sudo apt-get update
command curl -sSL https://rvm.io/mpapis.asc | gpg --import -
curl -L get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm requirements
rvm install 2.4.1
sudo apt-get install -y libcurl4-openssl-dev nodejs
rvm rubygems current
gem install rails
gem install passenger
rvmsudo passenger-install-nginx-module
Install RVM:
From this tutorial
sudo apt-get install -y software-properties-common
sudo apt-add-repository -y ppa:rael-gc/rvm
sudo apt-get update
sudo apt-get install -y rvm
Create a user :
You can follow this tutorial
useradd appname_production
Add user to sudoers:
usermod -aG sudo appname_production
Install postgresql:
From these notes : Creating user and adding access in Postgresql
sudo apt-get -y install postgresql postgresql-contrib
Start postgres:
sudo -u postgres psql
And run the following command
createuser owning_user
- Exit postgres (CTRL-D)
Setting up the database:
SSH into your machine
From shell do :
-
sudo -i -u postgres
-
createuser --interactive
Here you should follow the questions. You will have the following questions :- Enter name of role to add: user1
- Shall the new role be a superuser? (y/n) y
-
createdb appname_production
More info here:
Install PSQL for digital ocean
Copy database (if you have one already)
If you're migrating and have an existing database, here are the instructions. If not, then skip to next section.
Update the database with scp (from your mac):
scp budata.sql root@serverip:/
Log into your user name :
psql databasename < data_base_dump
Now we can access the database this way:
sudo -u appname_production psql
Change the password:
sudo -u appname_production psql
appname_production-# \password appname_production
Update the config/database.yml
Install RUBY 2.4.1:
rvm install ruby-2.4.1
rvm --default use ruby-2.4.1
gem install bundler --no-rdoc --no-ri
sudo apt-get install -y nodejs && sudo ln -sf /usr/bin/nodejs /usr/local/bin/node
Install rails :
gem search '^rails