psolomon
psolomon

Reputation: 173

Hosting Django web application on IIS error

My organization has implemented a web application in Django, and we need to host it on a Windows system. We are using Django 3.2.8 and Python 3.8.8.

The Django project is currently stored here:

C:\inetpub\wwwroot\CED_HRAP

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
          <add name="Python FastCGI" path="\*" verb="*" modules="FastCGIModule" scriptProcessor="c:\python38\python.exe|c:\python38\lib\site-packages\wfastcgi.py" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
        <defaultDocument>
            <files>
                <clear />
                <add value="Default.htm" />
                <add value="Default.asp" />
                <add value="index.htm" />
                <add value="index.html" />
                <add value="iisstart.htm" />
                <add value="base.html" />
            </files>
        </defaultDocument>
        <directoryBrowse enabled="false" />
    </system.webServer>

    <appSettings>
        <add key="PYTHONPATH" value="C:\inetpub\wwwroot\CED_HRAP" />
        <add key="WSGI_HANDLER" value="CED_HRAP.wsgi.application" />
        <add key="DJANGO_SETTINGS_MODULE" value="CED_HRAP.settings" />
    </appSettings>
</configuration>

Our settings.py file is located at

C:\inetpub\wwwroot\CED_HRAP\CED_HRAP\settings.py

We are currently seeing this error message:

HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory.

Most likely causes:

A default document is not configured for the requested URL, and directory browsing is not enabled on the server.

How should we configure the default documents in the web.config file so that it links to Django's internal routing (urls.py)?

We followed the instructions here: https://github.com/Johnnyboycurtis/webproject (youtube: https://www.youtube.com/watch?v=APCQ15YqqQ0)

You can see that the web.config file in the above repository does not have any default documents specified. We added them to web.config in an effort to resolve the current error.

Have also looked at several other tutorials and documents on this process, but have not been able to resolve the problem.

Thanks!

Upvotes: 0

Views: 1810

Answers (2)

Lex Li
Lex Li

Reputation: 63143

The instructions you found were actually broken (missing details) and not recommended (wfastcgi was deprecated).

If you revert all changes related to FastCGI and start cleanly with HttpPlatformHandler, you will see how smooth the experience is,

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="httpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified" requireAccess="Script" />
        </handlers>
        <httpPlatform stdoutLogEnabled="true" stdoutLogFile=".\python.log" startupTimeLimit="20" processPath="C:\Users\<user name>\AppData\Local\Programs\Python\Python310\python.exe" arguments=".\mysite\manage.py runserver %HTTP_PLATFORM_PORT%">
        </httpPlatform>
    </system.webServer>
</configuration>

Python/Django still takes care of everything including static files, and IIS/HttpPlatformHandler just passes requests on.

More details can be found from my blog post.

Upvotes: 0

samwu
samwu

Reputation: 5195

This problem occurs because the website doesn't have the Directory Browsing feature enabled. Also, the default document isn't configured. To resolve this problem, use one of the following methods:

Method 1: Enable the Directory Browsing feature in IIS

To resolve this problem, follow these steps:

  1. Start IIS Manager. To do it, select Start, select Run, type inetmgr.exe, and then select OK.
  2. In IIS Manager, expand server name, expand Web sites, and then select the website that you want to change.
  3. In the Features view, double-click Directory Browsing.
  4. In the Actions pane, select Enable.

Method 2: Add a default document

To resolve this problem, follow these steps:

  1. Start IIS Manager. To do it, select Start, select Run, type inetmgr.exe, and then select OK.
  2. In IIS Manager, expand server name, expand Web sites, and then select the website that you want to change.
  3. In the Features view, double-click Default Document.
  4. In the Actions pane, select Enable.
  5. In the File Name box, type the name of the default document, and then select OK.

Upvotes: 1

Related Questions