Skip to content

Instantly share code, notes, and snippets.

@diraulo
Last active March 1, 2017 11:46
Show Gist options
  • Save diraulo/dd590878ae7094f74f36 to your computer and use it in GitHub Desktop.
Save diraulo/dd590878ae7094f74f36 to your computer and use it in GitHub Desktop.
Deploying a Rails Application on a DigitalOcean VPS

Run the following as root

$ dd if=/dev/zero of=/swapfile bs=1M count=[swap_size] # swap_size = 1024 for 1GB
$ chmod -R 0600 /swapfile
$ mkswap /swapfile
$ swapon /swapfile

Add the swap to your fstab. To do so edit /etc/fstab and add the following line

/swapfile  swap      swap    defaults         0 0
# generate ssh key
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
# Upload public key to remote server
cat ~/.ssh/id_rsa.pub | ssh username@xxx.xxx.xxx.xxx "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
  1. Sign-up for DO account https://www.digitalocean.com/?refcode=9ce07ab8bad

  2. Create a Droplet (Ubuntu 14.04)

  3. Prepare the VPS (installing Git, RVM and Ruby)

sudo adduser deploy        # create a new user
sudo adduser deploy sudo   # add user to sudo group
su deploy                  # login as new user

sudo apt-get update && sudo apt-get upgrade

# Add PPA repo for git
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update

# Install Git and Ruby

## Git and dependencies
sudo apt-get install git-core curl zlib1g-dev build-essential libssl-dev libreadline-dev libyaml-dev libsqlite3-dev sqlite3 libxml2-dev libxslt1-dev libcurl4-openssl-dev python-software-properties libffi-dev -y

## Ruby using `rvm`
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3
sudo apt-get install libgdbm-dev libncurses5-dev automake libtool bison libffi-dev
curl -L https://get.rvm.io | bash -s stable

# Add the following line to the end of your bashrc
nano ~/.bashrc
>> source ~/.rvm/scripts/rvm



rvm install 2.2.3
rvm use 2.2.3 --default
ruby -v

# Now we tell Rubygems not to install the documentation for each package locally and then install Bundler
echo "gem: --no-ri --no-rdoc" > ~/.gemrc
gem install bundler
  1. Install nodejs
# Install node 4.x
curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
sudo apt-get install -y nodejs
node -v  # Verify version of node installed
npm -v   # Verify that you have npm installed too
  1. Install and config nginx
sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7
sudo apt-get install -y apt-transport-https ca-certificates

# Add Passenger APT repository
sudo sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger trusty main > /etc/apt/sources.list.d/passenger.list'
sudo apt-get update

# Install Passenger & Nginx
sudo apt-get install -y nginx-extras passenger
sudo vim /etc/nginx/nginx.conf
# sudo nano /etc/nginx/nginx.conf

##
# Phusion Passenger
##
# Uncomment it if you installed ruby-passenger or ruby-passenger-enterprise
##

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
passenger_ruby /home/deploy/.rvm/gems/ruby-2.2.3/wrappers/ruby; # use the command `passenger-config --ruby-command` to get the path to ruby executable

Start the web server

sudo service nginx start

Now if you copy the ip address of your droplet into a browser you should be able to see the nginx welcome page

  1. Install PG
sudo apt-get install postgresql postgresql-contrib libpq-dev
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
server_name mydomain.com;
passenger_enabled on;
rails_env production;
root /home/deploy/myapp/current/public;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
sudo su - postgres
psql

#Change user password
ALTER USER postgres WITH PASSWORD 'postgres';
\q
exit

@gustafr
Copy link

gustafr commented Nov 18, 2015

Here's a couple of suggestions on how to improve this tutorial:

  1. Instruction on how to connect to the droplet from terminal.
  2. Clearify that ">>" shouldn't be included when adding "source ~/.rvm/scripts/rvm" to ~/.bashrc
  3. Clearify that the following lines should be uncommented by removing #
    passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;
    passenger_ruby /home/deploy/.rvm/gems/ruby-2.2.3/wrappers/ruby; # use the command passenger-config --ruby-command to get the path to ruby executable

@gustafr
Copy link

gustafr commented Nov 19, 2015

Part 2:
Add to Gemfile: gem 'mina'
Run command: mina init
Uncomment following line in config/deploy.rb: require 'mina/rvm' # for rvm support. (http://rvm.io)

When logged in to Digital Ocean from Terminal:
mkdir -p /var/www/YOUR_APP_NAME
sudo chown -R deploy /var/www/YOUR_APP_NAME

Generate ssh key on your server: ssh-keygen -t rsa -b 4096 -C "deploy@YOUR_DROPLET"
Show generated ssh key on screen: cat ~/.ssh/id_rsa.pub
Copy the key to your Github repo/settings/Deploy Keys - add a new deploy key, no write access
Run command: repo_host=echo git@github.com:GIT_USERNAME/GIT_REPO.git | sed -e 's/.*@//g' -e 's/:.*//g'
Command echo $repo_host should respond: github.com
Run command: repo_port=echo git@github.com:GIT_USERNAME/GIT_REPO.git | grep -o ':[0-9]*' | sed -e 's/://g'
Run command: repo_port=22
Run command: ssh-keyscan -p $repo_port -H $repo_host
Run command: sudo apt-get install libgmp3-dev
Run command: mina deploy --verbose, will fail
In database.yml file under production changed username and password to postgres.
On server run command: sudo su - postgres
Run command: psql
Run command: ALTER USER postgres WITH PASSWORD 'postgres';
Run command: \q
Run command: exit
Run command: sudo nano /etc/postgresql/9.3/main/pg_hba.conf
Scroll down to section "# Database administrative login by Unix domain socket" and on all change from peer to trust. Save and exit.
sudo service postgresql reload
Run command: mina deploy --verbose, should work now
cd /etc/nginx/sites-available/
sudo touch YOUR_APP_NAME
sudo nano YOUR_APP_NAME - paste the content in sample_hostfile.conf from above
On the line "server_name mydomain.com;" replace mydomain.com with your own domainaddress
Change root to /var/www/YOUR_APP_NAME/current/public;
sudo ln -s /etc/nginx/sites-available/YOUR_APP_NAME /etc/nginx/sites-enabled/YOUR_APP_NAME
sudo service nginx restart
To view the logs for errors you can run command: sudo tail -f /var/log/nginx/error.log

@gustafr
Copy link

gustafr commented Nov 27, 2015

If you use paperclip or any other gems that uploads files locally to your public folder or similar, you will have to add that folder to your shared folder in order to make the file persistand between releases.
You do this by specifying in your deploy.rb which files/folders that should be shared like so:
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log', 'public/system']

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment