Saturday, December 25, 2010

Installing Django on IIS 7 Using PyISAPIe

  1. PyISAPIe is an ISAPI extension which can be downlod from here . Download the latest extension according to installed python version.
  2. Just make sure that ISAPI and ISAPI filters has been installed on IIS (During IIS installation these two component should have checked).
  3. Copy PyISAPIe.dll file and Http folder into your Django website project.Example:
    • Suppose your website folder path is: "C:\Python25\Lib\site-packages\MySite"
    • "C:\Python25\Lib\site-packages\MySite\PyISAPIe.dll" and "C:\Python25\Lib\site-packages\MySite\Http" will be the result.
  4. Copy and replace the isap.py from "PyISAPIe\Examples\Django" folder to "C:\Python25\Lib\site-packages\MySite\Http"
  5. IIS need to have access to your project folder and also django folder. so you need to give access "Network Service" to these folder.
    • Right click on site folder "C:\Python25\Lib\site-packages\MySite"  click on properties. On properties window choose the security tab then click on Edit then Add and choose the "Network Service"
    • Do the same procedure for the Django folder: "C:\Python25\Lib\site-packages\django"
  6. Edit the "isapi.py" file and set the DJANGO_SETTINGS_MODULE to your site settings file.
    • If  the setting file is"C:\Python25\Lib\site-packages\MySite\settings.py"  and site-packages is on your path then you should set: os.environ["DJANGO_SETTINGS_MODULE"] = "MySite.settings"
  7. Open the IIS Manager.
  8. Right click on "Sites" then choose "Add Web Site..."
  9. On "Add web Site" window fill the "site name" (an application pool with the same name will be created automatically).Set the "Physical Path" to your website folder in this example it will be "C:\Python25\Lib\site-packages\MySite". Set the Host Name an IP address then click "OK"
  10. On IIS manager at left tree click on "Application Pools" there is an application pool with the name of your site name. Right click on it and then choose "Advance Settings...". On this window just make sure that "Enable 32-Bit Application" has been set to "true" and also "Identity" property has been set to "Network Service". Click "OK".
  11. On IIS manager at left tree under "Sites"click on your website and on the center panel double click on "Handler Mappings". 
  12. On the right panel choose "Add Script Map..." a new window will be open.
  13. Set the "Request Path" to "*". Set the "Executable" to "PyISAPIe.dll" file in this example it will be"C:\Python25\Lib\site-packages\MySite\PyISAPIe.dll". Choose any name like "PyISAPIe" and then click "OK". A window will appear regarding adding the "ISAPI Filters" choose "OK".
  14. On the right panel choose the "View Ordered List...". Just make sure that the recently added isapi is above the "Static File".Otherwise, move the PyISAPIe so it locate above the StaticFile.
  15. "Stop" and then "Start"  the website and its Application Pool( Recycling on application pool is not working).
  16. Browse to your website and every should be fine.
Important!
  • Current version has been compiled on x86-32-bit platform. You need to install x86 python version so this extension can work. Otherwise, you need to compile it from source for 64-bit platform.
  • "Http" folder name is case-sensitive do not change the name.
Update

  • I have compiled PyISAPIe on 64 bit python 2.7. Files are available on this post to download.
  • For deploying  the 64-bit PyISAPIe.dll on IIS and django, after downloading the file, follow these instructions.
    1. Copy the folder to drive C.
    2. Give the 'Network Service'  permission  to the folder .
    3. From Example\Django copy the Isapi.py and DjangoMap.py to Http folder
    4. Edit the Isapi.py and add your website settings
    5. Now from IIS add the PyISAPIe.dll to Handler Mapping  as Script Map. Make sure on the ordered list view it is located at the top of list.
    6. On 'Application pools' advance settings make sure that "Enable 32-Bit Aplications" is set to False and "Identity" is Network Service.
    7. Do a recycle and enjoy!
For more information you can check out PyISAPIe project HomePage.