Skip to content

Instantly share code, notes, and snippets.

@brookr
Created April 16, 2012 04:36
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save brookr/2396356 to your computer and use it in GitHub Desktop.
Save brookr/2396356 to your computer and use it in GitHub Desktop.
Rackup file I use for running WordPress on Pow
# config.ru for Pow + Wordpress, based on http://stuff-things.net/2011/05/16/legacy-development-with-pow/
# added hackery to work around wordpress issues - Patrick Anderson (patrick@trinity-ai.com)
# clearly this could be cleaner, but it does work
require 'rack'
require 'rack-legacy'
require 'rack-rewrite'
# patch Php from rack-legacy to substitute the original request so
# WP's redirect_canonical doesn't do an infinite redirect of /
module Rack
  module Legacy
    class Php
      def run(env, path)
        
        config = {'cgi.force_redirect' => 0}
        config.merge! HtAccess.merge_all(path, public_dir) if @htaccess_enabled
        config = config.collect {|(key, value)| "#{key}=#{value}"}
        config.collect! {|kv| ['-d', kv]}
        
        script, info = *path_parts(path)
        env['SCRIPT_FILENAME'] = script
        env['SCRIPT_NAME'] = strip_public script
        env['PATH_INFO'] = info
        env['REQUEST_URI'] = strip_public path
        env['REQUEST_URI'] = env['POW_ORIGINAL_REQUEST'] unless env['POW_ORIGINAL_REQUEST'].nil?
        
        super env, @php_exe, *config.flatten
      end
    end
  end
end
INDEXES = ['index.html','index.php', 'index.cgi']
use Rack::Rewrite do
  # redirect /foo to /foo/ - emulate the canonical WP .htaccess rewrites
  r301 %r{(^.*/[\w\-_]+$)}, '$1/'
  rewrite %r{(.*/$)}, lambda {|match, rack_env|
    rack_env['POW_ORIGINAL_REQUEST'] = rack_env['PATH_INFO']
    if !File.exists?(File.join(Dir.getwd, rack_env['PATH_INFO']))
      return '/index.php'
    end
    INDEXES.each do |index|
      if File.exists?(File.join(Dir.getwd, rack_env['PATH_INFO'], index))
        return File.join(rack_env['PATH_INFO'], index)
      end
    end
    rack_env['PATH_INFO']
  }
  # also rewrite /?p=1 type requests
  rewrite %r{(.*/\?.*$)}, lambda {|match, rack_env|
    rack_env['POW_ORIGINAL_REQUEST'] = rack_env['PATH_INFO']
    query = match[1].split('?').last
    if !File.exists?(File.join(Dir.getwd, rack_env['PATH_INFO']))
      return '/index.php?' + query
    end
    INDEXES.each do |index|
      if File.exists?(File.join(Dir.getwd, rack_env['PATH_INFO'], index))
        return File.join(rack_env['PATH_INFO'], index) + '?' + query
      end
    end
    rack_env['PATH_INFO'] + '?' + query
  }
end
use Rack::Legacy::Php, Dir.getwd
use Rack::Legacy::Cgi, Dir.getwd
run Rack::File.new Dir.getwd
@jfrux
Copy link

jfrux commented Jan 9, 2014

anytips for the Errno::ECONNREFUSED: Connection refused - connect(2) error I'm getting?

@lucaspiller
Copy link

@joshuairl I just had the same, it was because the wrong PHP version was being used. Check by running rackup in the Wordpress directory. It should boot up, if you see PHP help you have the wrong version.

Install php54 as follows.

  • Figure out what PHP you have:
$ which php
/usr/local/bin/php # old Brew version - skip the next step if it is /usr/bin/php (system default)
  • Uninstall old version:
$ ls -la /usr/local/bin/php                                                                                                                            127 ↵
lrwxr-xr-x  1 luca  admin  30 28 Sep 15:55 /usr/local/bin/php -> ../Cellar/php53/5.3.27/bin/php
$ brew uninstall php53
  • Install php54:
$ brew tap homebrew/dupes
$ brew tap josegonzalez/homebrew-php
$ brew update
$ brew install josegonzalez/php/php54 --with-mysql --with-cgi

@davidgeere
Copy link

I get the same error however my PHP is working perfectly. When I run rackup then application starts and I can view it on localhost:{assigned-port} however it just doesn't go via Pow. Any insights you might have on this would be really appreciated!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment