Django Windows + IIS + wfastcgi environment at deployment

Tutorial is based on  Windows 10 Pro + Python3.6 + IIS + wfastcgi  on deployment Django2.2, the same applies to the Windows7 Windows server2012 server and the Windows operating system and above.

Preparing the Environment

1.Python and virtual environments

Python Django depends on the interpreter environment. Installation steps are omitted. Refer https://www.cnblogs.com/wcwnina/p/9069783.html (according to the actual situation may not install a virtual environment).

2. IIS server installation (including CGI)

Open the [Control Panel] -> [Programs and Features], opening on the left side [] enable or disable Windows features, enable IIS, be sure to include CGI.

Enter localhost address in a browser to access the IIS test, if the following screen appears, indicating successful installation.

3. Copy the project file, dependent libraries

First of all, the deployment production environment To turn off debug mode, modify the settings file:

= DEBUG False # turn off debugging 

allowed_hosts = [ ' 127.0.0.1 ' ] # Specifies the IP host can access the site

It recommended that the server create a new folder, upload files and virtual project environment in the same folder. Enter the virtual environment, the bulk install all dependent libraries:

pip install -r requirements.txt

Note: the premise of the above command is executed with the command "pip freeze> requirements.txt" to export project environment depend on library information to requirements.txt file.

如果使用的是Mysql数据库,请自行安装Mysql和导入数据,这里就不说明。

4.安装wfastcgi应用服务器

进入虚拟环境,pip安装之,如图。

启动wfastcgi:

wfastcgi-enable

如上图,启动成功之后,它会把Python路径和wfastcgi的路径显示出来,我们需要把这个路径复制出来,保存好,后边用得着!

e:\djblog\vpy_myblog\scripts\python.exe|e:\djblog\vpy_myblog\lib\site-packages\wfastcgi.py

注意:上面的路径,是由Python解释器的路径和“|”以及“wfastcgi.py”文件路径组成。

配置IIS服务器

通过【控制面板】->【管理工具】打开IIS管理器。

1.添加网站

2.web配置文件

在项目根目录下新建"web.config"配置文件,复制粘贴以下内容。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
            <add name="Python FastCGI" 
                 path="*" 
                 verb="*" 
                 modules="FastCgiModule" 
                 scriptProcessor="<Path to Python>\python.exe|<Path to Python>\lib\site-packages\wfastcgi.py" 
                 resourceType="Unspecified" 
                 requireAccess="Script"/>
        </handlers>
    </system.webServer>
    <appSettings>
        <add key="WSGI_HANDLER" value="django.core.wsgi.get_wsgi_application()" />
        <add key="PYTHONPATH" value="<Path to Django App>" />
        <add key="DJANGO_SETTINGS_MODULE" value="<Django App>.settings" />
    </appSettings>
</configuration>

修改配置文件:

3.添加静态文件虚拟目录

右键添加虚拟目录,如图。

添加虚拟目录时,别名与你的settings里设置的一致,比如"static",物理路径就是静态资源的实际目录。

在static目录下新建一个"web.config"文件,然后写入下面的内容,保存即可无须修改。

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

So far basically completed the deployment. Restart IIS, enter http://127.0.0.1 in the browser, you can access the site.

common problem

1. If there are mistakes IIS access

This occurs because later versions of IIS7 have adopted safer web.config management mechanism, by default locked configuration items are not allowed to change. We put it unlocked on OK.

Open CMD, the following two commands are sequentially input in which:

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

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

2. Sometimes access the page, or visit the website background alone 400 errors

This time the reason may be because the site did not give permission. We open the IIS, to find the site, right, permission to edit, add, modify and write permissions to the IIS user. You will be able to access normal.

3. static files show abnormalities

Add STATIC_ROOT configured in settings.py, the designated collection static file path, such as:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

Enter the virtual environment, enter the following command to collect static files:

python manage.py collectstatic

After the collection is complete, refresh the page to display properly.

 

 At this point, please indicate the source.

Guess you like

Origin www.cnblogs.com/wcwnina/p/10960242.html