Setting up Rails with MySQL on DigitalOcean

30.Mar.2016

Standard DigitalOcean one-click install doesn’t offer MySQL database with Rails app anymore, and the process can be somewhat cumbersome.

We have assembled a super-quick step-by-step guide that can help you get your staging Rails up and running in matter of minutes.

Step 1: Setup DigitalOcean server one-click-install with Postgre and Rails
one-click-install-postgre-rails

Step 2: Create private key
SSH into your new server after it’s up and running, and create private key so you can connect to your Git project.

1
ssh-keygen -t rsa

After you created your private key, copy it from ~/.ssh/id_rsa.pub and add it to your Bitbucket or Github. In our example – it’s Bitbucket:
add-deployment-key-to-bitbucket-repository

Step 3: Go to your home Rails folder and clone your project

1
2
cd /home/rails
git clone git@bitbucket.org:stuntcoders/project_name.git

Step 4: Change permissions and replace the current project

1
2
3
chown rails:rails -R project_name
mv rails_project rails_project_backup
mv project_name rails_project

Step 5: Install generally needed tools and MySQL
Perform each line as separate task, and read lines before copying them. You want to fill these with your data, and not sample data from our example.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Install essentials
sudo apt-get install -y build-essential zlib1g-dev git-core sqlite3 libsqlite3-dev
 
# Install postfix
debconf-set-selections <<< "postfix postfix/mailname string your-domain.com"
debconf-set-selections <<< "postfix postfix/main_mailer_type string 'Internet Site'"
apt-get install -y postfix
 
# Install vim, curl, git
apt-get install -y vim git curl wget
 
# Set your root user password to root
# Install MySQL quietly
sudo apt-get -y install mysql-server-5.5
sudo apt-get install libmysqlclient-dev -y
 
# Setup DB and user
mysql -uroot -proot -e "CREATE DATABASE IF NOT EXISTS rails"
mysql -uroot -proot -e "GRANT ALL PRIVILEGES ON rails.* TO 'rails'@'localhost' IDENTIFIED BY 'password'"
mysql -uroot -proot -e "FLUSH PRIVILEGES"

Step 5: Stop the server

1
service unicorn stop

Step 6: Run rake to setup database

1
2
bundle install
rake db:setup RAILS_ENV=production

Step 7: Compile assets & start the server

1
2
rake assets:precompile RAILS_ENV=production
service unicorn start

If you wish to to setup Rails 5…

Step 1: Install Ruby 2.2.2+ (in our case 2.3.1)

1
2
3
rvm install 2.3.1
rvm use 2.3.1
rvm --default use 2.3.1

Step 2: Make sure Unicorn uses our Ruby version
Edit unicorn config to make it use Ruby 2.3.1 instead of 2.2.1. Just replace the numbers in /etc/default/unicorn.

1
vim /etc/default/unicorn

Step 3: Do not forget secrets.yml
Create config/secrets.yml with folowing content:

1
2
3
4
5
6
7
8
development:
  secret_key_base:
 
test:
  secret_key_base:
 
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

Step 4: Get ready and start the app
To start your Rails 5 app on DigitalOcean, just run following:

1
2
3
4
5
cd /home/rails/rails_project
bundle install --without development test
rake db:migrate RAILS_ENV="production"
rake assets:precompile RAILS_ENV="production"
service unicorn restart