Lampros - Weird Bricks

nginx with passenger support on FreeBSD 8.2

24 November, 2011 | Passenger

In a previous post, I showed how to install RVM and Ruby Enterprise Edition on FreeBSD 8.2. 


But this isn't enough, we want to be able to deploy rails applications. The best way to do that is by using nginx and passenger.

nginx is a light web-server but very easy on resources and although it's not as popular as say, Apache, it works really well with Passenger.

Passenger is the necessary module that will allow us to deploy application under nginx. Once that's installed setting up application is a breeze as I'm going to demonstrate.

Prerequisites:



Start by getting the necessary gems. Gems are downloaded by RubyGem, the de facto Ruby Package Manager.

gem install passenger --no-ri --no-rdoc


The --no-ri and --no-rdoc, simply tell it to now download the documentation. We don't need it.

You should see something like this:

Fetching: fastthread-1.0.7.gem (100%)
Building native extensions.  This could take a while...
Fetching: daemon_controller-0.2.6.gem (100%)
Fetching: rack-1.3.5.gem (100%)
Fetching: passenger-3.0.9.gem (100%)
Successfully installed fastthread-1.0.7
Successfully installed daemon_controller-0.2.6
Successfully installed rack-1.3.5
Successfully installed passenger-3.0.9
4 gems installed


This should only take a few seconds to complete.

Next step, fire up the installer:

passenger-install-nginx-module


You should see the installer:

Welcome to the Phusion Passenger Nginx module installer, v3.0.9.

This installer will guide you through the entire installation process. It
shouldn't take more than 5 minutes in total.

Here's what you can expect from the installation process:

 1. This installer will compile and install Nginx with Passenger support.
 2. You'll learn how to configure Passenger in Nginx.
 3. You'll learn how to deploy a Ruby on Rails application.

Don't worry if anything goes wrong. This installer will advise you on how to
solve any problems.

Press Enter to continue, or Ctrl-C to abort.

As the instructions say, simply press Enter.

Checking for required software...

 * GNU C++ compiler... found at /usr/bin/g++
 * The 'make' tool... found at /usr/bin/make
 * A download tool like 'wget' or 'curl'... found at /usr/local/bin/curl
 * Ruby development headers... found
 * OpenSSL support for Ruby... found
 * RubyGems... found
 * Rake... found at /usr/local/rvm/wrappers/ree-1.8.7-head/rake
 * rack... found
 * Curl development headers with SSL support... found
 * OpenSSL development headers... found
 * Zlib development headers... found

--------------------------------------------

Automatically download and install Nginx?

Nginx doesn't support loadable modules such as some other web servers do,
so in order to install Nginx with Passenger support, it must be recompiled.

Do you want this installer to download, compile and install Nginx for you?

 1. Yes: download, compile and install Nginx for me. (recommended)
    The easiest way to get started. A stock Nginx 1.0.6 with Passenger
    support, but with no other additional third party modules, will be
    installed for you to a directory of your choice.

 2. No: I want to customize my Nginx installation. (for advanced users)
    Choose this if you want to compile Nginx with more third party modules
    besides Passenger, or if you need to pass additional options to Nginx's
    'configure' script. This installer will  1) ask you for the location of
    the Nginx source code,  2) run the 'configure' script according to your
    instructions, and  3) run 'make install'.

Whichever you choose, if you already have an existing Nginx configuration file,
then it will be preserved.

Enter your choice (1 or 2) or press Ctrl-C to abort:

Take option 1. The source code will be downloaded and compiled. But before that you'll be asked where to install nginx.

Where do you want to install Nginx to?

Please specify a prefix directory [/opt/nginx]:

Just press enter to take /opt/nginx. That'll work great. Then the system will compile nginx and passenger. When done you should see this:

Nginx with Passenger support was successfully installed.

The Nginx configuration file (/opt/nginx/conf/nginx.conf)
must contain the correct configuration options in order for Phusion Passenger
to function correctly.

This installer has already modified the configuration file for you! The
following configuration snippet was inserted:

  http {
      ...
      passenger_root /usr/local/rvm/gems/ree-1.8.7-head/gems/passenger-3.0.9;
      passenger_ruby /usr/local/rvm/wrappers/ree-1.8.7-head/ruby;
      ...
  }

After you start Nginx, you are ready to deploy any number of Ruby on Rails
applications on Nginx.

Press ENTER to continue.

That's great! Just hit enter to continue.
Finally you'll see an example of how to deploy an application on Ruby on Rails:

--------------------------------------------

Deploying a Ruby on Rails application: an example

Suppose you have a Ruby on Rails application in /somewhere. Add a server block
to your Nginx configuration file, set its root to /somewhere/public, and set
'passenger_enabled on', like this:

   server {
      listen 80;
      server_name www.yourhost.com;
      root /somewhere/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;
   }

And that's it! You may also want to check the Users Guide for security and
optimization tips and other useful information:

  /usr/local/rvm/gems/ree-1.8.7-head/gems/passenger-3.0.9/doc/Users guide Nginx.html

Enjoy Phusion Passenger, a product of Phusion (www.phusion.nl) :-)
http://www.modrails.com/

Phusion Passenger is a trademark of Hongli Lai & Ninh Bui.

With Passenger installed we're going to go ahead and get ready to deploy a sample application. In this case we'll set up the Fat Free CRM.

First install SQLite3:

pkg_add -r sqlite3

You should see this:

Fetching ftp://ftp.freebsd.org/pub/FreeBSD/ports/i386/packages-8.2-release/Latest/sqlite3.tbz... Done.

Use git to download the latest version of fat_free_crm in /var/fatfreecrm (the directory will be created automatically):

git clone https://github.com/fatfreecrm/fat_free_crm.git /var/fatfreecrm

Enter the application directory:

cd /var/fatfreecrm

Install the bundler gem:

gem install bundler

Copy the default SQLite3 configuration file that comes with Fat Free CRM:

cp config/database.sqlite.yml config/database.yml

edit Gemfile, make sure pg is commented out and uncomment sqlite3

ee Gemfile

When you're done the first lines of the Gemfile should look like this:

source 'http://rubygems.org'

gem 'rails', '3.0.7'
gem 'rake',  '0.8.7'

# Uncomment the database that you have configured in config/database.yml
# ----------------------------------------------------------------------
# gem "mysql2", "0.2.7"
gem "sqlite3"
# gem "pg", ">= 0.9.0"

gem 'authlogic',           '~> 3.0.3'

Before we go ahead and use Bundler to install all Gem dependencies, let's take care of SQLite3 manually, else we'll end up with lots of weird errors. This is because SQLite3 is installed in a non-standard path on FreeBSD - use the following:

gem install sqlite3 -- --with-sqlite3-dir=/usr/local

If successful you should see this:

Fetching: sqlite3-1.3.4.gem (100%)
Building native extensions.  This could take a while...
Successfully installed sqlite3-1.3.4
1 gem installed
Installing ri documentation for sqlite3-1.3.4...

No definition for libversion

Enclosing class/module 'mSqlite3' for class Statement not known
Installing RDoc documentation for sqlite3-1.3.4...

No definition for libversion

Enclosing class/module 'mSqlite3' for class Statement not known

With that done, let's go ahead and use bundler (takes about a minute):

bundle install

You should see something like this if everything went well:

Installing sass (3.1.7)
Installing simple_form (1.5.2)
Using sqlite3 (1.3.4)
Installing will_paginate (3.0.0)
Your bundle is complete! Use `bundle show [gemname]` to see where a bundled gem is installed.

Now let's prepare the database for Fat Free CRM:

RAILS_ENV=production bundle exec rake db:create

This doesn't have much output, all I got was:

(in /var/fatfreecrm)

That's OK - continue with setting up a default user:

RAILS_ENV=production bundle exec rake crm:setup USERNAME=admin PASSWORD=password EMAIL=admin@example.com

You should see something like this when the user has been added:

===== Settings have been loaded.
Admin user has been created.

Now populate the database with some sample values:

RAILS_ENV=production bundle exec rake crm:demo:load

You should see:

-- change_column_default("accounts", "access", "Public")
   -> 0.0658s
===== Settings have been loaded.
Generating user activities...
...........................................................................

We're done setting up the application! Now let's go get nginx ready - first go to the nginx directory:

cd /opt/nginx/conf

Now, edit the main config file (that is nginx.conf) - on the top uncomment the nobody user:

user  nobody;

Then scroll down to the server section - delete all of it.

ee nginx.conf

Go to the end of the file and add this line just before the last }

include /opt/nginx/conf.d/*.conf;

In case of doubt the last lines of the file should look like this:

    #        index  index.html index.htm;
    #    }
    #}
include /opt/nginx/conf.d/*.conf;
}

Save the file and exit. Now create a config directory for our applications:

mkdir /opt/nginx/conf.d

And create a configuration file for Fat Free CRM:

ee /opt/nginx/conf.d/fatfreecrm.conf

Add the following lines:

server {
      listen 80;
      server_name www.yourhost.com;  # <-- this doesn't matter 
      root /var/fatfreecrm/public;   # <--- be sure to point to 'public'!
      passenger_enabled on;
}

Save and exit. Now change the permissions for the /var/fatfreecrm directory:

chown -R  nobody:nobody /var/fatfreecrm/

Finally run nginx:

/opt/nginx/sbin/nginx -c /opt/nginx/conf/nginx.conf

Fire up your browser and point it to the IP address of your FreeBSD server:
Login with username 'ben' and password 'ben' !


If you like you can check the nginx logs for activity:

tail -f /opt/nginx/logs/access.log


Or the error logs:

tail -f /opt/nginx/logs/error.log


Or maybe you want to check the production logs of Fat Free CRM:

tail -f /var/fatfreecrm/log/production.log