I stumbled in the same place every time, so make a note for myself.
--Windows Server 2016 is installed. --Apache 2.4 is installed. --Thank you for the article How to embed mod_wsgi in Apache on Python Windows.
Follow How to embed mod_wsgi in Apache on Python Windows.
I added it because it didn't work according to the above article.
After installation, mod_wsgi-express.exe
is created in the Python Scripts directory (C: \ Program Files \ Python38 \ Scripts
in your environment).
The Apache httpd.conf settings are automatically generated by mod_wsgi-express module-config
, so copy and paste them into httpd.conf.
> mod_wsgi-express module-config
LoadFile "c:/program files/python38/python38.dll"
LoadModule wsgi_module "c:/program files/python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd"
WSGIPythonHome "c:/program files/python38"
↓
httpd.conf
…
LoadFile "c:/program files/python38/python38.dll"
LoadModule wsgi_module "c:/program files/python38/lib/site-packages/mod_wsgi/server/mod_wsgi.cp38-win_amd64.pyd"
WSGIPythonHome "c:/program files/python38"
…
Reference: Django 1.4 documentation-How to use Django in Apache and mod_wsgi environment
The first argument of the> WSGIScriptAlias
line is the location where you want to publish your application (/
represents the root URL), and the second argument is the location of the "WSGI file (discussed below)" on your system. WSGI is usually placed inside a project package (mysite in this example). Apache will now process all requests under the root URL using the specified WSGI file.
That is, it means that the request that comes after the first argument is processed by the WSGI file specified by the second argument. For the second argument, specify wsgi.py in the Django project app you want to connect to.
httpd.conf
WSGIScriptAlias / d:/apps/appname/appname/wsgi.py
Reference: mod_wsgi --WSGIScriptAlias
Reference: Django 1.4 documentation-How to use Django in Apache and mod_wsgi environment
The> WSGIPythonPath
line ensures that the project's packages can be used for imports in Python paths.
It means that I will tell you the location of the site-packages used in the project. If you change the Python version and create more venv, you need to change this path as well (Related article: ValueError at set_wakeup_fd only works in main thread workaround / hiro-jp / items / 1a15ac51165f5af395f3)), but usually ʻappname / venv / Lib / site-packages` is fine.
httpd.conf
WSGIPythonPath d:/apps/appname/venv/Lib/site-packages
The> \
In the following, it means to allow access to d: / apps / appname / appname
(Require all granted
).
httpd.conf
<Directory d:/apps/appname/appname>
<Files wsgi.py>
Require all granted
</Files>
</Directory>
** Note **: In Old documentation like Django 1.4 Documentation The description is as follows, but this does not work with Apache 2.4 or later (Reference: Apache 2.4 changed the description method of access restrictions / items / c8eb1fedef3c00c5fbac)).
httpd.conf
# Apache2.2 Previous description method
<Directory d:/apps/appname/appname>
<Files wsgi.py>
Order allow,deny
Allow from all
</Files>
</Directory>
When I try to connect, I get an Internal Server Error here. It can be solved by the following.
wsgi.py
"""
WSGI config for appname project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/3.0/howto/deployment/wsgi/
"""
import os
import sys #← Add
from django.core.wsgi import get_wsgi_application
sys.path.append('D:/apps/appname') #← Add
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'appname.settings')
application = get_wsgi_application()
Related article: How to deal with mod_wsgi 500 Internal Server Error
Reference: Apache2 mod_wsgi, 500 Internal Server Error
Recommended Posts