Writing a Passenger Module for Puppet – Vagrant

Following the steps described in the “Deploying Rails Applications” book and using the currently latest puppet installation described here, the actual puppet files had to be modified slightly in order to make it work as expected.

The main issues requiring the changes were as follow:

  • System wide rvm installation – different paths, and the use of the rvmsudo command instead of sudo
  • Use of latest puppet gem (3.0.1) – ruby 1.9.3 is supported
  • Use of Debian/Ubuntu system – different way of configuring passenger with apache2

So the full file for the passenger module (puppet/modules/passenger/manifests/init.pp) is:

class passenger {

  # required for passenger gem
  package {
    "apache2-prefork-dev":
      ensure => installed
  }

  # required for passenger-apache-module
  package {
    "libcurl4-openssl-dev":
      ensure => installed
  }

  exec  {
    "gem install passenger -v=3.0.18":
      user    => vagrant,
      group   => vagrant,
      alias   => "install_passenger",
      require => Package["apache2"],
      unless  => "ls /usr/local/rvm/gems/ruby-1.9.3-p327@rails3_2_9/bin/passenger",
      before  => Exec["passenger_apache_module"]
  }

  exec  {
    "passenger-install-apache2-module --auto":
      user    => root,
      group   => root,
      alias   => "passenger_apache_module",
      require => Package["apache2-prefork-dev","libcurl4-openssl-dev"],
      unless  => "ls /usr/local/rvm/gems/ruby-1.9.3-p327@rails3_2_9/gems/passenger-3.0.18/ext/apache2/mod_passenger.so",
      before  => File["passenger_conf"]
  }

  file  {
    "/etc/apache2/mods-available/passenger.conf":
      mode    => 644,
      owner   => root,
      group   => root,
      alias   => "passenger_conf",
      source  => "/home/vagrant/puppet/modules/passenger/files/passenger.conf",
      before  => File["passenger_load"]
  }

  file {
    "/etc/apache2/mods-available/passenger.load":
      mode    => 644,
      owner   => root,
      group   => root,
      alias   => "passenger_load",
      notify  => Service["apache2"],
      source  => "/home/vagrant/puppet/modules/passenger/files/passenger.load",
      before  => Exec["enable_passenger"]
  }

  exec {
    "a2enmod passenger":
      user    => root,
      alias   => "enable_passenger"
  }
}

Going through the file one of the first things to notice is the declaration about the “apache2-prefork-dev” package installation

 package {
    "apache2-prefork-dev":
      ensure => "installed"
  }

The reason for this package, is that when trying to compile the passenger apache module (passenger-install-apache2-module), the script will complain that the following packages are missing:

apache2-prefork-dev
libapr1-dev
libaprutil1-dev

By installing the first of the packages (apache2-prefork-dev) the other two are installed automatically as dependencies.

NOTE In trying to install it in an Ubuntu AMI image in AWS EC2 there is also an additional package requirement because the installer complains about:

To install Curl development headers with SSL support:  Please run apt-get install libcurl4-openssl-dev or libcurl4-gnutls-dev, whichever you prefer.

So there is the additional package added as:

# required for the passenger_apache_module
package {
  "libcurl4-openssl-dev":
    ensure  => installed
}

Further down there are two file resources named “passenger_conf” and “passenger_load”.
Looking at the structure in debian for the apache2 configuration it seems that apache2 in debian needs two different files for each module that can be loaded one with named as module.conf and another as module.load to be placed into /etc/apache2/mods-available.

So we can create these two files and placed them in the files folder, as:

puppet/modules/passenger/files/passenger.conf


  PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p327@rails3_2_9/gems/passenger-3.0.18
  PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p327@rails3_2_9/ruby
  PassengerUseGlobalQueue on
  PassengerMaxPoolSize  5
  PassengerPoolIdleTime 900
  PassengerMaxRequests 10000

and, puppet/modules/passenger/files/passenger.load

LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p327/gems/passenger-3.0.11/ext/apache2/mod_passenger.so

Which you can actually see if you compile manually the passenger apache2 module as they are described in the output on successful compilation:

The Apache 2 module was successfully installed.

Please edit your Apache configuration file, and add these lines:                                                                                                                                                                                

   LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p327@rails3_2_9/gems/passenger-3.0.18/ext/apache2/mod_passenger.so
   PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p327@rails3_2_9/gems/passenger-3.0.18
   PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p327@rails3_2_9/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails                                                                                                                                                                   
applications on Apache, without any further Ruby on Rails-specific                                                                                                                                                                              
configuration!                                                                                                                                                                                                                                  

Press ENTER to continue.

Finally the last section of the init.pp file enables the passenger module with the a2enmod apache command:

  exec {
    "a2enmod passenger":
      user    => root,
      alias   => "enable_passenger"
  }

A couple of useful apache2 commands when testing are:

  • To test what modules are availble for loading:
    a2enmod

    If run after creating the two passenger files passenger.conf and passenger.load, then the passenger module should appear in the list

  • To see the currently enabled modules:
    apache2ctl -M

    Which again if it is run after the passenger modules has been enabled with a2enmod passenger then it should show an entry with

    passenger_module (shared)

Could not find activesupport-3.0.12 in any of the sources (Bundler::GemNotFound)

Problem

You are trying to update your rails app deployed on a server, for example from 3.0.11 to 3.0.12, and at the same time trying to update the Ruby version from 1.9.2 to 1.9.3 but you are getting the following error from passenger:

    Could not find activesupport-3.0.12 in any of the sources (Bundler::GemNotFound)

Solution

You would need to reinstall and compile the passenger module with the newest Ruby 1.9.3 installation.

gem install passenger

passenger-install-apache2-module

and then modify the apache configuration file as in:

The Apache 2 module was successfully installed.                   

Please edit your Apache configuration file, and add these lines:

   LoadModule passenger_module /usr/local/rvm/gems/ruby-1.9.3-p125/gems/passenger-3.0.11/ext/apache2/mod_passenger.so
   PassengerRoot /usr/local/rvm/gems/ruby-1.9.3-p125/gems/passenger-3.0.11
   PassengerRuby /usr/local/rvm/wrappers/ruby-1.9.3-p125/ruby

After you restart Apache, you are ready to deploy any number of Ruby on Rails
applications on Apache, without any further Ruby on Rails-specific
configuration!

and then reload your apache configuration

sudo /etc/init.di/apache2 reload

passenger ArgumentError (invalid byte sequence in US-ASCII)

Problem
After upgrading your server to use rvm and Ruby 1.9 you get the following error when you are trying to access certain parts of your redmine installation:

passenger ArgumentError (invalid byte sequence in US-ASCII)

Solution
If you follow the instruction posted here, you can add the following to your redmine’s “RAILS_ROOT/config/initializers/string_encodings.rb”:

Encoding.default_external = 'UTF-8'

and restart your application

Passenger (mod_rails) gem installation – Apache2 – Debian – rvm

Problem
You would like to (re)-install passenger after a system wide rvm installation, but you only have the mod_passenger.c file and not the .so after the gem installation.

Solution
Make sure that after using gem install passenger you also use the command to install the apache2 passenger module:

passenger-install-apache2-module

you should then be able to see the .so file and add it to your /etc/apache2/mods-available/passenger.load

Rails Passenger Staging environment problem in Dreamhost

Problem
You want to deploy your rails application using Passenger in Dreamhost, but on a staging environment. It doesn’t work just changing your Capistrano recipe or your environment files, as the default environment for passenger is ‘production’.

Solution
Looking at the Passenger documentation here, there are four different ways of achieving that by changing the RailsEnv option. If you don’t want to change the Apache configuration files, and as long as the option AllowOverride is on (which should be on Dreamhost), then you would need to create an .htaccess file in your application’s public folder with the following:

RailsEnv staging

Phusion Passenger installation in Mandriva 2009

Problem
You have recently installed Mandriva 2009 and you want to install phusion passenger.

Solution

  1. First make sure that apache is installed. If it is not installed (default), do:
    urpmi apache

    On the selection question about the dependencies select option 1 (apache-prefork)

  2. Use the details from the phusion passenger website. ie:
    Install passenger:

    gem install passenger
  3. Run the passenger installation:
    passenger-install-apache2-module
  4. If at the second stage of the installation the installer complains that GNU C++ compiler … not found, install the gnu c++ compiler, with:
    urpmi gcc_c++
  5. Run the passenger installation (step 3) again. If at this stage the installer complains that Apache 2 development headers … not found, use:
    urpmi apache-devel
  6. Run passenger installer (step 3), once more. Now everything should be ok and the apache 2 module will be installed when the installer finishes.
  7. Restart the Apache webserver, and you should be able to add your Rails application. Use the following to deploy your application in /somewhere/public:
    <VirtualHost *:80>
      ServerName www.yourhost.com
      DocumentRoot /somewhere/public
    </VirtualHost>