Django is naturally having ‘problem’ to set up on Windows. Consequently, deploying project files requires special attention. Here, we would try to run Django 1.8.1 and Python 2.7.9 on IIS 7. It took me days to resolve all the issues encountered. But for you, following this tutorial carefully will speed up the overall process.
Installing Python & Django
I presume that everyone playing around with django, especially those who are at deploying-files-to-server stage is having much experience in this. For first time user, please kindly search the tutorial on the web. They are excessively available, but I recommend you to read the official tutorial.
I have installed Django on Windows Server 2008 R2 and Windows Server 2012 Essential. It runs perfectly without major problems. However, configuring IIS is the most challenging step. I have been through many tutorial and it seems none of them are intended to describe step-by-step process to run the version I would like to deploy.
- From WebPI, search for ‘FastCGI’. It will return the list of modules available. Choose one that satisfy your current Python release.
- Make sure you have Web Platform Installer (WebPI) installed on IIS. All you have to do is open IIS Manager, click on your computer and see “Management” sections. If nothing is wrong, WebPI is located there. If you dont , you have to WebPI first.
- WFastCGI 2.1 Gateway for IIS and Python 2.7.9 suits my current version. After finish installing, a file (wfastcgi.py) will be compiled to C:\Python27\Scripts\. However, in my case, wfastcgi.py was compiled to C:\Python27_X86\Scripts\. Please note there might be different for another version of server or python you use.
- Copy wfastcgi.py to your project folder.
If you have another site currently running and you dont want to ruin it, simply add new sites. Nonetheless, if you know exactly what you are doing, please continue with Default Sites. All steps given here are pretty straightforward.
Following steps are safer to follow as it does not affect the existing site’s settings.
- Add New Sites. It should be pointing to your project files.
Be careful with the port. If you are unsure that port 80 is available, set another one (it can be 81 or 8081).
- Add Module Handlings.
Click on your site and go to Handlers Mapping. At the right side, click Add Module Mapping.
Fill the form with above data. Note that Executable (optional) field must be filled properly with pipe character | pointing to your previously-copied file wfastcgi.py in project folder.
- Go to your computer. Locate FastCGI settings on IIS Section.
- Click add application.
Full Path: C:\Python27\python.exe
Dont forget to fill Environment Variables with following data:
DJANGO_SETTINGS_MODULE --> asstrack.settings (your project settings) PYTHONPATH --> D:\assettracker\venv\src (your project folder) WSGI_HANDLER --> django.core.wsgi.get_wsgi_application()
Now open http://(your_ip):(your_port)/ (this may be different for you, refer to your setting before). Wait a minute. My static folder works well. I have collected static files via manage.py but why the administration page does not loaded correctly? It looks like the page is missing its css and js file. Well, the workaround was simple.
Go to your static folder, create web.config file containing following code.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <!-- this configuration overrides the FastCGI handler to let IIS serve the static files --> <handlers> <clear/> <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" /> </handlers> </system.webServer> </configuration>
If you have further question, please do not hesitate to contact me via comment.