Novice Xiaobai uses IIS to deploy the Django framework

        It is the first time for Xiaobai to deploy the Django framework with IIS on Windows, follow the tutorial below step by step. What if the deployment is still not good? Brother, I know you are in a hurry, but don't worry, just listen to me:

        The first is to check, then check, then check, and then check. All in all a must check.

        In the end, it must be checked, or it is not possible to turn off the computer and go to sleep before checking.

Ok, let's serve.

  • Installation and deployment item check

Django provides an automatic check configuration command (python manage.py check --deploy) for the project's security configuration, which will generate a configuration report after execution. By modifying the configuration, the project can mainly include: (the highlighted part is the key point, otherwise an error will be reported)

  1. key configuration items

DEBUG: Set it to True in the production environment, which can protect sensitive information related to the configuration type from being leaked. If there is an error in website deployment, set it to False, which is convenient for debugging

  1. Specific environment configuration items

· ALLOWED_HOSTS : It is recommended not to use '*' in production deployment, otherwise it will increase the probability of encountering CSRF attacks (this time using intranet deployment, there will not be too much risk, just change it to '*') . The domain names allowed to be accessed can be specified in ALLOWED_HOSTS. (If you use LAN deployment, it is the IP address of the server)

· DATABASE: The parameters in the configuration list are set to the parameters in the official operating environment. (modified according to the database documentation)

STATICFILES_DIRS and STATIC_URL: Before the official deployment, the static file path in the specified production environment must be used.

STATIC_URL =  ‘/static/’

STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]

· MEDIA_ROOT and MEDIA_URL: Specify the fixed subdirectory for uploading media files to prevent them from being interpreted and executed by the web server

MEDIA_ROOT = os.path.join(BASE_DIR,‘media’) .replace(‘\\’,’/’)
MEDIA_URL = ‘/media/’

  1. Cookie security configuration items

SESSION_COOKIE_SECURE=True  #Enable HTTPS to transmit cookie information to improve information security.

CSRT_COOKIE_SECURE=True   #Enable CSRF anti-attack cookie transmission method

  1. Performance optimization configuration items

CONN_MAX_AGE: Mainly to provide online multiplexing function for database links when large concurrent visits to the website, avoiding frequently establishing links directly with the database

TEMPLATES: When using templates, the compiled code will be rendered first, and cache-enabled templates do not need to be recompiled, which usually greatly improves performance.

In actual use, the above methods for improving performance need to be judged by actual operation results.

  • Install IIS

1. Select "Control Panel" -> "Programs" -> "Turn Windows Features on or off"

2. Select "Internet Information Services" -> "World Wide Web Services" -> "Application Development Functions" in "Enable or Disable Windows Functions", set "ASP", "CGI", "ISAP Extensions", "ISAP Filters" " and other options are ticked, as shown below

3. Select OK and wait patiently for a while.

4. After the IIS installation is complete, search for IIS in the Windows system, and you can find the IIS startup main page

  • Configure the Web site

At this point IIS, Python are installed. The Web site configuration is as follows:

  1. Install the wfastcgi library in the command prompt to realize the connection between Python and IIS. The command is as follows:

Pip install wfastcgi

You can see the installed wfastcgi library in the Python installation directory (such as ".\Python\Lib\site-packages").

  1. Configure the project site in IIS

Open the list on the left, select "Sites", and click "Add Site"

Then, set the website name, physical path, port, and IP address 4 parameters in the pop-up interface

Among them, the physical path is the path where the project is located, and the original port number is 80, which is changed to 8000 (the port number of each deployment of the website cannot be the same)

  1. After the website is successfully added, you can see the newly added website list item in IIS, click on the website, select "Handler Mapping", and click "Add Module Mapping..."

  1. Set the following parameters in the "Add Module Mapping" module:

Request path: *

Module: FastCgiModule

Executable file: open cmd, enter wfastcgi-enable or wfastcgi-disable to get, (such as: "D:\Python\python.exe|D:\Python\Lib\site-packages\wfastcgi.py")

name: random

Finally, click on "Request Limits" and untick the checkbox

  1. After the module is added successfully, you can see the "FastCGI Settings" icon on the IIS main page, click to enter

  1. Select and click the executable file just now

  1. Find "Environment Variables" and click the icon next to "Collections"

  1. Click "Add" to add three environment variables, as follows:

name:WSGI_HANDLER

 value:django.core.wsgi.get_wsgi_application()

 name:PYTHONPATH

 value: D:\undergraduate\junior student\internship\test3.3\DataManageSystem (replace with your own project path)

 name:DJANGO_SETTINGS_MODULE

 value: DataManageSystem.settings (DataManageSystem is replaced by your own project name)

  1. Visit the website, click the browse website in the list on the right side of your own website, you can browse

Four. IIS deployment website displays static files

  1. Right-click on your website and select Add Virtual Directory

  1. Alias: static

Physical path: the path where static is located in the project

  1. Create a new web.config file in the static directory

Purpose: The web.config file is an XML text file used to store configuration information for ASP.NET Web applications. When you create a new web application through .NET, a default Web.config file will be automatically created in the root directory by default, including the default configuration settings, and all subdirectories will inherit its configuration information/If you want to modify The configuration settings of the subdirectory, you can create a new web.config file in this subdirectory. It can provide configuration information other than that inherited from the parent directory, and it can override or modify settings defined in the parent directory.

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

  • If the following error occurs when accessing the website:

This happens because versions after IIS7 have adopted a more secure web.config management mechanism, which locks configuration items by default and does not allow changes. We unlock it and it's OK.

Open cmd and enter the following two commands in sequence

%windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/handlers

%windir%\system32\inetsrv\appcmd unlock config -section:system.webServer/modules

Guess you like

Origin blog.csdn.net/h_u_m_a_n/article/details/128377201