如何将 appache2 的 WSGI 设置为与 python 3.7 一起使用?

Ome*_*eld 7 python django ubuntu python-3.x python-3.7

我正在使用 ubuntu 16.04 并安装了 python 3.7 并使用以下说明将其设置为默认值: 无法在 ubuntu 中将默认 python 版本设置为 python3 当我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
Run Code Online (Sandbox Code Playgroud)

但是当我尝试连接到我没有在终端上获得的服务器时,我仍然会遇到已经安装在 /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
Run Code Online (Sandbox Code Playgroud)

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

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

 mod_wsgi (pid=89300): Call to 'site.addsitedir()' failed for '(null)', stopping.
Run Code Online (Sandbox Code Playgroud)

我的 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()
Run Code Online (Sandbox Code Playgroud)

此错误的原因是什么?

小智 5

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


Moh*_*ari 3

我们自己定制 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
Run Code Online (Sandbox Code Playgroud)

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