Not logged in. Login

Deploying Rails + Nginx + PostgreSQL

You are free to choose other technologies (like Apache or MySQL), but I can't provide instructions for all possible combinations.

You will need Nginx and PostgreSQL installed, as you did in Exercise4. This should include a config file for /etc/nginx/sites-available/default, and creating a PostgreSQL database with the ubuntu user having all permissions on it.

Rails + PostgreSQL

The Ruby Gem pg needs to be added to your Gemfile, and you will need the Ubuntu libpq-dev package installed to build it.

In your database.yml, set your PostgreSQL database for production environments:

production:
  adapter: postgresql
  encoding: unicode
  database: mydb
  pool: 5
  username: ubuntu

When migrating (or doing other management commands), make sure you specify production mode:

rake db:migrate RAILS_ENV=production

Rails + Nginx

Make sure you're forwarding port 80 in your Vagrantfile. The easiest way to get Rails running under Nginx seems to be unicorn. Not that it's easy; just that it's easiest. [Instructions based on How To Deploy a Rails App with Unicorn and Nginx.]

You'll need to add the unicorn gem to your Gemfile.

In your Nginx config file, before the server section, tell it to proxy to the Rails app:

upstream rails {
    server unix:/tmp/unicorn.sock fail_timeout=0;
}

And then set that as the server root:

    root /home/ubuntu/project/blog/public;
    try_files $uri/index.html $uri @rails;
    location @rails {
        proxy_pass http://rails;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }

You will need a unicorn.rb file in your repo, which will serve as a recipe to start up the Unicorn process:

app_dir = "/home/ubuntu/project/blog"
shared_dir = "/home/ubuntu"
working_directory app_dir

worker_processes 2
preload_app true
timeout 30

listen "/tmp/unicorn.sock", :backlog => 64

stderr_path "#{shared_dir}/unicorn.stderr.log"
stdout_path "#{shared_dir}/unicorn.stdout.log"
pid "#{shared_dir}/unicorn.pid"

This runs your Unicorn processes ...

Finally, you'll need to start the uwsgi process on system start. You can do that by creating /etc/init.d/unicorn_rails. Check the USER, APP_NAME, and APP_ROOT variables in that script to make sure they match your arrangement.

As part of your configuration, you'll have to make sure the init script is enabled:

update-rc.d unicorn_rails defaults

In config/secrets.yml you need to specify a secret_key_base for production mode. This should generally be secret and not in your widely-visible codebase, but for this course I'm happy for you to put a static value there (possibly copied from the development mode entry).

Static Files

In its default configuration, files in the public directory will be served as static, so you're probably okay but check to make sure.

See also DeployStatic for more information on configuring for static files.

Updated Sun May 06 2018, 11:37 by ggbaker.