我在 Docker 容器中运行 Apache2,并希望使用
mod_wsgi
托管我的 Django 站点。无论如何,WSGI 进程无法启动,并且出现以下错误。我尝试使用容器和单独虚拟环境的 Python 路径,但出现相同的错误。我做错了什么?
[Thu Jan 11 10:39:44.447690 2024] [wsgi:warn] [pid 316:tid 140618355098752] (2)No such file or directory: mod_wsgi (pid=316): Unable to stat Python home /var/www/html/my-site.com.com/python3.7. Python interpreter may not be able to be initialized correctly. Verify the supplied path and access permissions for whole of the path.
Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'
这是我的
Dockerfile
:
FROM python:3.7.17-buster
WORKDIR /var/www/html/my-site.com
SHELL ["/bin/bash", "-c"]
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
ENV VIRTUAL_ENV=/var/www/html/my-site.com/python3.7
ENV PATH="$VIRTUAL_ENV/bin:$PATH"
COPY ./dev .
COPY ./requirements.txt .
RUN apt update
RUN apt upgrade -y
RUN apt install apache2 libapache2-mod-wsgi-py3 -y
RUN a2enmod ssl rewrite wsgi headers macro
RUN python -m venv $VIRTUAL_ENV
RUN source $VIRTUAL_ENV/bin/activate
RUN pip install --upgrade pip && \
pip install --upgrade setuptools && \
pip install -r requirements.txt
这是相关的 Apache 配置段:
WSGIScriptAlias / /var/www/html/my-site.com/main/wsgi.py
WSGIDaemonProcess prod_$site python-home=/var/www/html/my-site.com/python3.7 python-path=/var/www/html/my-site.com/python3.7/site-packages
WSGIProcessGroup prod_$site
好吧,我想通了。我通过删除虚拟环境应用了评论中的建议。另外,我更新了
WSGIDaemonProcess
指令以使用容器的 Python 路径。
Dockerfile
FROM python:3.7.17-buster
WORKDIR /var/www/html/my-site.com
SHELL ["/bin/bash", "-c"]
ENV PYTHONUNBUFFERED 1
ENV PYTHONDONTWRITEBYTECODE 1
COPY ./dev .
COPY ./requirements.txt .
RUN apt update
RUN apt upgrade -y
RUN apt install apache2 libapache2-mod-wsgi-py3 -y
RUN a2enmod ssl rewrite wsgi headers macro
RUN pip install --upgrade pip && \
pip install --upgrade setuptools && \
pip install -r requirements.txt
my-site.conf
WSGIDaemonProcess prod_$site python-path=/var/www/html/my-site.com:/usr/local/lib/python3.7/dist-packages