Skip to content

Instantly share code, notes, and snippets.

@johno
Last active December 7, 2015 23:13
Show Gist options
  • Save johno/5db793a089d61aa558ed to your computer and use it in GitHub Desktop.
Save johno/5db793a089d61aa558ed to your computer and use it in GitHub Desktop.
Steps necessary to deploy a standard Rails app to an Ubuntu 14 box with a Postgres database and DelayedJob.

Ubuntu Rails Deploy

This is a walkthrough for deploying a Rails app on an Ubuntu Server with Nginx, Passenger, Postgres, and DelayedJob.

Basic required packages

sudo apt-get update
sudo apt-get install curl git nodejs

Install RVM and Ruby 2.1.3

This app requires RVM to manage Ruby versions and gemsets. You can install RVM and Ruby 2.1.3 with the following:

gpg --keyserver hkp://keys.gnupg.net --recv-keys D39DC0E3
\curl -sSL https://get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm requirements # This will take a while...
rvm install ruby-x.x.x
rvm rvmrc warning ignore allGemfiles
gem install bundler

Install and configure postgres

sudo apt-get install postgresql postgresql-contrib libpq-dev

sudo su postgres -c psql
postgres=# CREATE ROLE <username> SUPERUSER LOGIN;
postgres=# \q

Install the Rails project

First, you need to clone the repository and cd into it.

git clone https://url-for-git-repo.com/git-user/project-name.git && cd project-name

Then install the gem dependencies:

bundle --without development test # This will take a while...

And copy over the configuration files:

cp config/application.yml.sample config/application.yml
cp config/database.yml.sample config/database.yml

It is also advisable to run the test suite as a sanity test (if doing so run $ bundle --without development):

rspec

Running the app

rails s

Troubleshooting Postgres

Locales

If PG complains about your locale see https://gist.github.com/turboladen/6790847

Peer Authentication

If PG fails for authentication see http://stackoverflow.com/questions/2942485/psql-fatal-ident-authentication-failed-for-user-postgres

Create, migrate, and seed the database

rake db:create RAILS_ENV=production
rake db:migrate RAILS_ENV=production
rake db:seed RAILS_ENV=production
rake assets:precompile

Install and configure Passenger

gem install passenger
rvmsudo passenger-install-nginx-module

Here, the install may complain about missing dependencies. You will need to install them and then execute rvmsudo passenger-install-nginx-module to restart the installation.

Configuring nginx

sudo nano /opt/nginx/conf/nginx.conf

Add the following to server block:

http {
  # ...
  server {
    # ...
    passenger_enabled on; 
    root /path/to/project-name/public;
    # ...
  }
  # ...
}

Which nginx

If you have nginx in /usr/bin, you will need rename it and link the nginx that was installed by passenger-install-nginx-module

cd /usr/sbin
mv nginx nginx.dist
ln -s /opt/nginx/sbin/nginx .

Update crontab

Cron jobs are used for a few background tasks that keep the UI up to date with the VC, you can add them to a crontab with:

whenever --update-cron projectname

Compile the assets (CSS, images, and Javascripts)

This will minify, uglify, and concatenate static assets so they're ready to be served up by the app.

rake assets:precompile

Run the nginx service

sudo service nginx start

Installation complete

That's it, now open your browser and navigate the the IP specified in the nginx conf.

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