Adithya
Adithya

Reputation: 163

Making nginx work with ruby on rails

I am trying to run ruby on rails on nginx.

I have setup ruby on rails and installed the passenger gem. I all ready have an nginx running how do I integrate it with ruby on rails.

I enabled this in nginx.conf:

passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini; 
passenger_ruby /usr/bin/ruby;

And also this in my virtual host /etc/nginx/sites-available/default

passenger_enabled on;

After this change nginx fails to restart.

Can some one please help me with this I am running ubuntu 14.04

Upvotes: 0

Views: 922

Answers (1)

Pavel Tkackenko
Pavel Tkackenko

Reputation: 953

I will try to explain it step by step. Nginx + RoR + Capistrano

1. Create a user (if needed)

adduser deployer

2. Move user to sudo users

visudo
deployer ALL=(ALL:ALL) ALL

3. Install bash for him

nano /etc/passwd
Меняем /bin/sh на /bin/bash

4. Change ssh-port for security

nano /etc/ssh/sshd_config
Change Port 22 on Port XXXX (where XXXX is any number)

5. reload ssh

6. Enter by ssh with our new user

ssh -p XXXX [email protected]

7. Update system if needed

sudo apt-get update 
sudo apt-get upgrade
sudo apt-get install curl

8. Install rvm:

curl -L get.rvm.io | bash -s stable
source ~/.rvm/scripts/rvm
rvm requirements

9. Install ruby, postgres, etc..

10. Install gem passenger:

gem install passenger

11. Install nginx using passenger

rvmsudo passenger-install-nginx-module

12. Set swap if needed

sudo dd if=/dev/zero of=/swap bs=1M count=1024
sudo mkswap /swap
sudo swapon /swap

13. Change config of nginx (server section):

server {
      listen 80;
      server_name www.yourhost.com;
      root /somewhere/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;
      location ^~ /assets/ 
           { gzip_static on; 
           expires max; 
           add_header Cache-Control public; 
      }
 }

14. Clone good script for nginx start/stop/restart if Ubuntu:

git clone https://github.com/vkurennov/rails-nginx-passenger-ubuntu.git
sudo cp nginx/nginx.conf /etc/init.d/nginx.conf
sudo chmod +x /etc/init.d/nginx/conf

15. Run nginx:

sudo /etc/init.d/nginx start

BOYA!! Check your browser!

Let's go to our app!

1. gem ‘capistrano’ for depoly

group :development do
  gem 'capistrano'
  gem 'rvm-capistrano’
  gem ‘net-ssh’, ‘2.7.0'
end

2. Run:

capify .

Open deploy.rb and set:

set :application, 'app_title'
set :repository,  'app_repo'

5. add:

require 'bundler/capistrano'
require "rvm/capistrano"

load 'deploy/assets’

set :port, XXXX
set :use_sudo, false

set :rails_env, :production
set :branch, "master"
set :deploy_to, "/home/deployer/app_title"
set :user, 'deployer'

role :web, "XXX.XXX.XXX.XXX"                          # Your HTTP server, Apache/etc
role :app, "XXX.XXX.XXX.XXX"                          # This may be the same as your `Web` server
role :db,  "XXX.XXX.XXX.XXX", :primary => true # This is where Rails migrations will run

6. cap deploy:check

7. cap deploy:setup

set :bundle_cmd, "/home/deployer/.rvm/gems/ruby-2.0.0-p451@global/bin/bundle"
set :bundle_dir, "/home/deployer/.rvm/gems/ruby-2.0.0-p451"

set :rvm_type, :user
set :rvm_ruby_string, 'ruby-2.0.0-p451@global'

8. Recipes for deploy:

namespace :deploy do
  task :start do ; end
  task :stop do ; end
  task :restart, :roles => :app, :except => { :no_release => true } do
    run "#{try_sudo} touch #{File.join(current_path,'tmp','restart.txt')}"
  end
end

9. Clone ssh-key on server and add to repo

ssh-keygen -t rsa
cat ~/.ssh/id_rsa.pub

10. Add gem ‘therubyracer’

11. cap deploy:cold

Boya! I opened for questions if smth is not clear.

Upvotes: 2

Related Questions