Setup Nginx + Passenger + RVM (ruby and rails) + Redmine 2.5

Recently I had to move a Redmine instance on a new server (Ubuntu 14.04).  When I was lost in process overwhelmed by some ruby, bundler and rake commands, I swore to myself that I would write down al the steps once succeeded. Here we are:

RVM

RVM is a program that let you install a precise version of Ruby (the one required by Redmine for example) and then let you partition all ruby gem in a specific folder (as once agin, Redmine required a precise version of each gem).

Run this as a regular user in your home folder:

\curl -sSL https://get.rvm.io | bash -s stable --rails

This will install RVM in one command. Then, you have to source it to use it:

source ~/.rvm/scripts/rvm

Then install Ruby 2.0.0 as required by Redmine:

rvm install 2.0.0

If you install multiple version of Ruby, use rvm use ruby_version  to switch the version of Ruby you want to work with. Then we will create a specific folder called gemset to store Redmine gems and switch RVM focus on this folder:

rvm gemset create gemset_name    # create a gemset
rvm ruby_version@gemset_name  # specify Ruby version and our new gemset

You will have something similar to this .rvm/gems/ruby-2.0.0-p481@redmine2.5/gems/

Note: Running rvm ruby_version@gemset_name  as your regular user before installing any gem (using gem install  or bundle install  is very important. Moreover, you shouldn’t run the command from Byobu/Screen, or you will have this error like “rvm is not a function”.

REDMINE 2.5

Prior to the Redmine install, you can install these program as you will need them (some headers are required for the gem compilation below):

apt-get install git subversion imagemagick libmagickwand-dev mysql-server libmysqlclient-dev

I recommend following the official guide for the installation. The point is to give right on the Redmine2.5 folder to the user that is used by passenger to run your app. You can check which user run your app using ps aux | grep -i passenger  which should be the user who installed RVM, then apply the proper rights:

mkdir -p tmp tmp/pdf public/plugin_assets
sudo chown -R <regular_user>: files log tmp public/plugin_assets
sudo chmod -R 755 files log tmp public/plugin_assets

And then be sure to have select the right version of ruby and the right gemset, and to be in the Redmine2.5 folder before running these commands:

gem install bundler
bundle install --without development test
bundle exec rake generate_secret_token
RAILS_ENV=production bundle exec rake db:migrate
RAILS_ENV=production bundle exec rake redmine:load_default_data

 

first let’s install a version of Nginx compiled with passenger using the official guide of Passenger project.

Passenger help Nginx to handle ruby code. We need to tell Nginx which version of Ruby to use when running the app.

Run this comand /usr/bin/passenger-config –root  and copy its result in the file /etc/nginx/nginx.conf  It should look something like passenger_root /usr/lib/ruby/vendor_ruby/phusion_passenger/locations.ini;  . Then from your gemset dir run: passenger-config –ruby-command  that will tell you where is the wrapper for the correct version of ruby that you want Nginx+Pasenger to use! According to this, edit you nginx vhost, and add the line:

server {
   listen 80;
   root /var/wwww/redmine2.5/web/public;
   passenger_enabled on;
   passenger_ruby /home/user/.rvm/wrappers/ruby-2.0.0-p247@your_gemset/ruby;
}

So you can run a different version of ruby and a different set gem for every app using a different passenger_ruby line in your nginx vhost. That’s the trick!

BONUS PROXY

If when running gem or bundler the gem server is unreachable because you are behing a proxy (or in China 🙂 then you should tell your bash to go through a proxy using this command:

export http_proxy=http://user:password@host:port

BONUS LDAP Active Directory

Maybe you want to authenticate your redmine users against your company AD LDAP (windows server…bouh). Then I advise you to first discover the LDAP server architecture using a LDAP browser from Linux or Windows. And maybe setup a user than has read access on the LDAP schema, as Redmine ned one. Here is the official documentation to setup LDAP authentication.

And here is my config :

Name : Ldap
Host : ldap server IP
Port : 389
Account : redmine@AD.LOCAL
Password : redmine user password (redmine user should have read access on the ldap)
Base DN : OU=Users_Custom,DC=CTXSUZ,DC=LOCAL
LDAP filter : Empty
Timeout (in seconds) : 5
On-the-fly user creation : checked
 
Attributes
 
Login attribute : sAMAccountName
Firstname attribute : givenName
Lastname attribute : sN
Email attribute : mail

Enjoy 😉