[PYTHON] Django + Apache with mod_wsgi on Windows Server 2016

background

I stumbled in the same place every time, so make a note for myself.

environment

Premise

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

Download-install

Follow How to embed mod_wsgi in Apache on Python Windows.

Incorporate mod_wsgi into Apache

I added it because it didn't work according to the above article.

1. Module load + α

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"
…

2. WSGIScriptAlias settings

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

3. WSGIPythonPath settings

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

4. Directory settings

The> \ part ensures that Apache can access the wsgi.py file.

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>

Django wsgi.py settings

When I try to connect, I get an Internal Server Error here. ss19.png 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

Django + Apache with mod_wsgi on Windows Server 2016
Run django applications on Windows + Apache + mod_wsgi + services.
Apache + Django + Shibboleth on Windows Server 2016 Related Articles Summary
CentOS 6.4 with Python 2.7.3 with Apache with mod_wsgi and Django
How to embed mod_wsgi into Apache on Python Windows
Python3 + Django ~ Mac ~ with Apache
Publish your Django app on Amazon Linux + Apache + mod_wsgi
Django environment development on Windows 10
Django --Apache mod_wsgi virtualhost deployment
How to run Django on IIS on a Windows server
Blogging with Pelican on Windows
Set up a web server with CentOS7 + Anaconda + Django + Apache
Until the start of the django tutorial with pycharm on Windows
Set multiple WSGIPythonPath with Apache + mod_wsgi
Run SwitchBot on Windows 10 with Bleak
Run python3 Django1.9 with mod_wsgi (deploy)
Use Tensorflow 2.1.0 with Anaconda on Windows 10!
Getting started with USD on Windows
Getting started with Python 3.8 on Windows
Create an environment for Django x Apache x mod_wsgi with Vagrant (Ubuntu 16.04)
Run servo with Python on ESP32 (Windows)
Handle multiple Django projects with Apache (WSGIDaemonProcess)
CentOS8 + Apache2.4 + pyenv + mod_wsgi + Django project deployment
A note on enabling PostgreSQL with Django
Two things I was addicted to building Django + Apache + Nginx on Windows
Rock-paper-scissors with Python Let's run on a Windows local server for beginners
Project cannot be created with Python3.5 (Windows) + django1.7.1
With Django + Google Cloud Strage, ImageField images are displayed normally on the server
Install Windows 10 from a Linux server with PXE
Drawing tips with matplotlib on the server side
Internationalization with django
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Enable Jupyter Notebook with conda on remote server
Web server construction with Apache 2.4 (httpd 2.4.43) + PHP 7.4 on Linux ―― 4. Security (chown and firewalld)
Let's integrate Django and apache (httpd) on Mac! !!
Organize files on Windows with Linux commands-using WSL-
Boot CentOS 8 from Windows 10 with Wake On LAN
CRUD with Django
Reverse proxy with Apache 2.4 on Ubuntu 19.10 Eoan Ermine
What Python beginners got hooked on with Django
Mount a directory on another server with sshfs
Use Python / Django with Windows Azure Cloud Service!
Run Kali Linux on Windows with GUI (without VirtualBox)
Problems with windows python being called on pipenv on WSL
Looking back on creating a web service with Django 1
Build Apache HTTP Server and Wildfly on Oracle Linux 8
Looking back on creating a web service with Django 2
PIL with Python on Windows 8 (for Google App Engine)
Create an OpenAI Gym environment with bash on Windows 10
Deploy a Django app made with PTVS on Azure
Launch Django on a Docker container with docker-compose up
Install django on python + anaconda and start the server
How to operate Firefox with selenium on Windows Memo
Authenticate Google with Django
Tftp server with Docker
Django 1.11 started with Python3.6
Upload files with Django
Development digest with Django
Output PDF with Django
Markdown output with Django
Pylint on Windows Atom