Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deploy django Web application in windows IIS using AWS instance?

I'm currently working on the application which will call services to perform backend operations and I want to deploy those API'S in AWS instance using windows IIS. Can someone explain me the steps to deploy the same?

like image 460
Yash Chandani Avatar asked Nov 01 '25 06:11

Yash Chandani


2 Answers

I'm not sure what the purpose of your application is (and thus the best way to deploy), but there are fairly easy steps to deploying a Django project on a Windows IIS Server.

Here is a guide Deploy Django on Windows using Microsoft IIS and the code to follow along GitHub/webproject

Additionally, here's an article by Microsoft on how to deploy a Python project Configure Python web apps for IIS. The article guides you through steps on how to deploy Django projects with IIS, and how to configure your project's web.config files. The article is not fully detailed, so that's why I include my own tutorial above which provides step-by-step process on deploying a Django project on Microsoft IIS.

Setting up Django with Microsoft IIS

Depending on your project, you may have specific needs, but the general workflow to set up Django with IIS are the following

  1. Install IIS and enable CGI

  2. Place your project in the following directory C:/inetpub/wwwroot/ as well as the web.config file. The web.config file CANNOT sit in the Django project directory as IIS will not find it.

  3. Enable wfastcgi and copy its Python path in your web.config file (under the handler section).

  4. Add necessary changes to your settings.py file and make sure ALLOWED_HOSTS is updated.

  5. Unlock IIS handlers

  6. Navigate to localhost and test your project.

Again, there's detail missing in these steps, but hopefully the links above provide the necessary to get your project up and running.

like image 134
Jon Avatar answered Nov 03 '25 21:11

Jon


How to Deploy Django Web Application on Windows using Microsoft IIS Server

Deploy Django on Windows using Microsoft IIS

Step 1: Paste the Project File to C:\inetpub\wwwroot or C:\ Directory or Any other Directory.

Step 2: Install Python in C:\Python because path limit user friendly – Appropriate Version and set path environment variable.

Step 3: Install Microsoft C++ Build Tools https://visualstudio.microsoft.com/visual-cpp-build-tools/.

Install_Mirosoft C++ Buid Tools

Install_Mirosoft _C++_Buid_Tool

Step 4: If Microsoft IIS is not Installed, follow these steps : Go to Control Panel -> Program and Features -> Turn Windows Features On or Off -> Select Internet Information Services ( IIS ) -> Select All services as per your project, Check Application Development features all enabled !!! -> Confirm and Continue.

Turn_on_IIS_Feature_in_Windows

Turn_on_IIS_Feature_in_Windows

Step 5: Open Microsoft IIS Application – Right Click -> Add Website… -> Enter the Site Name , Select the Physical project path location i.e select manage.py designation folder and enter the binding information.

Step 6: Yes, you successfully created IIS Application Site.

Step 7: Now, you should give access for your project folder and python folder.

Step 8: Select your project folder and right click -> Select Properties -> Security -> Edit Group or user names -> Add -> Select All Object Types, Select the Machine, and last, Enter the object names -> IIS AppPool\your IIS Site name.

e.g. IIS AppPool\DjangoWebApplication and check the names in objects if its correct information it get the Application Object Name e.g. DjangoWebApplication then, then select the object name and select full control to the application.. and use the same procedure on python folder also

Step 9: Now, Is ready for install python libraries., Install all project requirement libraries with also install, pip install openpyxl, wfastcgi.

Step 10: Open a CMD as Administrator and enter the command wfastcgi-enable.

CMD Code

CMD_Code

Step 11: Then, Check on Microsoft IIS -> IIS -> Click FastCGI Settings -> If it is Successfully Configured the Details is found else not found we enter by manually Click Add Application In Name, paste the python executable path location.

e.g. c:\python\python.exe , In Argument paste the wfastcgi.py location.. if you don’t know copy from cmd e.g. c:\python\lib\site-packages\wfastcgi.py and remaining all same okay and continue

FastCGI Settings

FastCGI_Settings

Step 12: Create a file web.config nearby manage.py file and enter the details:

  <configuration>
  <system.webServer>
    <handlers>
      <add name="Python FastCGI"
           path="*"
           verb="*"
           modules="FastCgiModule"
           scriptProcessor="C:\Python\python.exe|C:\Python\Lib\site-packages\wfastcgi.py"
           resourceType="Unspecified"
           requireAccess="Script" />
    </handlers>
  </system.webServer>

  <appSettings>
    <!-- Required settings -->
    <add key="WSGI_HANDLER" value="my_app.wsgi_app()" />
    <add key="PYTHONPATH" value="C:\MyApp" />
    <add key="DJANGO_SETTINGS_MODULE" value="my_app.settings" />

    <!-- Optional settings -->
    <add key="WSGI_LOG" value="C:\Logs\my_app.log" />
    <add key="WSGI_RESTART_FILE_REGEX" value=".*((\.py)|(\.config))$" />
    <add key="APPINSIGHTS_INSTRUMENTATIONKEY" value="__instrumentation_key__" />    
    <add key="WSGI_PTVSD_SECRET" value="__secret_code__" />
    <add key="WSGI_PTVSD_ADDRESS" value="ipaddress:port" />
  </appSettings>
</configuration>

From here, you have to change according to your system perspective

First, Change scriptProcessor settings e.g. "c:\python\python.exe|c:\python\lib\site-packages\wfastcgi.py" can now be used

Second, change <add key="WSGI_HANDLER" value=" DjangoWebApplication.wsgi.application" />

Third, change <add key="PYTHONPATH" value="C:\DjangoWebApplication folder" />

Fourth, change <add key="DJANGO_SETTINGS_MODULE" value=" DjangoWebApplication.settings" />

Then, remaining optional.

Step 12: Then, Create another file web.config from static folder and enter the details:

<?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>

Step 13: Then, Create another file web.config from media folder and enter the details:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <clear />
            <add name="StaticFile" path="*" verb="*" type="" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" scriptProcessor="" resourceType="Either" requireAccess="Script" allowPathInfo="true" preCondition="" responseBufferLimit="4194304" />
        </handlers>
        <directoryBrowse showFlags="Date, Time, Size, Extension, LongDate" />
    </system.webServer>
</configuration>

Step 14: 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.

Step 15: Add Virtual Directory. In order to enable serving static files map a static alias to the static directory, C:/inetpub/wwwroot/webproject/static/.

Step 16: Add Virtual Directory. In order to enable serving media files map a media alias to the media directory, C:/inetpub/wwwroot/webproject/media/.

Step 17: Yes, Now is ready for hosting the site before check the settings.py edit the Allowed Host and add the IP Address and port number.

Step 18: Then, generate the staticfiles by Django run the command python manage.py collectstatic.

Step 19: Now, Is All Set… Start the Server and check all is working correct.

See: https://github.com/Johnnyboycurtis/webproject for a clear explanation...

References

  • https://pypi.org/project/wfastcgi/
  • https://learn.microsoft.com/en-us/visualstudio/python/configure-web-apps-for-iis-windows?view=vs-2019
like image 20
Ammy Vijay Avatar answered Nov 03 '25 22:11

Ammy Vijay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!