|Version 10 (modified by sfb, 8 years ago) (diff)|
Getting Django to run on Windows, with pages served by IIS and a SQL Server backend. Assume Windows Server 2003, IIS 6 and SQL Server 2005 where no versions are mentioned.
Useful for people who already have such a setup and can't justify (or just don't want) Apache and PostgreSQL as well.
Note: This doesn't appear to work with Django 0.91 (the pyisapie.py handler is written to the refactored APIs in 0.92 and later) [SF]
Credit to: http://thinkhole.org/wp/2006/04/03/django-on-windows-howto/ for the Django on Windows with Postgres/Apache guide, from which I took some parts of the django-on-windows setup.
- Windows Server
- IIS installed and working serving normal pages
- SQL Server installed and working
- Install Python
- Install PyISAPIe - an extension for IIS
- Install Django
- Connect up Django and IIS
- Configure Django to look at SQL Server
A simple download and install for this bit, so open http://www.python.org/ and look for "Quick Links" -> "Windows Installer" in the menu on the left. Download and run it, following the wizard steps until it is complete. Easy bit over with, let's get Python talking to IIS.
Install PyISAPIe - an ISAPI extension for IIS
There is no installer for this, so there are quite a few steps, but they aren't very complicated.
This is an IIS extension that loads the Python interpreter into memory while IIS is running, and uses it to serve requests more quickly. It avoids the overhead that CGI has of starting Python for every request, however it does mean that some files are cached in memory and you wont see changes while testing your Django site until you restart the process. With IIS 6 you have to right-click on the application pool running your services and select 'recycle' for changes to take. Previous versions of IIS might the whole IIS service to be restarted.
Go to http://pyisapie.sourceforge.net/ and download it (look in the menu on the right, under "Links" for a download link). Unzip the archive so you can copy files out of it.
I will assume you've extracted it to c:\pyisapie, and are using c:\python24 as the folder where Python is installed, but please change these where necessary for your setup.
There is documentation in the readme files, but at the time of writing the install consists of:
Setting up Files
- Copy PyISAPIe.dll to c:\python24\
- Edit the properties -> security settings on that file, add "Network Service" and tick "read" permission. (So IIS can use it).
- Go to c:\pyisapie\source\PyISAPIe\Python\ and copy the entire Http folder to c:\python24\lib\site-packages. Note: the Http folder is case sensitive. Saving in 'http' (or any other variation) will not work [SF].
Setting up IIS
You don't have to create a virtual directory - you can do this with the root folder. If you do use a virtual directory, say /myfolder, then only urls starting with /myfolder will be handled by PyISAPIe. If you use the root folder, all URLs will be handled by PyISAPIe, and you risk everything else on the site being inaccessible afterwards if you're not careful.
- Open the IIS Management Console, and create a new virtual directory, and allow executing ISAPI extensions when prompted by the wizard.
- View the properties of the new folder and click on the "configuration" button (if it's greyed out, click 'create' first), then add a new wildcard extension (the lower box), locate the pyisapie.dll file and untick the "check file exists" box.
- In the IIS Manager, go to the "Web Service Extensions" section, and right click -> add new web service extension.
- Give it a name (it doesn't matter what), add the pyisapie.dll fill as a required file and check the box to set the extension status to allowed.
- IIS 5.x doesn't support wildcard application maps, at least not through the configuration interface. On IIS 5, the procedure is as follows:
- Right Click on virtual directory and choose "properties"
- Ensure "Execute Permissions" is set to "Scripts and Executables"
- Click "Configuration" (opposite "Scripts and Executables")
- On "App Mappings" tab, select "Add"
- For Executable, browse to pyisapie.dll (needs full path if you enter manually)
- For extension enter * (this will redirect all requests to pyisapi)
- You should probably limit the verbs allowed; I left as all since it was for internal use.
- 'OK' to close all the open dialogs.
That's it installed. In c:\pyisapie\source\PyISAPIe\Python\examples folder is a file called Info.py. Copy this to your new virtual directory folder, and then try and view http://site/Info.py to test it. It should work. If it doesn't, when you find out why, please come back and explain what happened and how you fixed it. ;)
The PyISAPIe extension has a driver for Django as well, so that's where I will be heading, after this:
I'm not going to say what this is; if you're here, following this, then you should really know by now!
Get the development version if you wish
If you want to keep up with the latest Django developments, you will need to download and install a subversion client for Windows. (e.g. Tortoise SVN, install it, then create a new folder somewhere. Right click on it, and choose to SVN Checkout. Give the URL of the repository as: http://code.djangoproject.com/svn/django/trunk/ and click OK.)
Or get the normal version
Otherwise, just download Django from the main site, and extract the archive to a folder.
Do The Install
Drop to a command prompt (start -> run -> cmd -> OK), change to your Python folder and install Django. Example:
c:\> cd python24\django c:\python24\django> python setup.py install
NB: You will probably need to do some messing around with paths and such to make this work neatly. I'm afraid I'm leaving that to you for now. Without doing that you can still test the Django install, but will need a full path to django-admin.py:
E:\> md test e:\> cd test E:\test>d:\Python24\django\django\bin\django-admin.py startproject proj E:\test>cd proj E:\test\proj>python manage.py runserver Validating models... 0 errors found. Django version 0.95 (post-magic-removal), using settings 'proj.settings' Development server is running at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK.
Now open a web browser, and visit the site. It should serve a neat page to you.
Linking Django to PyISAPIe
Next, you must follow the readme in the PyISAPIe examples\django folder (it explains where to copy two files - one goes into the Django install (One goes into python24\lib\site-packages\django-xyz-123.egg\core\handlers\). Then edit the isapi.py file you just copied to lib\site-packages\Http\. See where it says you should change a line? If applicable, edit it for your django.settings.module settings.
Serving Django with IIS
You have created a new virtual folder in IIS, and added the PyISAPIe extension and tweaked it to drive django sites. There is also an addition to Django so it can be driven by PyISAPIe. And, you can now use django-admin.py to create a site inside the virtual folder.
You have everything you need to have Django running from IIS.
... I'll leave you to sort out your Import Path and DJANGO_SETTINGS_MODULE and stuff ...
Now, while I don't have an app running, I do have Django errors rather than IIS errors. I need to get my model access sorted for my test app.
Configure Django to look at SQL Server
With any luck, the suggestion that there is an ado_mssql backend will do, and this will be easy.