Skip to main content

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.

Comments

  1. You can use Helicon Zoo to easily install Django on IIS 7.

    ReplyDelete
  2. I followed the instructions of the python 2.7 version, and got the following error:

    Possible recursion detected!
    You probably did a passthrough with PyISAPIe configured as an application map instead of a wildcard map.

    Any idea why?

    ReplyDelete
    Replies
    1. I have tried "Script Map" with following configuration:

      request path: *
      Executable: c:/path/to/pysapie.dll
      Name: PyISAPIe
      Access: Script
      Verb: All Verbs
      Mapping: unchecked the "Invoke only if request is mapped to"
      python version is: 2.7.2
      and Isapi.py is like this:
      #-------------------------------------
      ["/" , "mysite.settings" ], # first Django app

      ###comment out the following if there is on other site###
      #["/2" , "mysite2.settings" ], # second
      #-------------------------------------

      I have not encounter the error you mentioned. I hope this will help.Otherwise if you explain more about the error and your configuration I could help more.

      Delete
    2. Using "Wildcard Script Map" instead of "Script Map" worked for me.

      Delete
  3. Hi,


    What is the correct ip congiguration for the web site create in the iis manager?

    Thanks

    ReplyDelete
    Replies
    1. It depends how many ip with how many domain name you are having on the server or if you are using NAT or not.
      If you are doing one ip on a single domain you can add a Binding as below:

      Type: http
      IP Address: All unassigned
      Port:80
      Host Name: yourdomain.com

      If on domain controller also sending www sub domain www.yourdomain.com to the same ip you need also to add a Binding with www.yourdomain.com as Host Name.

      Delete
    2. Hi,

      Thak you very much for your help.

      I'm using python 2.7 and iis 7.5, I follow the step of your tutorial but i get some errors.

      I'm working in a local machine (localhost).

      I included a simple page html, when i see in the browser http://localhost/prueba.html it succes. when i acces http://localhost the browser show the iis welcome page.

      excuseme for my ignorance. I'm getting started with iis and django.

      Luis

      Delete
    3. There is a "Default Web Site" running on iis which has the "localhost" as its binding, that is why you are seeing iis welcome page. Stop the site and its application pool Then add localhost to your site binding.
      About the "http://localhost/prueba.html", is this url defined in django and are you rendering it from django or it is a simple html file with the same path in the root folder of your website?
      If this is not a django url and simply it is a html file then your configuration is wrong, because all request should handle by Pyisapie; to fix this in iis click on your website then on center panel click on "Handler Mappings" then on the right panel click on "view ordered list" on view ordered list PyISAPIe handler should be the very first one. if it is not, select PyISAPIe and then on right panel click on move up until you see the PyISAPIe at beginning of the list.
      Now, recycle the application pool and restart the website.
      try 'http://localhost/prueba.html' on browser if this is not a django url you should see IIS Error page.

      Then define a url in url.py and for the related view just define a function, having a simple HttpResponse('hi....') as its return, recycle the application pool and try the url on browser.

      Good Luck

      Delete
    4. Hi,

      Thanks for this guide. I followed the steps show in this tutorial but when i browse the website
      appear a list with the files on directory but when click on a file like views,py that contains a simple HttpResponse("Hello world")
      am getting the following error as:

      " HTTP Error 404.17 - Not Found
      The requested content appears to be script and will not be served by the static file handler. "

      My sistem especification are:

      1)Windows Seven , 64 Bit

      2)ISS 7.0

      I also put a html inside my directory called test and was shown correctly.

      Thanks.

      Delete
  4. Appearing the file and folder list on browser and the error you are receiving shows that your project is serving by static file handler instead of PyISAPIe and this is because of wrong configuration on handler mappings.
    To fix this on IIS select the website on the left panel, then on center panel select "Handler Mappings". you will see the list of handlers. First make sure PyISAPIe is on the list, second on the left panel click on the "View ordered list" now on center panel you should see the PyISAPIe on top of the list above any other handlers, if it is not you should move the handler to the top of the list by using the Arrows on right panel. Now your django site will be served by PyISAPIe handler. restart the site and its application pool.
    Hope this will help. Good luck.

    ReplyDelete
    Replies
    1. Thanks for your reply, I selected the website and then selected "Handler Mappings"
      on center panel and when I clicked on the "View ordered list" PyISAPIE was on top of the
      list and StaticFile was on bottom but now I am getting the following error:

      HTTP Error 404.3 - Not Found

      The page you are requesting cannot be served because of the extension configuration.

      when I chose "Add Script Map..." I Set the "Request Path" to "*", the "Executable" to
      "C:\Python27\Lib\site packages\mysite\PyISAPIe.dll". where is located the django project that Im testing and in name I put PyISAPI.

      Delete
    2. Other information of the error page is shown below:

      Module StaticFileModule url:http://127.0.0.1:80/prueba/views.py
      Notification ExecuteRequestHandler path:C:\Python27\Lib\site-packages\mysite\prueba\views.py
      Controller: StaticFile
      user: anonymous
      code error 0x80070032

      Delete
    3. this info: "Module StaticFileModule" shows that your django site is still handling by static handler not pyispie. remember set the handler mapping should be set for the whole site not only some app folder.

      Delete
  5. Did you gave "network service" permission to the mysite folder.
    On 'Application pools' advance settings make sure that "Enable 32-Bit Aplications" is set to False and "Identity" is Network Service.

    ReplyDelete
    Replies
    1. I gave all privilegies to mysite folder from "Network services" and on 'Application pools' I set "Identity" in Network Service ¿"Enable 32-Bit Aplications" is set to false or true? In the tutorial said tha it had to be set to True.

      Delete
    2. other thing is: I selected the folder test where is located the test page and I clicked on convert on aplication and when I test the conection appeared that identity of 'application pool' is valid but also is shown a warning that says: it cant verify the access to path: "C:\Python27\Lib\site-packages\mysite\prueba".

      Delete
    3. when I click on browse the site it appeared the following error:
      Could not initialize interpreter


      Traceback (most recent call last):
      File "C:\Python27\Lib\site-packages\mysite\Http\Isapi.py", line 19, in
      os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"
      NameError: name 'os' is not defined

      The handler mapping I have it set for the site that I added called pruebadjango not in the app.

      Delete
    4. In apps also I have to set it or only setting it on the site is enough

      Delete
    5. when you set it for whole site it will take affect for all apps unless you have changed it manually before. Then you need to do it for all app.

      Add "import os" at top of the Isapi.py
      Where did you grab the pyisapie files?
      Are you using singleInstance Isap.py?
      Add "import os" at top of the Isapi.py

      if you have changed the configuration many times it is better to delete the website and its application pool. Then try to do the steps from scratch.

      Delete
    6. Thanks very much, I deleted the website and its application pool and I try the steps again, this time I used the Isapi.py file that is located inside the folder singleinstance and all works fine, but when I used the Isapi.py file inside Django Folder was appeared a 500 error. what are differences between both files? Another question if my file settings.py werent on the root of my site ¿How should I set the DJANGO_SETTINGS_MODULE properly?

      Delete
    7. If you are running only one django project on the server you should use the Isap.py which is in singleinstance folder otherwise you can use the other one.

      settings.py should exist at root of your site. if you are having different settings on development machine and server the best solution would be extracting settings differences from the settings.py and putting it in different file out side the website folder and importing it on settings.py.

      Delete
    8. But why when I run the app using the Isapi.py which is in Django folder not in singleinstance folder it doesn´t work ang it gave a 500 error. And if my settings.py is on root of my site but inside another folder like for example src.In src I have the files:
      manage.py
      settings.py
      urls.py

      At the same level of src I copied the Http folder and PyISAPIe.dll ¿how should I reference the settings.py in this case?

      Delete
    9. Good night,

      I have a Django project with extjs 4 If I run it with the web server that Django has by default it works correctly, but I need to run it on IIS. Im using the PyISAPIE.dll but I have problems loading the styles and scripts js. It gave me a 404 error-not found. Do you know what could be the cause of this problem?

      Thanks.

      Delete
    10. Static files(js, css, media) should serve by IIS static handlers. For serving static files, on IIS select the folder you want to serve statically. Then on center panel choose "handler mappings" then click "View ordered list" on the right panel move the "StaticFile" to the top, restart the website and application pool.

      Note:

      if on your django project, defined media url path is different than its directory path you need to create a virtual directory under the website with the same path as url then refer it to the media directory path. After that change the "Handler mappings" to "StaticFile" for the recently created virtual directory.

      Delete

Post a Comment

Popular posts from this blog

Installing MS SQL Server driver on Ubuntu Linux

Although making a connection to SQL Server can be done using unixODBC and FreeTDS, it does not work with some collations on SQL Server 2008. Recently, Microsoft released an ODBC driver for Linux that works on 64-bit systems only. Installing the driver is tricky. I begin by installing the build-essential, which is required for compiling from source. Checkinstall can be installed if you want to add the following application to the package manager. $ sudo apt-get install build-essential  Download unixODBC-2.3.0  and Microsoft ODBC dirver  and extract. $ sudo wget ftp://ftp.unixodbc.org/pub/unixODBC/unixODBC-2.3.0.tar.gz $ tar xvf unixODBC-2.3.0.tar.gz $ wget http://download.microsoft.com/download/6/A/B/6AB27E13-46AE-4CE9-AFFD-406367CADC1D/Linux6/sqlncli-11.0.1790.0.tar.gz $ tar xvf sqlncli-11.0.1790.0.tar.gz  Install the unixodbc using the following commands: $ ./configure --disable-gui --disable-drivers --enable-iconv --with-iconv-char-enc=UTF8 --with-ico...

Installing pyodbc on ubuntu

Installing pyodbc is straight forward but if  getting     "error: command 'gcc' failed with exit status 1"  when installing pyodbc, make sure you have installed  requirements  as below. sudo apt-get install unixODBC-dev g++ pip install pyodbc

Monkey Patching Django Model Class

Django is shipped with some useful application, also there are lots of third party re-usable application written for Django. Sometime you need to modify or customize these applications(Models of application) to meet your software requirements. One of the usual solution is adding a Model class to have a one-to-one relationship with the third party Models. This solution has some disadvantages such as having more tables and classes which can highly adversely affect database performance, scalability and add more complexity to the software development.  I think of another solution to this problem. I used Django South along with monkey patching of third party Model classes. I added a field to Django user class. Here is a walk through over this approach. 1. Add an application: python manage.py startapplication userpatch 2. Add the following  code to add the new field to the user class from django.db import models from django.contrib.auth.models import User ...