How to Deploy Django on IIS Server Windows 11 in?2024

How to Deploy Django on IIS Server Windows 11 in?2024

Python Initialization:

  • Install the Python package required by the project. In this case, Python 3.12 is being used. Navigate to the Python website and download the installer for Windows (64 bit).


  • Run the installer and choose a custom path for installation at C:\

Note: Installing Python directly at C:\ is for ease of access purposes. You can install it at any other location as well.

  • Check the options if not already checked:
  • Precompile standard Python libraries.
  • Add Python to environmental variables.
  • Click Install.

IIS Server Initialization:

  • Press Start and search for “Turn Windows features on or off.”
  • After opening, find “Internet Information Services.”
  • Click on it and expand the directory to find:
  • Select “Web Management Tools.”
  • Select and expand “World Wide Web Applications.”
  • Select and expand “Application Development Services.”
  • Now select “CGI”.
  • Click OK, and it will install all services.

Project Initialization:

  • Place your project under:

C:/inetpub/wwwroot/        

  • Before installing dependencies inside the project, you need to install one package globally:

pip install wfastcgi        

Create an environment in your project and install all the dependencies from requirements.txt

cd C:\inetpub\wwwroot<your project> 
python -m venv myenv 
.\myenv\Scripts\activate 
pip install -r requirements.txt        

  • Enable wfastcgi. Open a CMD terminal as Administrator and run the command:

C:\wfastcgi-enable        
Applied configuration changes to section “system.webServer/fastCgi” for “MACHINE/WEBROOT/APPHOST” at configuration commit path “MACHINE/WEBROOT/APPHOST” C:\inetpub\wwwroot\cras\myenv\Scripts\python.exe|C:\inetpub\wwwroot\cras\myenv\Lib\site-packages\wfastcgi.py can now be used as a FastCGI script processor

  • Take these paths and place them in the following web.config:

C:\inetpub\wwwroot\cras\myenv\Scripts\python.exe|
C:\inetpub\wwwroot\cras\myenv\Lib\site-packages\wfastcgi.py        

Create a file named web.config at C:/inetpub/wwwroot/<your project> and add the following content to it:

<?xml version="1.0" encoding="utf-8"?> 
<configuration> 
<system.webServer> 
<handlers> 
<add name="Python FastCGI" path="*" verb="*" modules="FastCgiModule" 
scriptProcessor="C:\inetpub\wwwroot\<your project>\myenv\Scripts\python.exe|C:\inetpub\wwwroot\<your project>\myenv\Lib\site-packages\wfastcgi.py" 
resourceType="Unspecified" requireAccess="Script" /> 
</handlers> 
</system.webServer> 
<appSettings> 
<add key="PYTHONPATH" value="C:\inetpub\wwwroot\<your project>" /> 
<add key="WSGI_HANDLER" value="<your project>.wsgi.application" /> 
<add key="DJANGO_SETTINGS_MODULE" value="<your project>.settings" /> 
</appSettings> 
</configuration>        

  • Edit WSGI_HANDLER (located in your wsgi.py)
  • Edit DJANGO_SETTINGS_MODULE (your settings.py module)

Create a file named web.config at C:/inetpub/wwwroot/<your project>/static and add the following content to it:

<?xml version="1.0" encoding="UTF-8"?> 
<configuration> 
<system.webServer> 
<handlers> 
<clear /> 
<add name="StaticFile" path="*" verb="*" modules="StaticFileModule" 
resourceType="File" requireAccess="Read" /> 
</handlers> 
</system.webServer> 
</configuration>        

  • Open Internet Information Services (IIS) Manager. Under connections, select the server, then in the center pane under Management select Configuration Editor. Under Section select system.webServer/handlers. Under Section select Unlock Section. This is required because the C:/inetpub/wwwroot/web.config creates a route handler for our project.
  • On the Server inside Feature, select FastCGI Settings.
  • Click on the previously created Application.
  • Double click on it and change its Max Instances to your desired number.
  • Right-click on Sites and select Add a website.
  • Give it a name and path to your project root folder.
  • Physical Path: C:\inetpub\wwwroot<your project>\
  • Click OK.

Allowing IIS permission to access the?project:

  • After creating the website inside IIS, select your website.
  • On the right-hand side under Actions, select Edit Permissions.
  • Select the Security tab and click Edit.
  • Click add to add a new user/group/object for permission.
  • Add IIS AppPool\<your application pool name> inside the box and give full control to your project folder.
  • Click OK.

Adding Environmental Variables inside IIS Server:

  • Select your website.
  • Under the Features section, select Configuration Editor. Under Section select appSettings.
  • Click on Collection and on the right side, click “Edit Items.”
  • Now add your appSetting variables, i.e., WSGI_HANDLER and others if you have any.
  • Click OK and on the right-hand side, click Apply.

Adding a virtual directory in the IIS?server:

  • Navigate to your project and run the following command to collect all the static files.

python manage.py collectstatic        

  • Write the Alias: static.
  • Give the Physical Path to the directory created by the above command.

Restart your website and search for localhost on the web?browser.


Abhishek Agrawal

AI/ML Engineer | Data Science & Python Developer | Transforming Data into Intelligence

5 个月

Hello sir I’m experiencing an issue while deploying my Flask application on IIS. The application runs properly on the server itself, but it times out when I try to access it from a local system connected through the intranet. Could you please provide guidance on how to resolve this issue? Thank you!

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了