Using Pow for PHP (or anything on Apache)
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.
``` bash
rvm_install_on_use_flag=1
rvm --create use 1.9.2@php
```
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.)
``` ruby
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!