Installing ruby on rails with Nginx passenger

  • install requisites:

apt-get install ruby ruby-dev ruby-bundler

  • install the Passenger app:

apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 561F9B9CAC40B2F7

apt-get install -y apt-transport-https ca-certificates

sh -c 'echo deb https://oss-binaries.phusionpassenger.com/apt/passenger xenial main > /etc/apt/sources.list.d/passenger.list'

apt-get update

apt-get install nginx-extras passenger

  • install ruby dev packages:

apt-get install libgmp3-dev make gcc g++ libpq-dev

  • set Nginx to use Passenger:

vi /etc/nginx/nginx.conf

  • find the following lines, and uncomment them:

include /etc/nginx/passenger.conf

  • edit the Passenger configuration:

vi /etc/nginx/passenger.conf

  • change the passenger_ruby line to point to your ruby executable:

passenger_ruby /home/deploy/.rbenv/shims/ruby; # If you use rbenv

passenger_ruby /home/deploy/.rvm/wrappers/ruby-2.1.2/ruby; # If use use rvm, be sure to change the version number

passenger_ruby /usr/bin/ruby; # If you use ruby from source

The passenger_ruby is the important line here. Make sure you only set this once and use the line from the example that pertains to the version of Ruby you installed.

Once you've changed passenger_ruby to use the right version Ruby, you can run the following command to restart Nginx with the new Passenger configuration:

service nginx restart

  • In order to get Nginx to respond with the Rails app, we need to modify it's sites-enabled:

server {

   listen 80;
   server_name devtimesheet.apoyar.eu;
   passenger_enabled on;
   rails_env production;
   passenger_friendly_error_pages on;
   root /var/www/timesheet;
   passenger_app_root /var/www/timesheet;
   access_log /var/log/nginx/timesheet_access.log;
   error_log /var/log/nginx/timesheet_error.log;
   # redirect server error pages to the static page /50x.html
   error_page   500 502 503 504  /50x.html;
   location = /50x.html {
       root   html;
   }

}