apache snippets

Generate a 56-bit DES encrypted (htpasswd) password with Ruby

Tagged ruby, irb, htpasswd, nginx, apache  Languages css

Run the following in an irb console to generate a 56-bit DES encrypted password:


The password can be used in an Apache or Nginx htpasswd file to enable basic authentication.

The generated password can also be used in other Unix password files.

Rails+Mongrel+Apache 2 on Mac OSX Leopard

Tagged apache, mongrel, osx, rails  Languages apacheconf

I use this configuration on my development machine when I need mod_rewrite; it's not meant for production:

<VirtualHost *:80>
ServerName dev.xxx.com

# Enable URL rewriting
RewriteEngine On

# Rewrite index to check for static pages
RewriteRule ^/$ /index.html [QSA]

# Rewrite to check for Rails cached page
RewriteRule ^([^.]+)$ $1.html [QSA]

# Redirect all non-static requests to cluster
RewriteRule ^/(.*)$ balancer://mongrel_cluster%{REQUEST_URI} [P,QSA,L]

DocumentRoot  "/Users/christian/Documents/Projects/xxx/public"
<Directory "/Users/christian/Documents/Projects/xxx/public">
     Options Indexes FollowSymLinks

     AllowOverride None
     Order allow,deny
     Allow from all


<Proxy balancer://mongrel_cluster>

Vlad deployment recipe for Phusion Passenger

Tagged vlad, phusion, passenger, apache, deployment  Languages ruby
# General configuration
#set :ssh_flags,             '-p 110000'
set :application,           'app.xxx'
set :domain,                'x.x.x.x'
set :deploy_to,             '/var/www/app.xxx'
set :revision,              'master'
set :repository,            '/var/lib/git/repositories/app.xxx/'

namespace :vlad do
  set :app_command, "/etc/init.d/apache2"
  desc 'Restart Passenger'
  remote_task :start_app, :roles => :app do
    run "touch #{current_release}/tmp/restart.txt"
  desc 'Restarts the apache servers'
  remote_task :start_web, :roles => :app do
    run "sudo #{app_command} restart"

How to hide X-Powered-By and Server headers

Tagged servertokens, server, x-powered-by, mod_rails, passenger, apache, apache2, headers  Languages apacheconf

First enable the mod_headers module:

sudo a2enmod headers

Then add this to your apache2.conf:

# Hide X-Powered-By and Server headers
Header always unset "X-Powered-By"
ServerTokens Prod
ServerSignature Off

Now restart Apache:

/etc/init.d/apache2 force-reload

This is security through obscurity at it's finest...

How to parse Apache logs with Ruby

Tagged apache, statistics, logs, ruby, combined, format  Languages ruby

Only supports the combined format at the moment...

class ApacheLog
    :combined => %r{^(\S+) - - \[(\S+ \+\d{4})\] "(\S+ \S+ [^"]+)" (\d{3}) (\d+|-) "(.*?)" "([^"]+)"$}
  class << self
    def each_line(log_file, log_format = FORMATS[:combined])

      f = File.open(log_file, "r")

      f.each_line do|line|
        data = line.scan(log_format).flatten

        if data.empty?
          p "Line didn't match pattern: #{line}"


        yield data

log_file   = ARGV[0]

ApacheLog.each_line(log_file) do |data|
  host, date, url_with_method, status, size, referrer, agent = data

Paperclip plugin + Phusion Passenger + path problem

Tagged paperclip, apache, phusion, passenger  Languages ruby

In config/initializers/paperclip.rb put:

if RAILS_ENV == "development"
  Paperclip.options[:command_path] = '/opt/local/bin/'
  Paperclip.options[:command_path] = '/usr/bin/'

Note image_magick_path is deprecated alias of command_path.

How to set the Expires header with Apache 2 and mod_expires

Tagged apache, expires, mod_expires, header, caching  Languages apacheconf

First you need to enable the mod_expires module:

a2enmod expires

Next add this to your configuration:

ExpiresActive On
# Set Expires header to current time by default
ExpiresDefault A0

<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
  ExpiresDefault "access plus 30 days"

<FilesMatch "\.(jpg|jpeg|png|gif|swf|bmp|)$">
  ExpiresDefault "access plus 7 days"

<FilesMatch "\.(txt|xml|js|css)$">
  ExpiresDefault "access plus 1 day"

Now restart Apache:

$ sudo /etc/init.d/apache2 force-reload

Check that the proper headers are set with Firebug, Yahoo YSlow or Google Page speed.

How to read and grep compressed & rotated log files

Tagged zgrep, zless, logs, apache, grep  Languages bash

Your typical server log directory looks something like this:


Now what if you want to extract data from day x to day y? You could use gzip and grep to uncompress the files, but there's a better way: the z commands.

All you have to do is:

zgrep "2010" /var/log/apache2/access.log*

More info on the subject can be found here: http://www.thegeekstuff.com/2009/05/zcat-zless-zgrep-zdiff-zcmp-zmore-gzip-file-operations-on-the-compressed-files/

How to log request processing times with Apache

Tagged mod_headers, apache, performance, x-request-received, x-request-processing-time  Languages apacheconf

To log the time it takes for Apache to process a request add this to your apache configuration file:

LogFormat "%h %D %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined

%D means you'll see the time it took Apache to process the request in the access log. The time is measured in microseconds.

To make it even easier to debug page load times, add this to your configuration file:

Header set X-Request-Received: %t
Header set X-Request-Processing-Time: %D

Remember to enable mod_headers first by executing a2enmod headers.

Now you should see these headers in the response:

X-Request-Received  t=1286995673038485
X-Request-Processing-Time   D=251