How to Host Django on IIS

 Install IIS on your VM or machine, and enable Common Gateway Interface CGI

How to Install IIS on Windows 10 or Windows 11


To Install IIS on Your Machine Check Internet Connection First and Open Command Promote

Copy and Paste the Following Code Give Below 

START /WAIT DISM /Online /Enable-Feature /FeatureName:IIS-ApplicationDevelopment /FeatureName:IIS-ASP /FeatureName:IIS-ASPNET /FeatureName:IIS-BasicAuthentication /FeatureName:IIS-CGI /FeatureName:IIS-ClientCertificateMappingAuthentication /FeatureName:IIS-CommonHttpFeatures /FeatureName:IIS-CustomLogging /FeatureName:IIS-DefaultDocument /FeatureName:IIS-DigestAuthentication /FeatureName:IIS-DirectoryBrowsing /FeatureName:IIS-FTPExtensibility /FeatureName:IIS-FTPServer /FeatureName:IIS-FTPSvc /FeatureName:IIS-HealthAndDiagnostics /FeatureName:IIS-HostableWebCore /FeatureName:IIS-HttpCompressionDynamic /FeatureName:IIS-HttpCompressionStatic /FeatureName:IIS-HttpErrors /FeatureName:IIS-HttpLogging /FeatureName:IIS-HttpRedirect /FeatureName:IIS-HttpTracing /FeatureName:IIS-IIS6ManagementCompatibility /FeatureName:IIS-IISCertificateMappingAuthentication /FeatureName:IIS-IPSecurity /FeatureName:IIS-ISAPIExtensions /FeatureName:IIS-ISAPIFilter /FeatureName:IIS-LegacyScripts /FeatureName:IIS-LegacySnapIn /FeatureName:IIS-LoggingLibraries /FeatureName:IIS-ManagementConsole /FeatureName:IIS-ManagementScriptingTools /FeatureName:IIS-ManagementService /FeatureName:IIS-Metabase /FeatureName:IIS-NetFxExtensibility /FeatureName:IIS-ODBCLogging /FeatureName:IIS-Performance /FeatureName:IIS-RequestFiltering /FeatureName:IIS-RequestMonitor /FeatureName:IIS-Security /FeatureName:IIS-ServerSideIncludes /FeatureName:IIS-StaticContent /FeatureName:IIS-URLAuthorization /FeatureName:IIS-WebDAV /FeatureName:IIS-WebServer /FeatureName:IIS-WebServerManagementTools /FeatureName:IIS-WebServerRole /FeatureName:IIS-WindowsAuthentication /FeatureName:IIS-WMICompatibility /FeatureName:WAS-ConfigurationAPI /FeatureName:WAS-NetFxEnvironment /FeatureName:WAS-ProcessModel /FeatureName:WAS-WindowsActivationService




Copy Django Project to C:/inetpub/wwwroot/yourproject


Install Python 3.++ in C:/Python39, and install the necessary libraries Django, openpyxl, wfastcgi; see yourproject/install_requirements.bat


Navigate to C:/, right-click on Python39, and edit Properties. Under Security, add IIS AppPool\DefaultAppPool. DefaultAppPool is the default app pool.


Enable wfastcgi


Open a CMD terminal as Administrator, and run the command wfastcgi-enable.


Copy the Python path, and replace the scriptProcessor="<to be filled in>" in web-config-template with the Python path returned by wfastcgi-enable.


Edit the remaining settings in web-config-template then save it as web.config in the C:/inetpub/wwwroot/ directory. It should NOT sit inside yourproject/. Other settings can be modified if yourproject does NOT sit at C:/inetpub/wwwroot/


Edit project PYTHONPATH (path to your project)


Edit WSGI_HANDLER (located in your wsgi.py)


Edit DJANGO_SETTINGS_MODULE (your settings.py module)


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.


Add These Line in Settings.py in your python Project

STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / "static"
python manage.py collectstatic

To Enable Static files on IIS:

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


Refresh the server and navigate to localhost

You have started a django project on IIS and it is working perfectly on your localhost. Now, when deploying it to web-server, the static files (CSS, JS, images,..) are not fetched. You can read here and here , but in fact, you don't want all these configurations and copying files from one directory to another...

What you want is that your IIS server will fetch the static files from the static/ directory, just as the development server (the one you run with python manage.py runserver) did. enter image description here

Solution

1) inside the static/ directory, create a new file called web.config (notice, you probably have another web.config file in upper directory - don;t touch it. just leave it as is). enter image description here

2) Write the following as the content of this file:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <!-- this configuration overrides the FastCGI handler to let IIS serve the static files -->
    <handlers>
    <clear/>
      <add name="StaticFile" path="*" verb="*" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
    </handlers>
  </system.webServer>
</configuration>

3) Go to your IIS server -> right click the site name -> Add virtual directory enter image description here

4) in alias wrote "static" (This is a MUST). Then, navigate to the folder in the project where all the static files are

enter image description here

5) run IIS server and enter your website. It should work.





Referred Github Page












Post a Comment

Previous Post Next Post