如何设置 appache2 的 WSGI 以使用 python 3.7?

how to set WSGI of appache2 to work with python 3.7?

我正在使用 ubuntu 16.04 并安装了 python 3.7 并使用以下说明将其设置为默认值: 当我在控制台中键入 python 我得到 python3.7 时,我尝试使用 :

将 appache2 设置为与 python 3.7 一起使用
sudo add-apt-repository --yes ppa:deadsnakes/ppa
sudo apt-get update --yes
sudo apt-get install --yes python3.7
sudo apt-get install --yes python3-pip
sudo apt-get --yes install python3-pip apache2 libapache2-mod-wsgi-py3 
sudo a2enmod wsgi
sudo apt install --yes python-django-common
sudo apt-get  install --yes python-django

但是当我尝试连接到服务器时,我仍然得到已经安装在 /var/log/apache2/error.log 中的导入包的异常,但我没有像这样在终端获得:

Traceback (most recent call last):
 File "/home/ubuntu/my_code/wsgi.py", line 11, in <module>
     from django.core.wsgi import get_wsgi_application
 ImportError: No module named 'django'
 mod_wsgi (pid=75005): Target WSGI script '/home/ubuntu/my_code/wsgi.py' cannot be loaded as Python module.
mod_wsgi (pid=75005): Exception occurred processing WSGI script '/home/ubuntu/my_code/wsgi.py'.
 Traceback (most recent call last):
  File "/home/ubuntu/my_code/wsgi.py", line 11, in <module>
  from django.core.wsgi import get_wsgi_application

mod_wsgi (pid=75005): Target WSGI script '/home/ubuntu/my_code/wsgi.py' cannot be loaded as Python module.

即使我在 python 3.7 中安装了 django 我得到的另一个错误是在服务重启后:

 mod_wsgi (pid=89300): Call to 'site.addsitedir()' failed for '(null)', stopping.

我的 wsgiy.py :

import os
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "servicesite.settings")
path='/home/ubuntu/my_code/'

if path not in sys.path:
  sys.path.append(path)

os.environ['DJANGO_SETTINGS_MODULE'] = 'my_code.settings'
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()

出现此错误的原因是什么?

我有一个类似的错误消息,原来是 SELinux 强制执行是罪魁祸首。 可以尝试关闭SELinuxtemporary进行测试

setenforce 0

// run sestatus to confirm that either it's permissive or disabled
sestatus

如果这解决了您的问题,您可以掷骰子并永久禁用 SELinux,或者找到正确的方法来解决它。

您是否手动编译 python?如果真是这样,那就完全不同了。您可能需要使用大量标志重新编译 python 才能使其正常工作。

首先,您似乎搞砸了 python 和 pip 的安装。如果你安装了 python3-pip 那么你应该尝试 sudo pip3 intall django 然后检查从你正在使用的 python37 解释器导入 Django。

另一件事是使用系统 python 不是好方法。

我建议按照 https://realpython.com/intro-to-pyenv/

提高您的知识

也将 apache 与 mod_wsgi 一起使用已经很老了,您可以尝试 https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-16-04

您需要构建 python 然后构建 wsgi,因为默认的 wsgi 是基于 is 系统构建的 python 使用本教程 https://medium.com/@garethbjohnson/serve-python-3-7-with-mod-wsgi-on-ubuntu-16-d9c7ab79e03a

我们自己制作 Python 和 mod_wsgi 的自定义版本。

apt install apache2 apache2-dev
wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.7.1.tar.gz
tar xvfz 4.7.1.tar.gz
cd mod_wsgi-4.7.1

./configure --with-python=[your python path]
## For example: ./configure --with-python=/usr/bin/python3.7

sudo make
sudo make install

## Finally:
sudo systemctl reload apache2

您可以使用 which python3.7 找到 Python 文件的路径。