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 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