Django on Windows Server 2019/ 2022 Base AWS
Environment where I performed the testing
Windows Server 2019 and Windows Server 2022 Base in AWS
Launch an EC2 windows instance.
Access the server using RDP.
Go to Server Manager and Add Roles
Proceed based by clicking Next button.
On the service page install IIS under web server section
On the next page under Application deployment select CGI
Complete the installation.
Now install the required python version from the website https://www.python.org/
Make sure to select the box saying all user and add to environment path (Manadatory selection)
Confirm you can access the python by opening command prompt and type the command
C:\>python Python 3.12.1 (tags/v3.12.1:2305ca5, Dec 7 2023, 22:03:25) [MSC v.1937 64 bit (AMD64)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> If the above command is working, then python installation is fine and it is added to the paths.
Create Virtual Environment
python -m venv myvenv
Activate venv
myvenv\Scripts\activate
upgrade pip
python -m pip install --upgrade pip
pip install Django
pip install wfastcgi
djang-admin startproject myapp
cd myapp
python manage.py runserver
(venv) C:\>django-admin startproject myapp
(venv) C:\>cd myapp
(venv) C:\myapp>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
System check identified no issues (0 silenced).
You have 18 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
January 31, 2024 - 11:59:00
Django version 5.0.1, using settings 'myapp.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.
By this step a default django welcome page will be available at localhost:8000 in your server browser, not outside the server for that you need to do further steps.
Now our part with environment and django, python step is completed. Now comes the main part of passing the request (http) coming to the website in the server to this python application.
Go to IIS console.
Click on the server name at right side you can see FastCGI Settings.
At right side column click on the Add application.
AS per the file path and arguments column can filled based on your virtualenv path to python and wfastcgi.
Under FastCGI properties: click on the button against Environment Variables and add the following key and value. Here Key willl be Name and value...
Name: DJANGO_SETTINGS_MODULE
Value: myapp.settings
Name: PYTHONPATH
Value: C:\myapp --------> Application Root path
Name: WSGI_HANDLER
Value: django.core.wsgi.get_wsgi_application()
After this step FastCGI setup is complete.
Now we need to create a site and forward the request coming to the site using a setting called Handler Mappings
Add new site
Add sitename and Physical Path as per the requirement.
Click ok and double click on the new site and in the box you can see Handler Mappings
Click on Handler Mappings > At the right side click Add Module Mappings.
In Rquest path Enter astrix *
In the Executable add the details like below
C:\venv\Scripts\python.exe|C:\venv\Lib\site-packages\wfastcgi.py
Make sure to select the Module FastCGI .
Click Request Restriction un-check the box Invoke handler only if the request is mapped to the
IIS part also completed.
Now go to windows firewall and add In-bound Rule for port 8080.
Now the call website using server IP or domain name if you against the port 8080
Chances are you will get the above error. To resolve the same go to settings.py under your application and add Django host host as a wildcard (dev only not recommended for prod)
Save the file and call the website.
TaDA...
Thank you