Wednesday, May 2, 2012

Moving from Apache-Win32/PHP to IIS6/FastCGI/PHP

Recently, for performance sake, I've decided to migrate an application running on a Apache/PHP server in a Windows 2003 environment to a IIS6/PHP/FastCGI. I've read the instructions on IIS.net and PHP.net site and produced the following brief task list:

  1. Install IIS6 on the server, with the help of Windows CD
  2. Get FastCGI for IIS6 on http://www.iis.net/download/FastCGI
  3. Run FastCGI setup installation
  4. On command line, run the following command:cd \windows\system32\inetsrv
    cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"C:\PHP
    cscript fcgiconfig.js -set -section:"PHP" -InstanceMaxRequests:10000
    cscript fcgiconfig.js -set -section:"PHP" -InstanceMaxRequests:10000
    cscript fcgiconfig.js -set -section:"PHP" -ActivityTimeout:90
    cscript fcgiconfig.js -set -section:"PHP" -RequestTimeout:90

  5. Get PHP binaries with the same build used by Apache instance (5.2.17 in my case) but "VC6 x86 Non Thread Safe"version, since FastCGI doesn't need thread protection
  6. Unzipped to a new PHP directory (C:\PHP directory in this case)
  7. Copy the "php.ini" file from the old instance to the new PHP
  8. Using Notepad (or Notepad++) change the following parameters of "php.ini": fastcgi.impersonate =1
    cgi.fix_pathinfo = 1
    cgi.force_redirect = 0

  9. IIS Manager: create a new Application Pool with default settings
  10. IIS Manager: create new Website pointing to the path where your *.php files reside, and give the permissions Read and Run Scripts at the end
  11. IIS Manager: edit site properties and change the following:
    • On Home Directory tab, change the Application Pool to the one created on step 9

    • On Documents tab, change de Default Content page to index.php


Additionally, the application needs IonCube Loader extension installed on PHP, in order to decrypt their .php files. This was easily done by downloading the binaries and unzipping it to a directory (c:\php\ioncube). After that, I've need to add the following line to "php.ini":
zend_extension="c:\\php\\ioncube\\ioncube_loader_win_5.2.dll"

After this, hopefully, you'll have a new site running PHP Now, the biggest challenge with this move (like most migrations in Windows environment) was with file permissions. But thankfully to Mark Russinovich, there is a tool that help you troubleshoot this issues: PROCMON

I was able to pinpoint the problems with some security permissions and eventually give permissions to the Internet Guest Account (IUSR_[hostname] user) on the *.php files directory.

For security reasons and easier troubleshooting, I've opted for creating a dedicated user for running PHP Application Pool, and giving permission to the filesystem tree below PHP home directory (where *.php files are).

There was a problem with some images not appearing on the browser and being reported on the log files has "404 - File not found". Checking in ProcMon, I found that the application was using image files without file extension, which is them blocked by IIS since there isn't a MIME specified. The problem was solved by adding a new MIME type "*" (asterisk) with value application/octet-stream (check Microsoft KB326965).

Obrigado!

No comments:

Post a Comment