in Python

Running Django 1.8.1 on IIS 7+ with Python 2.7.9

python-djangoDjango 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.

Configuring IIS

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.

Prerequisites

  • From WebPI, search for ‘FastCGI’. It will return the list of modules available. Choose one that satisfy your current Python release.WebPI - WFastCGI 2.1 Gateway for IIS and Python 2.7.9
  • 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.WebPI on IIS
  • 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.

Configuring Sites

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.

Add Sites on IIS

Add Sites Properties on IIS

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.

Add Module Mapping on IIS

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.

Configuring FastCGI

  • Go to your computer. Locate FastCGI settings on IIS Section.
  • Click add application.

 Add FastCGI Application on IIS

Full Path: C:\Python27\python.exe

Argument: D:\assettracker\venv\src\wfastcgi.py

 Environment Variables Collection Editor on IIS

Dont forget to fill Environment Variables with following data:

 Post Steps

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.

 

If you have further question, please do not hesitate to contact me via comment.

Write a Comment

Comment