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.
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
.
-
install rvm following instructions on https://rvm.io/rvm/install
-
install ruby with your version using rvm. e.g. to install ruby 2.2.2, run
rvm install 2.2.2
-
Go to
/var/www
directory by executing commandcd /var/www
-
Take clone of your code repo by executing
git clonse repo_url
. here replacerepo_url
with git repo url for your app -
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
-
create gemset called
demo
for the apprvm gemset create demo
-
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
-
go to app root directory. In our case it is
demo
folder in/var/www/demo
cd demo
-
install bundler gem
gem install bundler
-
install all gems for the
demo
app
bundle install
- 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
-
Go to the code repo and install passenger gem if it is not part of
Gemfile
cd demo && gem install passenger
-
install nginx and configure it follwing instructions coming up during execution of below command
rvmsudo passenger-install-nginx-module
-
note down
passenger_root
andpassenger_ruby
values that you get in the end of execution of above command.
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.
- 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. - Set
worker_connections
to number of threads you want in each process. I normally set it to 1024. - Under server section add
passenger_enabled on;
if it is not there. - 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
. - 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. - 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. - By default, nginx run in production environment. If you want to set it to something else, set it using
passenger_app_env
command - I have also added a sample
nginx.conf
for reference here.
- create database (I am doing it for production environment) if not already created
RAILS_ENV=production rake db:create
- run migrations (again for production environment)
RAILS_ENV=production rake db:migrate
- start nginx with
sudo nginx
- Restart nginx with
sudo nginx -s reload
- In case your system does not recognize
nginx
command, add the nginx bin to your path.
Good One 👍