Skip to content

Ted Kulp

Polyglot Developer. Teacher. Curmudgeon.

  • Home
  • About
  • Blog
  • Home
  • 2011
  • August
  • Using Pow for PHP (or anything on Apache)

Using Pow for PHP (or anything on Apache)

Posted on 2011/08/192014/07/10 By Ted Kulp No Comments on Using Pow for PHP (or anything on Apache)
Programming

When I reinstalled my system w/ Lion a few weeks back, I decided to start using Pow! as my “web server” of choice. I’ve been spending a lot more time in Rails over the past few months because of my day job and wasn’t really relying on my Apache/PHP installation as much. I
delegated Apache off to port 8080 while Pow took port 80 over because it was just too convenient.

The way I’ve always done my PHP development was just dump my projects in subdirectories of the Apache web root (/Library/WebServer/Documents) and leave it at that. Most of my URLs would look something like: http://localhost:8080/some-project.

Today I was luck enough to come across this post in Assaf Arkin’s Lab Notes site: Using Pow with your Node.js project. Long story short is that it uses the fact that config.ru is actually a Ruby script and uses it to make a little proxy to connect to whatever node.js server/daemon that you happen to be running. It still requires you to start it up, but it’s pretty convenient to have it with the same URL scheme and port 80-ness of the rest of your projects.

I immediately decided to see if I could modify it to:

  • Work on my port 8080 Apache install and
  • Get it to automatically work with my subdirectory scheme.

First, I dropped an .rvmrc into my PHP project’s directory so I had a php specific gemset.

rvm_install_on_use_flag=1
rvm --create use [email protected]

Then, I installed rack into that gemset.

gem install rack

I copied in my modified config.ru (which, incidentally has the path some-project hardcoded at the moment — I’ll clean it up later.)

require "net/http"

class ProxyApp
  def call(env)
    begin
      request = Rack::Request.new(env)
      headers = {}
      env.each do |key, value|
        if key =~ /^http_(.*)/i
          headers[$1] = value
        end
      end
      http = Net::HTTP.new("localhost", 8080)
      http.start do |http|
        response = http.send_request(request.request_method, '/some-project' + request.fullpath, request.body.read, headers)
        [response.code, response.to_hash, [response.body]]
      end
    rescue Errno::ECONNREFUSED
      [500, {}, ["Server is down, try $ sudo apachectl start"]]
    end
  end
end
run ProxyApp.new

And lastly, symlinked my directory into my ~/.pow dir. Since I use powify, I just did a powify create to basically do the same thing.

Pointed my browser to http://some-project.dev and voila! I don’t see any visible slowdowns related to the proxy, and it works really nicely. It’s a keeper!

Tags: apache mac nodejs php pow

Post navigation

Next Post: Padrino and the Asset Pipeline ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • Promisify Javascript Events with Bluebird
  • Full Screen Google Analytics via Bookmarklet
  • A few reasons to use Underscore.js (or Lo-Dash)
  • Office Setup
  • Turn Twitter Favorites into an RSS feed

Recent Comments

  • Tom on Full Screen Google Analytics via Bookmarklet
  • Russell on Full Screen Google Analytics via Bookmarklet
  • Max on Full Screen Google Analytics via Bookmarklet
  • lars on Full Screen Google Analytics via Bookmarklet
  • Dan O'Keefe on A few reasons to use Underscore.js (or Lo-Dash)

Archives

  • August 2014
  • July 2014
  • October 2013
  • February 2013
  • May 2012
  • November 2011
  • August 2011

Categories

  • Productivity
  • Programming

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2021 Ted Kulp.

Theme: Oceanly by ScriptsTown