Last update on .

The Internet Information Services Manager (IIS) from Microsoft is used on Windows Web servers. Most days, people are using Azure and other cloud services to host their websites, so it is often difficult to find help for using Django on stand-alone IIS servers. After lots of research and frustration, I cobbled together some steps to help set up a Django website on IIS.

IIS can also be used on Windows laptops. The steps below are based on this article written by Ryan J. Schave on August 10, 2016 and have been modified as needed.

Note: These steps do NOT refer to using Azure and work with a single web server using at least IIS v7.

Python Steps

  1. Create Python virtual environment on server and install required packages. Note: It is recommended that the virtual environment be outside the project.

    1. Navigate to the venv directory (c:\inetpub\venv) and create the virtual environment. Choose a name that reflects the project.

    2. python -m venv <projectname-venv>
    3. Install required packages using pip install.

  2. Copy/Clone your Django project into appropriate project directory:

  3. Production c:\inetpub\prod\<project name>
    Staging c:\inetpub\staging\<project name>
    git clone https://gitlab.com/<username>/<project>.git
  4. Activate the virtual environment. (Adjust the path as needed.)

  5. source../../venv/Scripts/activate
  6. Create PostgreSQL database. (If you are using PostgreSQL, this must be done separately before starting Python. If you are using Django's default, an sqlite3 database will be created when you start the Django server.)

  7. Migrate the models. This sets up any third-party apps and models.

  8. Create the superuser for the database from the command line.

IIS Steps

Prepare IIS

  1. Copy the wfastcgi.py file installed by the wfastcgi package into the project root directory.

    1. This file is copied from the \Lib\site-packages\ directory from root python. (You should also be able to find it in the virtual environment or venv directory).

  2. Open IIS Manager. Select the root web server and verify the icons for CGI and FastCGI Settings are present.

    1. If you see the icons, continue to the next step of creating the website.

    2. If the icons are NOT present, then you need to enable the CGI feature by doing the following.

      1. Open Server Manager
      2. Click Add Roles and Features from the Manage menu
      3. Navigate through the Wizard until you get to the Server Roles section
      4. Check the CGI box under Web Server (IIS), Web Server, Application Development
      5. Click Install.

Create the Website

  1. Create a new website and set the physical path to the project directory path. (Example: c:\inetpub\staging\<project directory>)

    1. In IIS, in the Connections pane on the left, right-click on Sites and click Add Website.

      1. In the Add Web Site dialog box, type a friendly name for your Web site in the Site name box.

      2. In the Physical path box, click the browse button (...) to browse the file system to find the folder of the project. Select the project name and click OK. (project sites are located in the c:\inetpub\prod\<project directory>.)

      3. In the Port box, type a port number.

        1. Port 80 is the default port for binding type http.
        2. Port 443 is the default port for binding type https.
      4. Enter a host name for each site web site when you have multiple sites on the same server that need to share port 80 or 443. This helps avoid port conflicts.

      5. Click OK to finish.

    2. Optional: To access the specified domain from the local server, you may need to add the localhost route to the 'hosts' file to assist in routing. You may also need to add an entry to your DNS zone file to map it to the public IP address of the web server.

      1. Open Notepad with Administrative rights. (Right-click and select open with Administrative rights.)
      2. Navigate to the directory C:\Windows\System32\drivers\etc\ and open the file named 'hosts' (with no extension).
      3. Add a line to map 127.0.0.1 to the new website and Save the file.

Configure the Handler

  1. Left-click the new website then double-click Handler Mappings, then Click Add Module Mapping (located in actions pane on the right side).

    1. Enter these settings:

    2. Request Path *
      Module FastCgiModule
      Executable

      (all one line)

      C:\inetpub\venv\<projectname-venv>\Scripts\python.exe|C:\inetpub\prod\<project>\wfastcgi.py
      Name <projectname> Django Handler
    3. Click Request Restrictions

      1. Uncheck the option to Invoke handler only if requested is mapped to

      2. Click OK to close the Request Restrictions dialog box

    4. Click OK to close the Edit Module Mapping dialog box

    5. Click YES when prompted to add an entry to the FastCGI Collection in IIS

Set Environmental Variables

  1. Select the root server and Open FastCGI Settings

    1. Double click the new python path you just set up to open it.

    2. Open the Environment Variables collection property box (click the ... button) and add the following environment variables:

    Name

    Example Value

    DJANGO_SETTINGS_MODULE <projectname>.settings OR config.settings.production (as outlined in Two Scoops of Django)
    PYTHONPATH c:\inetpub\prod\<project directory>
    WSGI_HANDLER django.core.wsgi.get_wsgi_application()
    Additional Environment Variables If you are using environment variables (such as secret keys and database credentials) as recommended by Two Scoops of Django then you would add them here as well.
    Note: You will still need to set these additional environment variables manually when running manage.py commands from the console. (See the Django documentation for how to do this.)
    Common env variables:
    DJANGO_ENVIRONMENT_NAME production
    DJANGO_SECRET_KEY 'rhjn3q@xsw#by&5omwceq5blqqev%-y++7((7(_=nee&)(ij79'
    DJANGO_ADMIN_URL example-admin/
    DJANGO_ALLOWED_HOSTS www.example.com
    DATABASE_URL PostgreSQL://username:password@127.0.0.1:5432/dbname
    DJANGO_AWS_ACCESS_KEY_ID AWSaccesskey
    DJANGO_AWS_SECRET_ACCESS_KEY AWSsecretaccesskey
    DJANGO_AWS_STORAGE_BUCKET_NAME s3-bucket-name
    DJANGO_MAILGUN_API_KEY Mailgunapikey
    MAILGUN_SENDER_DOMAIN mg.example.com
    DJANGO_DEFAULT_FROM_EMAIL postmaster@mg.example.com
  2. Click OK to close the Environment Variables Collection Editor dialog box.

  3. Click OK to close the Edit FastCGI application dialog box.

Set HTTP Response Headers

  1. HTTP Response Headers. If you are using outside services, you may need to set up additional parameters such as X-Frame-Options and Content-Security-Policy. (For more information, see this link.)

    1. Select the site in IIS and double-click the HTTP Response Headers icon.

    2. Click Add... in the Actions frame on the right.

    3. Enter the name of the header and the value, then click OK to finish.

Add Virtual Directories for Static and Media files

  1. Static Files - You MUST configure IIS to host the static files. There are some challenges getting Django to serve the static files and some additional IIS configuration needed to make that happen.

    1. Right-click the web site and choose Add Virtual Directory and set the following:

      1. Alias: This will be the name specified for STATIC_URL in your project's settings file (no quotes or slashes).
      2. Physical Path: This will be name specified for STATIC_ROOT in your project's setting file.
    2. Test the connections.

    3. Click OK to close the Add Virtual Directory dialog box.

    4. Select the newly created virtual directory (listed under the website and denoted with a shortcut symbol on the folder) and open Handler Mappings by double-clicking the icon.

      1. Click View Ordered List (located in actions pane on the right side).

      2. Select the Static virtual directory just created (at the bottom) and click Move Up until the entry is at the top of the list.

      3. Click Yes on the Handler Mappings warning dialog box informing you that changing made at the parent level will no longer be inherited at this level.

  2. Media Files (Optional) - If you are using Media files, you must configure IIS to host the media files (similar to what we did with the static files).

    1. Right-click the web site and choose Add Virtual Directory, then set the following:

      1. Alias: - This will be the name specified for MEDIA_URL in your project settings file.
      2. Physical Path: This will be name specified for MEDIA_ROOT in your project settings file.
    2. Click OK to close the Add Virtual Directory dialog box.

    3. Select the newly created virtual directory and Open Handler Mappings

      1. Click View Ordered List (located in actions pane on the right side).

      2. Select the Media virtual directory just created and click Move Up until the entry is at the top of the list under the static file handler previously created.

      3. Click Yes on the Handler Mappings warning dialog box informing you that changing made at the parent level will no longer be inherited at this level.

  3. Browse to the site and enjoy.

Additional Information about Entry

Comments

No comments yet.

Post your comment