BEEPS AND BLIPS FROM OUTERSPACE
HOW TO RUN PHP ON NGINX WITH SLIM FRAMEWORK ON WINDOWS

I found myself wanting to test some PHP on my local Windows machine and wanted a lightweight, fast solution besides Apache.  My motivation was getting a test environment with a RESTful serverside api for backbone.js testing.

The resulting toolchain is comprised of:

  1. Nginx (a tiny, fast HTTP server)
  2. PHP
  3. Slim Framework (framework for writing RESTful APIs and applications)

Being no fan of PHP, getting this configuration was like putting salt on a wound, but it might be useful for those that want to get up and running in a very short time.  I'll keep the explanations to a minimum to stay true to an Impatient Guide.

  • Download and unzip nginx to c:\nginx-xxx\ 
  • Download and unzip PHP to c:\nginx-xxx\php\ 
  • Open c:\nginx-xxx\conf\nginx.config in a text editor and uncomment the .php (FastCGI) location config to look like this (take note of the bold items):
location ~ \.php$ {
  root           scripts;
  fastcgi_pass   127.0.0.1:9123;
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
  include        fastcgi_params;
  try_files      $uri $uri/ /index.php?$args;
}
  • Download RunHiddenConsole.exe to c:\nginx-xxx\php\ 
  • Create a batch file called start-php-fcgi.bat in c:\nginx-xxx\ with the following (take note of the bold items):
@ECHO OFF
ECHO Starting PHP FastCGI...
set PATH=C:\nginx-xxx\php;%PATH%
RunHiddenConsole.exe C:\nginx-xxx\php\php-cgi.exe -b 127.0.0.1:9123
  • Your PHP files will be put in a directory called scripts so download the Slim Framework into c:\nginx-xxx\scripts\ 
  • Start nginx and FastCGI via a console window:
> cd c:\nginx-xxx\
> start nginx.exe
> start-php-fcgi.bat
> curl.exe -i -X DELETE localhost/index.php/delete
HTTP/1.1 200 OK
Server: nginx/1.2.1
Date: Tue, 26 Jun 2012 00:35:50 GMT
Content-Type: text/html
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.4.4

This is a DELETE route
  • That's it.  You're done.  High Five!!

One neat thing about this toolchain is you can zip everything up and make it portable.  There is no need to mess with installers, environment variables, etc...

Confused?  Visit PHPFastCGIOnWindows.

POSTED • 2012-06-26 20:34:53 • CATEGORY • ENGINEERINGSOFTWARE • TAGS • DEVELOPMENTWINDOWSSLIMNGINXIMPATIENT GUIDEPHP
comments powered by Disqus