The Django project under windows is deployed through Apache 2.4 + mod_ wsgi



(please read the following information, which may help you)

During the deployment in recent days, I encountered many problems. I want to record the deployment at that time and help friends in need.
  Here are other questions:

    I've tested using mod before_ The wsgi-cp38 version works with Apache. It is found that Django can be used normally, but there will be unstable data connection problems in use.

  Configuring two Django projects in the same service using Apache (configuring virtual domain names) and using the same virtual environment (the environment location pointed to by mod_wsgi configuration information) will cause data connection problems,

  For example, when starting the Apache service, the database connection of two projects will randomly connect the database of one project, and the other project will use the same data. When logging in to the website, the data will be connected together.

  If a friend solves this problem, please contact me.

Django’s official website explains:
Note: Django official website address

Because environment variables are process scoped, this will not work when you run multiple Django sites in the same process. This is different from mod_ WSGI occurs together.

  To avoid this problem, use mod in your own daemon for each site_ The daemon mode of WSGI, or overriding values in the environment by enforcement in.

  os.environ["DJANGO_SETTINGS_MODULE"] = "mysite.settings"

Operating environment:

First, let’s talk about the environment I use. The version is very important. I’m based on mod_ The version of WSGI requires each version to be downloaded (Python, Apache 2.4)

mod_ WSGI download address:

The following figure illustrates Python and Apache versions:

Version information:

Apache2.4VC14 64bit
  Python 3.5.2-amd64
  Django 2.2 #python 3.5 does not support versions above Django 3.0**
  MySQL 8.0.19

Apache 2.4 installation:

Find the corresponding installation package on the Apache official website

Download Apache 2.4 vc14 at:

Open the httpd.conf file (in the Apache 24 / conf directory)

Define SRVROOT "C:\web\Apache24"
  Listen 80
  DocumentRoot "C:\web\Apache24\htdocs"

I won’t say much about how to install Apache. Here are some common commands:

Httpd - K install - n # install Apache services
        Net start / stop Apache 2.4 # start and stop Apache services
        SC delete Apache 2.4 # deleting Apache services
        Httpd - K uninstall # uninstall Apache

Python-3.5 installation:
This article is brief
Mysql-8.0.19 installation:
This paper is brief; However, the MySQL library needs to be configured at runtime.

The following describes mod in detail_ WSGI related configuration:

After the above is completed, Apache starts accessing the IP address, and it works! Appears, Start deployment.
Create a virtual environment under the Django project folder:

PIP install virtualenv # download virtualenv package
  virtualenv 1_ Env # new environment 1_ env
  Activate # must CD to \ 1_ Run activate in the env \ scripts directory

First download the downloaded mod_ wsgi-4.7.0+ap24vc14-cp35-cp35m-win_ Copy amd64.whl to the scripts directory

PIP install "C: \ djangomode \ 1_env \ mod_wsgi-4.7.0 + ap24vc14-cp35-cp35m-win_amd64. WHL" # install mod through pip_ wsgi

If the installation fails, check whether there is a problem with the corresponding version you downloaded. If it succeeds, continue to move forward;

mod_ WSGI express module config # view module_ Configuration information of WSGI module

Two pieces of configuration information like this will be obtained after running successfully:

#These two pieces of information are required for the httpd.conf configuration of Apache 24 later
  #Save these two pieces of configuration information
  LoadModule wsgi_module "c:/djangomode/1_env/lib/site-packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd"
  WSGIPythonHome "c:/djangomode/1_env"

Note: you may have seen that other bloggers output three configurations, but it doesn’t matter. We don’t need to set LoadFile “C: / python35 / python35.dll

When running Apache service, it is found that it cannot be started. Check the apache24 / logs / error.log document and find the following errors:

Fatal Python error: Py_Initialize: unable to load the file system codec
  LookupError: no codec search functions registered: can't find encoding

Note; Python cannot find its modules directory, so of course it can not load encodings
Solution: modify the windows environment variable configuration:

Pythonpath = "C: \ python35 \ DLLs; C: \ python35 \ lib; C: \ python35 \ lib \ site packages" # use commas between paths; separate

Note that you may want to attach any other Library paths to pythonpath if required by the development environment, but it is most important to set DLLs, lib, and site packages correctly, that is, the python environment variables configured earlier.

Django project modification:

Here’s the reason. The setting configuration relative path information is moving to Apache and mod_ When WSGI is running, the relative path cannot be accessed and needs to be modified to an absolute path.
When I changed to absolute path, running Apache reported an error

OSError: [Errno 22] Invalid argument: 'C:\\\\Djangomode\\templates\\\0.html'\r

You need to add R “filepath” before the file path

R "filepath": it means to avoid the error caused by \ XX being an escape character, that is, after adding R, the escape character in "" will no longer appear, and the pure file address will be programmed.
  #Only one place is described here, and other relative paths need to be changed into absolute paths and added with R
    'DIRS': [r'C:\Djangomode\templates']

When the project is officially launched, debug must be set to false:

DEBUG = False

The host can be specified. If the element is’ * ‘, it means that all networks in the same LAN can be accessed:


To configure the httpd.con file for Apache 2.4:

DocumentRoot "C: \ djangomode" #djangomode is the root directory of the project

Add the following code at the bottom of the file (the parameters need to be changed according to the actual environment)

##---------------Django project deployment configuration---------------##

  LoadModule wsgi_module "c:/djangomode/1_env/lib/site-packages/mod_wsgi/server/mod_wsgi.cp35-win_amd64.pyd"
  WSGIPythonHome "c:/djangomode/1_env"

  #Specify the "WSGI. Py" configuration file path for the project
  WSGIScriptAlias / "C:\Djangomode\Djangomode\"

  #Specify the Django project root directory
  WSGIPythonPath "C:\Djangomode"
  Require all granted

  #Project static file address
  Alias /static "C:\Djangomode\static"
  AllowOverride None
  Options None
  Require all granted

  #Project upload file root directory
  Alias /uploads "C:\Djangomode\uploads"
  AllowOverride None
  Options None
  Require all granted

After modification, restart the Apache service, and the Apache service properties will be modified, as shown in the following figure:

Enter the IP address port or host domain name on the browser to access the Django project, and configure the host domain name servername in httpd.conf
C: \ windows \ system32 \ drivers \ etc domain name needs to be added, such as: Djangomode

Check whether the IP address or domain name is normal. Enter Ping in the CMD window, such as:

Ping #ping is normal

Access succeeded! Congratulations, yeah!
Later, I will write an article about Django using asgi to realize websocket connection.
Welcome to leave a message

Recommended Today

A detailed explanation of the differences between Perl and strawberry Perl and ActivePerl

Perl is the abbreviation of practical extraction and report language “practical report extraction language”. Application of activestateperl and strawberry PERL on Windows platformcompiler。 Perl   The relationship between the latter two is that C language and Linux system have their own GCC. The biggest difference between activestate Perl and strawberry Perl is that strawberry Perl […]