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$' --all
gem install rails -v 5.1.4
Install more tools:
Make sure you exit POSTGRESQL with CTRL-D.
sudo apt-get -y install rake curl gnupg build-essential libreadline-dev libxslt-dev libxml2-dev python
gem install libv8 -v '3.11.8.12'
sudo apt-get install -y libv8-dev libmagick++-dev webp imagemagick libpq-dev ruby-railties
Install passenger packages:
FOr installing passenger and rails, we are following this guide:
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates
Add our APT repository
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update
Install Passenger + Nginx
sudo apt-get install -y nginx-extras passenger
Enable passenger in nginx conf:
In file : /etc/nginx/nginx.conf
Uncomment the following line :
# include /etc/nginx/passenger.conf;
Now go into your user:
Change the password for user :
passwd appname_production
Then log into it:
su - appname_production
old :
rvm install ruby-2.4.1
sudo apt-get install ruby
sudo gem install libv8 -v '3.11.8.12' -- --with-system-v8
sudo gem install libv8 -v 3.11.8.17 -- --with-system-v8
* `rvm install ruby-2.4.1`
* `gem install bundler`
* `gem install nokogiri -v '1.5.11'`
* `gem install libv8 -v '3.11.8.12' -- --with-system-v8`
* `gem install libv8 -v 3.11.8.17 -- --with-system-v8`
* `gem install passenger`
* `sudo apt-get install libcurl4-openssl-dev`
* `sudo apt-get install nodejs`
new:
sudo apt-get install -y build-essential patch ruby-dev zlib1g-dev liblzma-dev
sudo gem install nokogiri -v '1.5.11'
export rvmsudo_secure_path=1
sudo gem install passenger
sudo apt-get install -y libcurl4-openssl-dev nodejs
NOw install nginx:
rvmsudo passenger-install-nginx-module
If you have issues with ruby racer:
gem uninstall libv8 gem install therubyracer -v '0.11.3' gem install libv8 -v '3.11.8.12' -- --with-system-v8
rbenv install 2.1.1
rbenv global 2.1.1
Install RVM:
Extra Notes:
Running your app:
Setting up your db:
RAILS_ENV=production rake db:create
Testing you app in production:
RAILS_ENV=production rails server
App is located in : /var/www
restarting app:
sudo service nginx restart
Logs for app are in :
/var/log/nginx/access.log
Create a user for PG SQL:
Loading another database:
I have another database on heroku. I can get it this way :
heroku config:get DATABASE_URL --app appname
The output has the format:
postgres://<username>:<password>@<host_name>:<port>/<dbname>.
You can save the content this way :
pg_dump --host=<host_name> --port=<port> --username=<username> --password --dbname=<dbname> > output.sql
And load it up again :
Loading into database:
psql databasename < data_base_dump
Change permission of user:
sudo -u postgres -i
psql
ALTER USER new_user CREATEDB;
Adding swap space to your app:
https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-14-04
Install Jenkins:
Installing jenkins on digital ocean:
Securing the app:
how-to-secure-nginx-on-ubuntu-14-04
References: