Introduction
Instructions for setting up Ubuntu 12.04.4 LTS to host a Ruby on Rails Application with nginx as the web server, Phusion Passenger as the application server and PostgreSQL as the database. This has been tested on a clean installation of Ubuntu 12.04.4, but should work on existing installation and different versions with little changes as well. This is one of my first blog posts so take that into account when giving feedback.
Installation
Write the following inside a script file and run it (Or run the commands one by one). It will add the Phusion Passenger repositories to apt sources, install it, install rbenv, ruby and bundler, stuff required to compile Ruby, PostgreSQL and nginx. Feel free to customize the script to your needs. If you prefer RVM to rbenv just change the corresponding lines etc.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Configuration
Change the following lines in /etc/nginx/nginx.conf:
1 2 3 4 5 6 7 8 9 10 11 |
|
to:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
(Default ruby installation directory is /home/<username>/.rbenv/versions/<ruby-version>)
Generate a ssh keypair and add your public key to where you deploy from (If you need to and use git with ssh). Otherwise just do whatever you need to get your application to the server. Guide to ssh key generation can be found at https://help.github.com/articles/generating-ssh-keys
Clone your repo to a directory of your choosing (If you use git):
1
|
|
Add an user to postgresql with the name and password you specified in the production database config in your database.yml:
1
|
|
1
|
|
1
|
|
To exit psql prompt, type:
1
|
|
Change the local authentication line in /etc/postgresql/9.1/main/pg_hba.conf:
1 2 3 4 5 6 |
|
to:
1 2 3 4 5 6 |
|
to allow password authentication to postgresql
And then restart postgresql:
1
|
|
source ~/.bash_profile to get access to rbenv and new commands of the gems that were installed:
1
|
|
Go to your rails application root and run bundle install:
1
|
|
Make sure your secret token is specified in your application’s config/application.yml if you’re using the figaro gem. If not, you can generate a new one with the following command in your rails apps’s root:
1
|
|
Then just add that to <your/rails/application/root>/config/application.yml:
1
|
|
If you are using ssl, now is the time to import your certificates or generate them. The following configurations assume they are stored in /etc/nginx/ssl/
Create a file to /etc/nginx/sites-enabled/ with the name of the name of your application and add the following contents Replace leading whitespaces with tabs if nginx complains about them:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Then modify /etc/nginx/nginx.conf to only include that site configuration:
1 2 3 4 5 6 7 |
|
Go to your rails application root and run the following commands:
1
|
|
Then restart nginx:
1
|
|
Your application should now be served properly by nginx on port 80 :) If you run into problems, check the logs at /var/log/nginx to see whether they help identify the problem.