How to Deploy Ruby + Passenger + Nginx on CentOS Linux

Before we get started into the technical tutorial, let’s first review the technologies involved in this Ruby + Passenger + Nginx guide on CentOS Linux.

Ruby is an agile, dynamic open source language focused on easy and natural code writing and reading, while keeping a nice and elegant simple syntax.

Passenger is an app server that is used to server apps written in Ruby language. In order to get your ruby apps online, you must integrate Passenger into a web server, in our case our beloved Nginx.

This tutorial is based on CentOS 6.x 64 bits but it should also work on RHEL 6.x.

Requirements

Let’s install some dev tools we may need

yum install gcc g++ make automake autoconf curl curl-devel openssl-devel zlib-devel httpd-devel apr-devel apr-util pygpgme
yum groupinstall 'Development Tools'

Kernel & Selinux Requirements

If SELinux is enabled, then Passenger requires kernel >= 2.6.39. If your kernel is not recent enough, then you can disable Selinux:

nano -w /etc/selinux/config

Then set SELINUX=disabled and reboot your server.

Or you can update your kernel too, using dnf

dnf update kernel*

How can I install Ruby + Passenger + Nginx on CentOS ?

Installing Ruby, Passenger and integrating Passenger into Nginx is not hard at all, but you need to follow the steps carefully and analyze every error if you see something.

Installing Ruby

Let’s install RVM and Ruby 2.2.2, run the following commands:

curl -L https://get.rvm.io | bash -s stable
source /etc/profile.d/rvm.sh
rvm install 2.2.2
rvm use 2.2.2 --default

Check your Ruby installation

Now check your ruby version, it should be 2.2.2

[[email protected]:~]which ruby
/usr/local/rvm/rubies/ruby-2.2.2/bin/ruby
[[email protected]:~]ruby --version
ruby 2.2.2p95 (2015-04-13 revision 50295) [x86_64-linux]

Install Passenger

gem install passenger

if that doesn’t work or shows any error, try this:

/usr/local/rvm/rubies/ruby-2.2.2/bin/gem install passenger

Installing Nginx + Passenger Module

wget http://nginx.org/download/nginx-1.11.6.tar.gz
tar -xvpzf nginx-1.11.6.tar.gz
cd nginx-1.11.6

Let’s grab Nginx dir inside passenger and set the global variable, as we will need this to add passenger support into Nginx.

Check what is your Nginx source dir inside passenger installation using this command:

passenger-config about nginx-addon-dir

Then set it as you see below, in my case the source dir was /src/nginx_module.

PASSENGER_NGINX_DIR=`passenger-config --root`/src/nginx_module

Make sure you have correct permissions on /tmp directory, otherwise passenger won’t compile:

chmod 1777 /tmp -v

We will now configure Nginx and at the end of the configure line we will add this

--add-module=$PASSENGER_NGINX_DIR

The full command would look like this. Make sure you add your own required Nginx modules and configure your paths to match your own needs, this is just a generic example:

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --with-http_geoip_module --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --add-module=$PASSENGER_NGINX_DIR

Then run make and make install

make
make install

Configure Passenger Nginx module

nano -w /etc/nginx/conf.d/passenger.conf

Paste this inside:

passenger_root /usr/local/rvm/gems/ruby-2.2.2/gems/passenger-5.0.30;
passenger_ruby /usr/local/rvm/rubies/ruby-2.2.2/bin/ruby;
passenger_instance_registry_dir /var/run/passenger-instreg;

Enable Rails and Passenger inside you Nginx Virtual Host

Add this two lines inside your vhost configuration:

passenger_enabled on;
rails_env production;

It should look like this:

### yourdomain.com

server {
access_log off;
log_not_found off;
error_log  logs/yourdomain.com-error_log info;

        listen       80;
        server_name  yourdomain.com www.yourdomain.com;

	# activate passenger & rails support
	passenger_enabled on;
    	rails_env production;

Restart Nginx to apply changes:

service nginx restart

Conclusion

That’s all, at this point you should have Ruby, Passenger and Nginx fully working. Please let us know if you have any errors or issues following the steps provided in this tutorial.

About the Author: Martin Keler

Leave a Reply

Your email address will not be published. Required fields are marked *