jose.costa

Dumb script to reassemble HTML + SSI pages

Posted by jose.costa
on January 31, 2009
While working on our [website(WyeWorks website)](http://www.wyeworks.com), we wanted to serve some dynamic content but it just didn't justify having some "big" dynamic technology behind to achieve this. Also as programmers, we like our code (even our static website content) to be nicely separated so we can keep things in order, better maintenance, blah blah blah. Seriously, we really like those things :) So that's when we came across the [Apache Server Side Includes (Apache SSI)](http://httpd.apache.org/docs/1.3/howto/ssi.html) (a much more interesting topic than the addressed here). So, then we divided our static pages to separate the header, menu, content, footer, and we placed a bunch of page includes. **One drawback:** checking the content and styles as a whole is now a little bit difficult if you don't have your Apache SSI capable on handy to reassemble all back together. So this tiny dumb script library (plus some bunch of code not shown here) did that for us.
require 'fileutils'

module SSI

  def self.generate(source, dest)
    file = File.new(source)
    lines = file.readlines
    file.close
    lines.each do |line|
      if line =~ //
        path_to_included_file = $2
        unless path_to_included_file[0..0] == "/"
          path_to_included_file = File.join(File.dirname(source),path_to_included_file)
        end
        partial = File.new(path_to_included_file)
        lines_to_include = partial.readlines
        partial.close
        line.gsub!(//, lines_to_include.join(""))
      end
    end
    if File.directory?(dest)
      dest = File.join(dest, File.basename(source))
    end
    file = File.new(dest, "w")
    lines.each do |line|
      file.write(line)
    end
    file.close
  end
    
end
It behaves pretty much like any standard copy command, adding the replacement of the included files for their real content. Note there is no error checking whatsoever.
SSI.generate(source_file, dest_file_or_directory)
Hopefully it will save a minute or two to someone somewhere.
| |
sebastian.martinez

Setting up Passenger in Linux

Posted by sebastian.martinez
on January 28, 2009
Clearly one of the problems with Rails as a major platform right now is it’s hosting situation. Currently a good solution is to proxy HTTP requests from Apache or Nginx to a cluster of mongrels, which is tricky to set up and somewhat tedious. I wanted to easily have our rails applications deployed (in our development environment), and have them running without having to manually start each server. Messing around I found [Passenger(Passenger)](http://www.modrails.com/), a module for Apache that hosts Rails applications. Note this was tested in Ubuntu, but it's very similar for other Linux distributions. To install the module simply run:
$ sudo gem install passenger
Then, you need to do
$ passenger-install-apache2-module
and just follow the instructions. The installer is very easy to follow, and in my case it detected some software not installed, so i had to run
$ sudo apt-get install apache2-prefork-dev
Once the installation was successfully completed, it asked to add the following lines to the apache configuration file, /etc/apache2/apache2.conf
"LoadModule passenger_module /usr/lib/ruby/gems/1.8/gems/passenger-2.0.2/ext/apache2/mod_passenger.so
PassengerRoot /usr/lib/ruby/gems/1.8/gems/passenger-2.0.2
PassengerRuby /usr/bin/ruby1.8"
At this point, Passenger is properly configured, and the only thing left is to configure our applications to work with Apache. So, let's do it: make your 'httpd.conf' file to look like this:
NameVirtualHost localhost:80

      ServerName app1.local
      DocumentRoot /app1_path/public



      ServerName app2.local
      DocumentRoot /app2_path/public

...and so on for more applications. Also, modify the '/etc/hosts' file to have your applications go to localhost. It should look similar to this:
127.0.0.1       localhost app1.local app2.local
There are some considerations to make here. First and most important, the rails environment. Passenger sets it to production as default, but if you want it to be some other, just need to add the following line into the configuration file:
RailsEnv environment
Now if you just type into your browser app1.local you should see your application alive! Without the images and style? Yes, simply to solve. Delete the following file and restart apache.
$ rm -f /your_app_path/public/.htaccess
Bingo! That was all the strictly necessary to have your application deployed with Passenger...but we have some tricks to share ;) Suppose you want your Ruby on Rails application to be accessible from the URL http://localhost/app. To do this, make a symlink from your Ruby on Rails application's public folder to a directory in the document root. For example: Type
$ ln -s /your_app_path/public /var/www/app1
in a terminal. Next, add a RailsBaseURI option to the virtual host configuration:


    ServerName app1.local
    DocumentRoot /app1_path/public
    RailsBaseURI /app1                # This line has been added.

Now, according to Passenger's documentation, this should be it, but I've found some problems with Rails 2.2.2. Going to http://localhost/app1 now may give you a HTTP 404 error. To solve this, you need to add this line to environment.rb of your application:
config.action_controller.relative_url_root = "/app1"
Another sweet thing is restarting a Rails app hosted by Passenger - simply touch a file called tmp/restart.txt within the Rails application root:
$ touch tmp/restart.txt
And that's it. Enjoy this beauty. Note that this configuration is intended to be for development environment only. Passenger provides other parameters for production, which we will tackle in the near future, and public our opinions. So, don't forget to keep visiting us. Seeya in the next post!
| |