September 7, 2020

Host Django site in IIS

Django is a free, open source web framework written in the Python language.

A web framework is a software framework that provide a standard way to build web applications. It allows web developers to focus on developing the business requirements rather than reinventing the wheel every time for standard, secure web applications.

In this post we will create a django project and host it in IIS.

It is best practice to provide a dedicated environment for each Django project you create, it allows you to run different versions of django for different applications on the same machine. There are many options to manage environments within the Python ecosystem, we will use the Python's default package venv for managing environments.

Create django project

Following are the steps you need to create a django project.

  • To create a virtual environment for your project, open a new command prompt, navigate to the folder where you want to create your project and then run the following command:

    	python -m venv myproject_env
    

    In this exmaple, I have executed this command at c:\projects. Above command will create a virtual environment and you will see a new folder named myproject_env at the path where you executed this command.

  • Next, we need to activate the environment, run this command from the command prompt(from same path).

    	.\myproject_env\Scripts\activate
    

    We have setup our virtual envrionemnt.

  • Lets create a new directory named src.

    	mkdir src 
    

    We have src folder at c:\projects.

  • Move to src folder

    	cd src
    
  • and install django by using this command.

        pip install Django
    

    This will install the latest release of django.

  • Create a new django project (from command prompt at src folder),

    	django-admin startproject myproject
    
  • Execute manage.py runserver command, it will start the django site with development server at http://127.0.0.1:8000/

    	python manage.py runserver
    
  • Enter this url in browser and you will see django's default page rendered successfully.

Install and enable wfastcgi

wfastcgi:

wfastcgi.py provides a bridge between IIS and Python using WSGI and FastCGI. It can be used with any Python web application or framework that supports WSGI.

  • Run the following command to install wfastcgi

    	pip install wfastcgi
    
  • After installing wfastcgi, we need to enable it using:

    	wfastcgi-enable
    

    Note that, you need to open command prompt will elevated privilege.

    If the above command is executed successfully, it will display the output similar to this:

    Applied configuration changes to section "system.webServer/fastCgi" 
        for "MACHINE/WEBROOT/APPHOST" 
        at configuration commit path "MACHINE/WEBROOT/APPHOST"
    

Host django site in IIS

We have a working django site. Following are the steps we need to configure to make django work under IIS.

  • Copy the project's folder(the folder which contains the actuall source code, in our case myproject folder located at c:\projects\src) in wwwroot.

  • Create a new website in IIS with name myproject.

    In our exmaple we are using port 82 for new website.

  • Open Hanlder Mappings section.

  • Click on Add Module Mapping link on the right side:

  • In Add Module Mapping dialog box, enter * for Request Path.

  • From Module dropdown, select FastCgiModule.

  • In Executable textbox, enter the following path to python.exe and wfastcgi.py separated by | sign. In our case the path is:

    "c:\projects\myproject_env\scripts\python.exe
        |
        c:\projects\myproject_env\lib\site-packages\wfastcgi.py" 
    	

    You need to change the path as per your environment.

  • Enter any desired name in Name field, in our case, it is Django Hanlder.

  • Click the Request Restrictions button, it will open the following dialog.

  • Uncheck the checkbox Invoke handler only if request is mapped to:

  • Then click OK on both dialog boxes.

  • It will ask you to create a FastCGI application.

    Click Yes.

  • It creates a web.config file in the project's root folder which we have copied in wwwroot(C:\inetpub\wwwroot\myproject). Open the web.config file and update the content as below:

    	<?xml version="1.0" encoding="UTF-8"?>
    	<configuration>
    		<system.webServer>
    			<handlers>
    			<add 
                    	name="Django Handler" 
                    	path="*" verb="*" 
                    	modules="FastCgiModule" 
                    	scriptProcessor="c:\projects\myproject_env\scripts\python.exe
                                     |
                                     c:\projects\myproject_env\lib\site-packages\wfastcgi.py" 
                    	resourceType="Unspecified" 
                    	/>
    			</handlers>
    		</system.webServer>
    		<appSettings>
    		  <add key="WSGI_HANDLER" value="myproject.wsgi.application" />
    		  <add key="PYTHONPATH" value="C:\inetpub\wwwroot\myproject" />
    		  <add key="DJANGO_SETTINGS_MODULE" value="myproject.settings" />
    		</appSettings> 
    	</configuration>
    
    

    Make sure to change the project name and path as per your settings.

That's it. We have setup the django site in IIS. Navigate the follwoing link in browser:

	http://localhost:82/

You can change the port number if you have configured a different one when creating website in IIS.

And you will see the django default page.

If you get any error, check if you have given the proper rights on the project folder to DefaultAppPool.

No comments:

Post a Comment