Skip to content

Instantly share code, notes, and snippets.

@vikashvikram
Last active August 22, 2019 10:37
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vikashvikram/9e98d58ebd97a5e34532 to your computer and use it in GitHub Desktop.
Save vikashvikram/9e98d58ebd97a5e34532 to your computer and use it in GitHub Desktop.
How to setup nginx and passenger for a ruby on rails application on Mac OS X

Setting up nginx with passenger for a Ruby on Rails app on Mac OSX

Nginx with passenger is a very popular choice for production and staging environment for rails applications. It is always a good practice to replicate the production and staging environments on our local systems so that we do not have last minute surprises while deploying. Most of the Ruby on Rails developers use Mac OS as their development environment so this document is regarding setting up Nginx with on it for an already existing rails app. The assumption for this doc is that you do not even have ruby installed on your system. So feel free to skip the steps that are not relevant.

Installing Ruby, setting up Rails app, installing gems and nginx

The recommended way to work on a rails app is to use rvm for installing ruby. We need to have multiple versions of ruby and our gems which makes it resonable to isolate the dependency environments for each project. we achieve it by using different gemsets for our projects. There are other alternative and each are equally good but for this doc we will follow rvm. Here I am assuming that ruby version for the app is 2.2.2 and the app is demo. The gemset I will use for the app will also be called demo (It is a good pratice to have gemsets with same name as those of projects so that we do not get confused in future). The Mac OS that I have on my laptop is El Capitan 10.11.3.

  1. install rvm following instructions on https://rvm.io/rvm/install

  2. install ruby with your version using rvm. e.g. to install ruby 2.2.2, run rvm install 2.2.2

  3. Go to /var/www directory by executing command cd /var/www

  4. Take clone of your code repo by executing git clonse repo_url. here replace repo_url with git repo url for your app

  5. set the ruby version for the code by executing. This will ensure that we come to this folder, ruby version is automatically set.

    echo 2.2.2 > demo/.ruby-version

  6. create gemset called demo for the app

    rvm gemset create demo

  7. make sure app uses demo gemset. Again this file will ensure that when we are inside this folder, correct gems will be used.

    echo demo > demo/.ruby-gemset

  8. go to app root directory. In our case it is demo folder in /var/www/demo

    cd demo

  9. install bundler gem

    gem install bundler

  10. install all gems for the demo app

bundle install

  1. come out of directory and set proper permissions

cd ..

suppose current user is vikram and group of the user is wheel, we will set the permissions for the code repo

sudo chown -R vikram demo

sudo chgrp -R wheel demo

  1. Go to the code repo and install passenger gem if it is not part of Gemfile

    cd demo && gem install passenger

  2. install nginx and configure it follwing instructions coming up during execution of below command

    rvmsudo passenger-install-nginx-module

  3. note down passenger_root and passenger_ruby values that you get in the end of execution of above command.

set nginx configuration

you will find a default nginx.conf file under conf folder of nginx. It will have some default values in it. You can change some of them as per your requirement.

  1. Set worker_processes according to number of processes you want run at a time. It should not be more than number of cores in your processor.
  2. Set worker_connections to number of threads you want in each process. I normally set it to 1024.
  3. Under server section add passenger_enabled on; if it is not there.
  4. Under server section add root /private/var/www/demo/public; to connect nginx to your app. My app is demo. Replace it with app root directory of your app. Please notice that we have to prefix the directory address with /private.
  5. Under http section add passenger_root /Users/vikram/.rvm/gems/ruby-2.2.2@demo/gems/passenger-5.0.22; . Don't forget to replace the value for passenger_root that you get while running command in step 13.
  6. Under http section add passenger_ruby /Users/vikram/.rvm/gems/ruby-2.2.2@demo/wrappers/ruby; Don't forget to replace the value for passenger_ruby that you get while running command in step 13.
  7. By default, nginx run in production environment. If you want to set it to something else, set it using passenger_app_env command
  8. I have also added a sample nginx.conf for reference here.

Setup Database

  1. create database (I am doing it for production environment) if not already created

RAILS_ENV=production rake db:create

  1. run migrations (again for production environment)

RAILS_ENV=production rake db:migrate

Start and restart nginx

  1. start nginx with sudo nginx
  2. Restart nginx with sudo nginx -s reload
  3. In case your system does not recognize nginx command, add the nginx bin to your path.
@prabhat4ever
Copy link

Good One 👍

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