Many people might find it not a good solution to deploy Django web applications on IIS but sometimes we really need to do when we have only a windows server. In this post I will guide you step by step that how can we host a Django web application on IIS server. This tutorial is tested on windows 10 and windows server 2016.
Step 1 – First Install python for all users on windows. Follow this – Install python in windows.
Step 2 – Then Create a virtual environment for your Django web application, activate it, and install all requirements for your Django web application. Follow this – Create virtual environment.
Also, install wfastcgi in your virtual environment using
pip install wfastcgi
Step 3 – Ater that, Install IIS on windows with the CGI feature. Follow this blog.
At this point make sure you are able to access http://localhost from your browser which will show the default IIS site.
If not then see if all services for IIS are running.
Step 4 – Next, Open IIS Manager and double click on “FastCGI Settings” as shown in picture.
Step 5 – Now you will see “Add Application” under the Actions section on the right pane. Click on “Add Application”
A popup window will open with the title “Add FastCGI Application”
Now in Full path box add the python script from your virtual environment.
And in the Argument box add the wfastcgi.py file from your virtual environment.
At this point the window should look like this.
Then in General section > Environment Variables click on three dots “…” button next to “(Collection)”, Please note the button will only appear when you click on Environment Variables.
After clicking “…” button you will see the window look like this
FastCGI Environment Variables
In the Members, section click on Add to add a Member.
Now your screen should look like this
Now in the Name section write “DJANGO_SETTINGS_MODULE” and in the value section write “learning_users.settings”
Please note that “learning_users” is the name of my project level app in the Django project where the settings.py file exists replace “learning_users” with your’s name.
Now add two more variables with the following Names and Values.
Name = WSGI_HANDLER
Value = learning_users.wsgi.application
Replace learning_users with your’s name.
Name = PYTHONPATH
Value = C:\PythonWebApps\MyProject
Please note that this path should be the root of your project where manage.py file exists.
Now the screen should look like this
Note – All Variables are case sensitive
Now click on OK to close EnvironmentVaribales Collection Editor.
Again click OK to close Add FastCGI Application Editor.
Step 6 – Django Web Applications on IIS deployment
Now Open the IIS Manager, Expand the server name, Right-click on Sites, and click “Add Website…”
In “Add Website” Dialog enter the name of the website whatever you want.
In “Physical Path” browse and select the root folder of your Django project where manage.py file exists.
Then click on “Test settings” and verify that you don’t have any warnings related to authorization. If there is then try the “Connect As” option and select the user which has administrator access to that computer. If still you have warnings then go to the project folder properties and give full control access to Users.
Now change the port to 81 because on 80 already IIS default website is running. In the real-world we use hostnames and use the same port 80 and the site is selected and the request is processed by the hostname header by the IIS.
For now change it to 81 and leave the hostname blank.
Step 7 – Handler Mappings
Then double click on your website and then in middle pane select Handler Mappings as shown in picture
Now on the right pane under Actions click on “Add Module Mapping…” as shown in picture
Module Mapping Settings
Then in “Add Module Mapping” Dialog.
A – After that Type * in Request Path
B – Then Select FastCgiModule in Module Textbox.
C – And In “Executable(Optional)” section write the following with a pipe separator
D -Finally In the “Name” Section write anything.
Note – Replace the environment path as we did in previous steps the paths are different in the below picture as I captured from an existing project, use the paths from your virtual environment.
The picture is shown here
E – Then click the “Request Restrictions” button and uncheck the “Invoke handler only if request is mapped to” checkbox
After that click OK to close Request Restriction Dialog.
Now click OK to close Add Module Mapping Dialog.
Step 8 – The Last Thing
Finally, add hostname in the settings.py file otherwise you will get an error while accessing the website.
For more information on IIS go to – https://www.iis.net/
And We have done. Now open your browser and type http://localhost:81 you will see your website is running. Thanks for reading the tutorial for “Deploy Django web applications on IIS”
Note – You might see some performance issues like the process will terminate after some time if no request comes in. Must follow “Improve performance of Django and flask on IIS “to fix everything else.