Steps to Deploy Python Django Web Application on Windows IIS Server (2024)

AYUSHI SHARMA

May 17, 2019

·

10 min read

Though many Django Developer might find it might consider it blasphemous, sometimes it is actually necessary to deploy Django applications on Windows/IIS, especially when working with a client that has based its infrastructure around the Windows ecosystem.

This short, focused tutorial walks you through the basic setup of a Django project on Windows. It covers the installation of Python, activating environment.

  1. Download the latest Windows x86–64 executable installer from https://python.org
  2. Double-click the Python installer. If you get a security warning at this point, click “Run” to continue.
  3. In the Python Setup dialog that appears, click “Customize Installation”
Steps to Deploy Python Django Web Application on Windows IIS Server (2)

4. On the Optional Features step, leave the default settings and click “Next”

5. On the Advanced Options step, make the following changes:

  • Check the “Install for all users” box (note that this will also check the “Precompile standard library” box)
  • Check “Add Python to environment variables”
  • In the input box below “Customize install location” change the value to the following: C:\Python35
Steps to Deploy Python Django Web Application on Windows IIS Server (3)

6. Click “Install”

7. When the installation is complete, click “Close”

Confirm the Python Installation

  • Open a new Command Prompt. Note that if you already had a command prompt open you will need to close and reopen it, as the Python installation process added new environment variables and any open command prompts will not have the new environment variables available.
  • Type python and press Enter. This should open a Python interpreter:
Steps to Deploy Python Django Web Application on Windows IIS Server (4)

If instead of seeing the Python interpreter you see the error “‘python’ is not recognized as an internal or external command …” this likely means the “Add Python to environment variables” box was not checked on the Advanced Options step of the installation process. If this occurs you can uninstall and reinstall Python and make sure to check the “Add Python to environment variables” checkbox, or add the following to the System environment “Path” variable:
C:\Python35;C:\Python35\Scripts

  • Exit the Python interpreter by typing exit() and pressing Enter.
  • Close the Command Prompt.

Python virtual environments are used to isolate application-specific Python environments from the system-wide Python environment. Even if you will only be running one application on a server, it’s good practice to create a virtual environment for your application to keep the virtual environment free from any unanticipated side effects of system updates.

1. Open a Command Prompt.

2. Navigate to the root of the C: drive: cd C:\ [then press Enter]

3. Create a new directory in which to store your virtual environments:
mkdir virtualenvs [then press Enter]

4. Navigate to the newly created virtualenvs directory:
cd virtualenvs [then press Enter]

5. Create a new virtual environment. We’ll use the name foo for the purposes of illustration. Note that if you change this, you’ll have to adjust the rest of the steps in this post accordingly. python -m venv Eric [then press Enter]

Activate the Virtual Environment and Upgrade pip

With the virtual environment created, the next steps are to activate the virtual environment and update pip, Python’s package installer.

First, we’ll activate the virtual environment:

  • Open a Command Prompt
  • Navigate to the virtual environment’s directory:
    cd C:\virtualenvs\Eric [then press Enter]
  • Activate the virtual environment:
    Scripts\activate.bat [then press Enter]
  • At this point you will see (Eric) at the beginning of the command prompt, indicating the virtual environment is active:
Steps to Deploy Python Django Web Application on Windows IIS Server (5)

Once a virtual environment is active, all Python-related commands such as python, pip, etc. will be executed against the active virtual environment. For example, with the virtual environment active if a package is installed with pip, that package will only be available in the virtual environment, not in the system’s global Python environment.

Next, to ensure we don’t run into installation or security issues with pip, let’s upgrade it:

  • In the same Command Prompt, you opened above, with the virtual environment activated, run the following command:
    python -m pip install — upgrade pip [then press Enter]
  • If an update for pip is available, it will be downloaded and installed.
  • Then install all the dependent modules and packages related to the project using command and make sure you are running it in same path where requirement.txt file is present by- pip install -r requirement.txt.
  • Install gitbash from https://git-scm.com/download/win for 64-bit
  • Open the gitbash and go to the path where you want to copy your project.
  • Download the project from git using command in git bash: git clone https://github.com/ayushi21095/User_details.git
  • Go to the path where your project file is located in command prompt where virtual environment was activated.
  • In the Command Prompt with the virtual environment activated, install Django: pip install wfastcgi [then press Enter]
  • Start the Django development server: python manage.py runserver [then press Enter]
  • You should see the following output:
Steps to Deploy Python Django Web Application on Windows IIS Server (6)
Steps to Deploy Python Django Web Application on Windows IIS Server (7)
  • Go back to the Command Prompt and press Ctrl-C to stop the Django development server.

Even if you already have IIS installed, you may need to go through these steps to install the CGI feature if it wasn’t installed as part of the original IIS installation.

  • Open the Control Panel
  • In the search box in the top right, type “windows features” (without the quotes)
  • In the search results under “Programs and Features” click “Turn Windows features on or off.” This launches the Add Roles and Features Wizard.
Steps to Deploy Python Django Web Application on Windows IIS Server (8)
  • Make sure to check CGI box.

Verify the IIS Installation

  • Open a web browser on the server.
  • Enter http://localhost in the address bar and press Enter. You should see the default IIS page.
Steps to Deploy Python Django Web Application on Windows IIS Server (9)
  • If you don’t see the default IIS page:

1. Open Control Panel

2. Type “services” in the search box

3. Under “Administrative Tools” click “View local services”

  • Scroll to the bottom of the list and ensure you see “World Wide Web Publishing Service” listed, and that the status is “Running”.

Thus far we’ve accomplished the following:

  • Installed Python
  • Installed IIS with CGI
  • Created and activated a Python virtual environment
  • Installed Django and wfastcgi in a Python virtual environment
  • Created a new Django project
  • Ran the Django project using the Django development server

With all the underlying infrastructure in place, the last step is to configure IIS to serve Django applications. The Django development server is not designed for production use, so using a production-quality web server such as IIS is mandatory for production applications.

As with previous steps you’ll need to adjust accordingly for the actual application you want to deploy, but for the purposes of this documentation we’ll configure IIS to serve the test Django application we created in the previous steps.

  • Open the IIS Manager by clicking the Windows button, clicking on Administrative Tools, and double-clicking Internet Information Services (IIS) Manager
  • Click on the name of the server in the list on the left. If you see the following dialog box, check the box “Do not show this message” and click “No.” (You can always get to this later if necessary by clicking “Get New Web Platform Components” under “Actions” on the right-hand side of IIS Manager.)
Steps to Deploy Python Django Web Application on Windows IIS Server (10)
  • Double-click the “FastCGI Settings” icon
Steps to Deploy Python Django Web Application on Windows IIS Server (11)
  • Under “Actions” on the right-hand side click “Add application …”
  • In the Add FastCGI Application dialog, in the “Full Path” box, type the path to the Python executable for the application’s virtual environment:
    C:\virtualenvs\Eric\Scripts\python.exe
  • In the Arguments input box, type the path to the wfastcgi.py file in the application’s virtual environment:
    C:\virtualenvs\Eric\Lib\site-packages\wfastcgi.py
  • At this point your settings in the Add FastCGI Application dialog should look like this:
Steps to Deploy Python Django Web Application on Windows IIS Server (12)
  • With the Add FastCGI Application dialog box still open, under the “General” section, click on the “Environment Variables” line, then click the gray “…” button that appears next to (Collection) on the right-hand side of the line. This opens the Environment Variables Collection Editor dialog.
Steps to Deploy Python Django Web Application on Windows IIS Server (13)
  • In the Environment Variables Collection Editor dialog, click “Add”
  • In the “Name properties” section on the right, click the input box to the right of “Name,” remove the “Name” text that is already in the input box, and enter DJANGO_SETTINGS_MODULE (note that this MUST be entered in ALL CAPS)
  • Click the input box to the right of “Value” and enter Ericsson.settings
  • Click “Add” again and enter the following:
    Name: PYTHONPATH
    Value: C:\User_details\Ericsson
  • Click “Add” again and enter the following:
    Name: WSGI_HANDLER
    Value: Ericsson.wsgi.application
  • At this point you will have three environment variables:
Steps to Deploy Python Django Web Application on Windows IIS Server (14)

NOTE: All of these settings are CASE-SENSITIVE. They must be entered with exactly the case indicated here in order to work.

  • Click “OK” to close the EnviromentVariables Collection Editor.
  • Click “OK” to close the Add FastCGI Application dialog.

Next we need to create a new web site in IIS for the Django application, and add a Handler Mapping to the newly created web site so that requests coming to the IIS web site are passed off to the Django application for processing.

  • Open IIS Manager
  • On the left-hand side under Connections, expand the tree under the server name by clicking on the arrow to the left of the server name
  • Right-click on the Sites folder and click “Add Website …”
  • For the site name enter Ericsson
  • For the physical path, type the following: C:\User_details\Ericsson
  • For the purposes of this example configuration, change the Port to 81, since the Default site is running on port 80. For a real-world application you’ll likely want to use name-based virtual hosting by adding bindings and run the site on port 80.

You may leave the “Host name” blank. At this point the Add Website dialog should look like this:

Steps to Deploy Python Django Web Application on Windows IIS Server (15)
  • Click “OK”

Next, we’ll add a FastCGI handler mapping to this site so the requests coming into the site will be processed by the Django application.

  • In IIS Manager, expand the Sites folder on the left-hand side and click on the site
  • On the right, double-click “Handler Mappings”
Steps to Deploy Python Django Web Application on Windows IIS Server (16)
  • On the right, under “Actions,” click “Add Module Mapping”
Steps to Deploy Python Django Web Application on Windows IIS Server (17)
  • In the “Request path” box enter an asterisk: *
  • Click the arrow on the right-hand side of the “Module” box and select “FastCgi Module”

NOTE: Make sure to select FastCgi Module, NOT CgiModule

  • In the “Executable” box, enter the following:
    C:\virtualenvs\Eric\Scripts\python.exe|C:\virtualenvs\Eric\Lib\site-packages\wfastcgi.py

Note that the character after python.exe is a pipe (|), which is entered by pressing Shift-\ on your keyboard

  • In the “Name” box, enter Django Handler (you can call this whatever you want; it’s merely a friendly name for the module mapping)
  • The Add Module Mapping dialog should now look like this:
Steps to Deploy Python Django Web Application on Windows IIS Server (18)
  • Click the “Request Restrictions” button and uncheck the “Invoke handler only if request is mapped to:” checkbox
  • Click “OK” to close the Request Restrictions dialog
  • Click “OK” to close the Add Module Mapping dialog
  • When prompted “Do you want to create a FastCGI application for this executable?” click “No” since we created the application earlier.

Note that you CAN have it create the FastCGI application for you from the module mapping, but the settings seem to be different and the end result isn’t fully editable. I also detailed how to create the FastCGI application to be as thorough as possible with all the various pieces involved.

  • You will now see the Django Handler listed in the Handler Mappings for the foo website:
Steps to Deploy Python Django Web Application on Windows IIS Server (19)

At this point everything should be working, so verify by loading the application in a browser.

You should see the following:

Steps to Deploy Python Django Web Application on Windows IIS Server (20)

This is the same result as when we ran the Django test server earlier, but now the requests are being handled by IIS.

Make sure in setting.py file under you project folder you do:

1. DEBUG = False

2. ALLOWED_HOSTS= [‘localhost’,’server ip’]

Steps to Deploy Python Django Web Application on Windows IIS Server (2024)

FAQs

How do I deploy Python Django web application in Windows IIS? ›

Deploy Django Application on Windows IIS Server
  1. Step 1: Create VM. ...
  2. Step 2: Download the Python 3 Installer. ...
  3. Step 3: Git for Windows stand-alone installer. ...
  4. Step 4: Configure Database. ...
  5. Step 5: Configure Python Virtual Environment. ...
  6. Step 6: Install IIS. ...
  7. Step 7: Configure IIS.
  8. Step 8: Create and Configure a New IIS Web Site.

Does IIS support Django? ›

Django is a framework which has its own URL routing, so Django apps must be installed as a “handler” in IIS for specific paths. To install a Django app on the IIS's Default Web Site, select it in the management console, and open the Handler mappings configuration feature.

How do I host a Django site in Windows? ›

Deploying a Django application in Windows with Apache and...
  1. Make sure both the checboxes at the bottom of the window are checked (Install for all users, and Add Python 3.7 to PATH checkboxes) ...
  2. MySQL. ...
  3. Download the latest Visual C++ from here and install it. ...
  4. Click on I accept the license terms and click Next.
29 Apr 2019

How do you deploy a Python web application? ›

Python Web Applications: Deploy Your Script as a Flask App
  1. Brush Up on the Basics. Distribute Your Python Code. ...
  2. Build a Basic Python Web Application. Set Up Your Project. ...
  3. Deploy Your Python Web Application. ...
  4. Convert a Script Into a Web Application. ...
  5. Improve the User Interface of Your Web Application. ...
  6. Conclusion.

How do I deploy a Python application in Windows? ›

Let us know.
  1. Go to the application folder: Console Copy. cd msdocs-python-django-webapp-quickstart.
  2. Create a virtual environment for the app: Windows. macOS/Linux. Cmd Copy. ...
  3. Install the dependencies: Console Copy. pip install -r requirements.txt.
  4. Run the app: Console Copy. python manage.py runserver.
23 Aug 2022

Does Django need a web server? ›

Django, being a web framework, needs a web server in order to operate. And since most web servers don't natively speak Python, we need an interface to make that communication happen. Django currently supports two interfaces: WSGI and ASGI.

How does Django Project deploy on production? ›

To learn where and how you can deploy a Django app to production.
...
Overview
  1. Make a few changes to your project settings.
  2. Choose an environment for hosting the Django app.
  3. Choose an environment for hosting any static files.
  4. Set up a production-level infrastructure for serving your website.
13 Oct 2022

How do I run Django server? ›

Use the Django admin console
  1. Create a superuser. You will be prompted to enter a username, email, and password. python manage. py createsuperuser.
  2. Start a local web server: python manage. py runserver.
  3. Log in to the admin site using the username and password you used when you ran createsuperuser .

How do I turn on Wfastcgi? ›

Try to run the wfastcgi-enable.exe in command prompt as system administrator write "cmd" on windows start, then right click and run the command prompt as administrator. then go to the directory where your python environment install the fastcgi . then run the wfastcgi-enable.exe command That is how I made it work.

How do I install IIS on Windows? ›

Enabling IIS and required IIS components on Windows 10
  1. Open Control Panel and click Programs and Features > Turn Windows features on or off.
  2. Enable Internet Information Services.
  3. Expand the Internet Information Services feature and verify that the web server components listed in the next section are enabled.
  4. Click OK.

How do you use waitress in Django? ›

You could just install Waitress itself, and run the ``waitress-serve`` command it provides, pointing to your ``wsgi.py`` file; the only thing that this project does is provide a Django management command, and remove the need for a ``wsgi.py`` in your project. <span property="dct:title">cookiecutter-django</span>.

Top Articles
Latest Posts
Article information

Author: Arielle Torp

Last Updated:

Views: 6036

Rating: 4 / 5 (61 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Arielle Torp

Birthday: 1997-09-20

Address: 87313 Erdman Vista, North Dustinborough, WA 37563

Phone: +97216742823598

Job: Central Technology Officer

Hobby: Taekwondo, Macrame, Foreign language learning, Kite flying, Cooking, Skiing, Computer programming

Introduction: My name is Arielle Torp, I am a comfortable, kind, zealous, lovely, jolly, colorful, adventurous person who loves writing and wants to share my knowledge and understanding with you.