带有python 3.7和Apache的Python虚拟环境:是否必须编译mod_wsgi?

时间:2018-08-09 09:21:07

标签: django python-3.x apache mod-wsgi wsgi

经过很多小时,我仍然无法获得用于运行Django项目的虚拟环境。

我的Django项目在本地工作(使用runserver,没有apache,使用python 2.7和venv)。 以前,它在具有Apache但没有venv和默认python版本2.7的开发服务器上也可以正常工作。

现在,我正在尝试使用使用python 3.7的虚拟环境。


步骤1:

我用python 3.7创建了一个虚拟环境:

sudo virtualenv -p /usr/bin/python3.7 /var/www/abcd.com/venvs/venv_core
cd /var/www/abcd.com/venvs/venv_core/bin
source activate
sudo ./pip install --upgrade pip
cd ../../../
sudo venvs/venv_core/bin/pip install -r requirements.txt

(---- system-site-packages默认关闭)

Ubuntu 18.04.1。已包含python版本3.7。所以我用了。 (在之前尝试自己编译python 3.7时,我完全迷失了,不得不回滚到虚拟机的先前快照。)

系统:

Ubuntu 18.04.1 LTS "bionic"
included Python versions: 2.7.15rc1 (sys default), 3.6.5 (sys default), 3.7.0b3
Apache/2.4.29 (Ubuntu)

https://docs.python.org/3/library/venv.html https://virtualenv.pypa.io/en/stable/reference/

这个venv似乎是正确创建的。

我还修改了wsgi配置(见下文)。

http://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#

但是当我重新启动apache时,我得到了错误:

ImportError: No module named site

https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html#virtual-environment-and-python-version

Django Apache and Virtualenv ImportError: No module named site

https://serverfault.com/questions/285229/python-django-wsgi-apache-importerror-no-module-named-site

似乎我必须使用python 3.7.0编译mod_wsgi。


步骤2:

然后我尝试使用python 3.7编译mod_wsgi:

sudo apt-get remove libapache2-mod-wsgi
sudo apt-get build-dep libapache2-mod-wsgi
sudo mkdir /tmp/wsgi
cd /tmp/wsgi
sudo apt-get source libapache2-mod-wsgi
cd mod-wsgi-4.5.17
sudo dpkg-buildpackage -rfakeroot -b
sudo dpkg -i /tmp/wsgi/libapache2-mod-wsgi-py3_4.5.17-1_amd64.deb

我收到以下错误(下面的详细日志):

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

mod_wsgi is compiled in one version and running in a different version even after following the given steps

https://bugs.launchpad.net/ubuntu/+source/libapache2-mod-python/+bug/1073147/comments/8

Django / Apache / mod_wsgi: No module named importlib

https://code.google.com/archive/p/modwsgi/wikis/CheckingYourInstallation.wiki#Python_Installation_In_Use


最后,我不知道该如何做才能使系统正常运行。 我对Linux不太了解,而且我不是Python专家。

1)我必须为python 3.7编译mod_wsgi还是有更简单的方法?

2)我是否正确编译了mod_wsgi?如果没有,我该怎么办?

3)是否已正确配置dev.conf?如果没有,我该怎么更改?

请问有人可以帮我吗?


dev.conf(Apache配置:etc / apache2 / sites-available / dev.conf):

Define server_name abcde.com
WSGIPythonHome /var/www/${server_name}/venvs/venv_core/lib/python3.7/
...
<IfModule mod_ssl.c>
    WSGIDaemonProcess ${server_name} processes=2 threads=15 display-name=%{GROUP} python-path=/var/www/${server_name}:/var/www/${server_name}/venvs/venv_core/lib/python3.7:/var/www/${server_name}/venvs/venv_core/lib/python3.7/site_packages
    WSGIProcessGroup ${server_name}
    WSGIScriptAlias / /var/www/${server_name}/index.wsgi
...

index.wsgi:

import os
import time
import traceback
import signal
import sys
from django.core.wsgi import get_wsgi_application

sys.path.append('/var/www/abcde.com/')
sys.path.append('/var/www/abcde.com/abcde')
os.environ['DJANGO_SETTINGS_MODULE'] = 'abcde.settings'

try:
    application = get_wsgi_application()
except Exception:
    print 'handling WSGI exception'
    # Error loading applications
    if 'mod_wsgi' in sys.modules:
        traceback.print_exc()
        os.kill(os.getpid(), signal.SIGINT)
        time.sleep(2.5)

error.log:

Current thread 0x00007f2e8992dbc0 (most recent call first):
[Wed Aug 08 21:21:05.853747 2018] [core:notice] [pid 6566:tid 139837853326272] AH00051: child pid 6985 exit signal Aborted (6), possible coredump in /etc/apache2
[Wed Aug 08 21:21:05.853852 2018] [mpm_event:notice] [pid 6566:tid 139837853326272] AH00491: caught SIGTERM, shutting down
[Wed Aug 08 21:21:05.995273 2018] [mpm_event:notice] [pid 7007:tid 139971756051392] AH00489: Apache/2.4.29 (Ubuntu) OpenSSL/1.1.0g mod_wsgi/4.5.17 Python/3.6 configured -- resuming normal operations
[Wed Aug 08 21:21:05.995471 2018] [core:notice] [pid 7007:tid 139971756051392] AH00094: Command line: '/usr/sbin/apache2'
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
...
Current thread 0x00007f4db6cc4bc0 (most recent call first):
[Wed Aug 08 21:21:06.996663 2018] [core:notice] [pid 7007:tid 139971756051392] AH00051: child pid 7009 exit signal Aborted (6), possible coredump in /etc/apache2
[Wed Aug 08 21:21:06.997392 2018] [core:notice] [pid 7007:tid 139971756051392] AH00051: child pid 7010 exit signal Aborted (6), possible coredump in /etc/apache2
[Wed Aug 08 21:21:06.997804 2018] [core:notice] [pid 7007:tid 139971756051392] AH00051: child pid 7011 exit signal Aborted (6), possible coredump in /etc/apache2
[Wed Aug 08 21:21:06.997870 2018] [core:notice] [pid 7007:tid 139971756051392] AH00051: child pid 7012 exit signal Aborted (6), possible coredump in /etc/apache2
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'
...

0 个答案:

没有答案