Error [ImportError: No module named 'django'] while trying to integrate apache2 with Django 1.11

Error [ImportError: No module named 'django'] while trying to integrate apache2 with Django 1.11

我真的需要这方面的帮助,因为我已经尝试了一段时间了。我试图让 Django 使用 apache2 server.However,当我尝试登录到 apache 页面时,我得到:


内部服务器错误

服务器遇到内部错误或配置错误,无法完成您的请求。

请通过 webmaster@localhost 联系服务器管理员,告知他们发生此错误的时间,以及您在此错误之前执行的操作。

服务器错误日志中可能提供了有关此错误的更多信息。 Apache/2.4.7 (Ubuntu) 服务器位于 127.0.1.1 端口 80


我已经从 ubuntu 1404LTS

复制了我的版本输出

Python 3.6.0+(默认,2017 年 2 月 4 日,11:11:33) linux 上的 [GCC 4.9.4] 键入 "help"、"copyright"、"credits" 或 "license" 以获取更多信息。


django.VERSION (1, 11, 4, 'final', 0)


dpkg -l | grep wsgi ii libapache2-mod-wsgi-py3 3.4-4ubuntu2.1.14.04.2 amd64 Python 3 WSGI 适配器 module for Apache


当我打开日志时,我看到以下内容:


[2 月 27 日星期二 02:32:43.409875 2018] [:error] [pid 3211:tid 140311519147904] 断言错误: [2 月 27 日星期二 02:32:44.388403 2018] [:warn] [pid 3567:tid 139906080114560] mod_wsgi:为 Python/3.4.0 编译。 [2 月 27 日星期二 02:32:44.388477 2018] [:warn] [pid 3567:tid 139906080114560] mod_wsgi:运行时使用 Python/3.4.3。 [2 月 27 日星期二 02:32:44.389364 2018] [mpm_event:通知] [pid 3567:tid 139906080114560] AH00489:Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/3.4.3 co$ [2 月 27 日星期二 02:32:44.389393 2018] [核心:通知] [pid 3567:tid 139906080114560] AH00094:命令行:'/usr/sbin/apache2' [2 月 27 日星期二 02:32:56.461421 2018] [:error] [pid 3570:tid 139905975727872] [远程 127.0.0.1:10860] mod_wsgi (pid=3570): 目标 WSGI 脚本 '/呵呵 [2 月 27 日星期二 02:32:56.461514 2018] [:error] [pid 3570:tid 139905975727872] [远程 127.0.0.1:10860] mod_wsgi (pid=3570): 异常发生 proc$ [2 月 27 日星期二 02:32:56.474525 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] Traceback(最后一次通话): [2 月 27 日星期二 02:32:56.476070 2018] [:error] [pid 3570:tid 139905975727872] [远程 127.0.0.1:10860] 文件“/home/serverio/server.io_Tools/server.i$ [2 月 27 日星期二 02:32:56.476112 2018] [:error] [pid 3570:tid 139905975727872] [远程 127.0.0.1:10860] 来自 django.core.wsgi 导入 get_wsgi_ap$ [2 月 27 日星期二 02:32:56.476194 2018] [:error] [pid 3570:tid 139905975727872] [远程 127.0.0.1:10860] ImportError: No module named 'django' [2 月 27 日星期二 02:32:59.026294 2018] [:error] [pid 3570:tid 139905858123520] [远程 127.0.0.1:30046] mod_wsgi (pid=3570): 目标 WSGI 脚本 '/呵呵 [2 月 27 日星期二 02:32:59.026362 2018] [:error] [pid 3570:tid 139905858123520] [远程 127.0.0.1:30046] mod_wsgi (pid=3570): 异常发生 proc$ [2 月 27 日星期二 02:32:59.026402 2018] [:error] [pid 3570:tid 139905858123520] [远程 127.0.0.1:30046] 追溯(最近一次通话最后一次): [2 月 27 日星期二 02:32:59.026461 2018] [:error] [pid 3570:tid 139905858123520] [远程 127.0.0.1:30046] 文件“/home/serverio/server.io_Tools/server.i$ [2 月 27 日星期二 02:32:59.026480 2018] [:error] [pid 3570:tid 139905858123520] [远程 127.0.0.1:30046] 来自 django.core.wsgi 导入 get_wsgi_ap$ [2 月 27 日星期二 02:32:59.026507 2018] [:error] [pid 3570:tid 139905858123520] [远程 127.0.0.1:30046] ImportError: No module named 'django' [2 月 27 日星期二 02:33:07.853432 2018] [:error] [pid 3570:tid 139905975727872] [远程 127.0.0.1:30046] mod_wsgi (pid=3570): 目标 WSGI 脚本 '/豪$


这是我的 apache 服务器的 000-默认配置:


ServerName serverio

ServerAdmin webmaster@localhost

Alias /static /home/serverio/server.io_Tools/server.io/mysite/userManager/static
    <Directory /home/serverio/server.io_Tools/server.io/mysite/userManager/static>
       Require all granted
     </Directory>

<Directory /home/serverio/server.io_Tools/server.io/mysite/mysite>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

WSGIDaemonProcess serverio python-path=/home/serverio/server.io_Tools/server.io:/home/serverio/server.io_Tools/server.io/python3.6/site-packages
WSGIProcessGroup serverio
WSGIScriptAlias / /home/serverio/server.io_Tools/server.io/mysite/mysite/wsgi.py


ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined

____________________________________________________________________________

当我尝试 Yka 建议的步骤时,我得到了错误: 当我 运行 配置命令时,我得到以下 error:__checking ___________________________________________________________________________ apxs2 ... 没有 检查 apxs ... 没有 检查 gcc ... gcc 检查 C 编译器是否工作...是的 检查 C 编译器默认输出文件名... a.out 检查可执行文件的后缀... 检查我们是否在交叉编译......不 检查目标文件的后缀... o 检查我们是否使用 GNU C 编译器...是的 检查 gcc 是否接受 -g... 是 检查 gcc 选项以接受 ISO C89... none 需要 检查 prctl... 是的 检查 Apache 版本... ./configure: line 2765: apxs: command not found ./configure:第 2765 行:apxs:找不到命令 ./configure:第 2766 行:apxs:找不到命令 ./configure: line 2769: /: 是一个目录

./configure:第 2964 行:apxs:找不到命令 配置:创建 ./config.status config.status: 创建 Makefile


当我尝试执行 sudo make 时,我得到:


apxs -c -I/usr/include/python3.6m -DNDEBUG -Wc,-g -Wc,-O2 src/server/mod_wsgi.c src/server/wsgi_*.c -L/usr/lib - L/usr/lib/python3.6/config-3.6m -lpython3.6m -lpthread -ldl -lutil -lm /bin/sh: 1: apxs: 未找到 make: *** [src/server/mod_wsgi.la] 错误 127

YKH,检查我遇到的新错误


当我 运行 :sudo apt-get install apache2-dev 我一输入 sudo make 就会出现以下错误。

virtual-machine:~/Downloads/mod_wsgi-4.5.24$ sudo make 
/usr/bin/apxs2 -c -I/usr/include/python3.6m -DNDEBUG  -Wc,-g -Wc,-O2  src/server/mod_wsgi.c src/server/wsgi_*.c -L/usr/lib -L/usr/lib/python3.6/config-3.6m  -lpython3.6m -lpthread -ldl  -lutil -lm
/usr/share/apr-1.0/build/libtool --silent --mode=compile --tag=disable-static x86_64-linux-gnu-gcc -std=gnu99 -prefer-pic -pipe -g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security  -D_FORTIFY_SOURCE=2   -DLINUX -D_REENTRANT -D_GNU_SOURCE  -pthread  -I/usr/include/apache2  -I/usr/include/apr-1.0   -I/usr/include/apr-1.0 -I/usr/include -g -O2 -I/usr/include/python3.6m -DNDEBUG  -c -o src/server/mod_wsgi.lo src/server/mod_wsgi.c && touch src/server/mod_wsgi.slo
In file included from src/server/mod_wsgi.c:22:0:
src/server/wsgi_python.h:24:20: fatal error: Python.h: No such file or directory
 #include <Python.h>
                    ^
compilation terminated.
apxs:Error: Command failed with rc=65536
.
make: *** [src/server/mod_wsgi.la] Error 1


I also tried this another machine, this time on ubuntu 16LTS and get a similar 
error message as shown below:
_________________________________________________________


  2018] [mpm_event:notice] [pid 65824:tid 140243591341952] AH00491: caught
SIGTERM, shutting down [Tue Feb 27 14:49:42.462678 2018]
[mpm_event:notice] [pid 1366:tid 140394775304064] AH00489: Apache/2.4.18
(Ubuntu) configured -- resuming normal operations [Tue Feb 27
14:49:42.463290 2018] [core:notice] [pid 1366:tid 140394775304064]
AH00094: Command line: '/usr/sbin/apache2' [Tue Feb 27 14:55:49.361004
2018] [mpm_event:notice] [pid 1366:tid 140394775304064] AH00491: caught
SIGTERM, shutting down [Tue Feb 27 14:55:50.444530 2018] [wsgi:warn]
[pid 2393:tid 139752168679296] mod_wsgi: Compiled for Python/3.5.1+.
[Tue Feb 27 14:55:50.444616 2018] [wsgi:warn] [pid 2393:tid
139752168679296] mod_wsgi: Runtime using Python/3.5.2. [Tue Feb 27
14:55:50.445196 2018] [mpm_event:notice] [pid 2393:tid 139752168679296]
AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured
-- resuming normal operations [Tue Feb 27 14:55:50.445223 2018]
[core:notice] [pid 2393:tid 139752168679296] AH00094: Command line:
'/usr/sbin/apache2' [Tue Feb 27 16:26:53.178835 2018] [mpm_event:notice]
[pid 2393:tid 139752168679296] AH00491: caught SIGTERM, shutting down
[Tue Feb 27 16:26:54.191342 2018] [mpm_event:notice] [pid 2972:tid
139914716534656] AH00489: Apache/2.4.18 (Ubuntu) configured -- resuming

从消息中的路径可以看出,您的 mod_wsgi 是为 Python 3.4 编译的。您尝试 运行 for Django 的代码是 python3.6.

您需要重新安装 mod_wsgi 根据您要使用的 Python 3.X 版本编译。

另请注意,您不能使用针对一个 Python 版本编译的 mod_wsgi 和针对不同版本的 Python 虚拟环境。 mod_wsgi 必须为同一版本编译。

mod_wsgi issue239

到这里下载最新的mod_wsgi,复制到你的服务器,运行:

    tar xvfz mod_wsgi-X.Y.tar.gz
    cd mod_wsgi-X.Y/
    sudo ./configure --with-python=/usr/bin/python3.6
    sudo make
    sudo make install 

    sudo nano /etc/apache2/mods-available/wsgi.load
    LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so # write this to wsgi.load file

    sudo a2enmod wsgi 
    sudo service apache2 restart

mod_wsgi 不能 运行 django 项目同时与 python3.4python3.6 项目,如果你有一些项目 运行 之前使用python3.4,你也需要更新它的虚拟环境到python3.6,因为django2.0(将在2019/03停止维护)是最后一个版本支持python3.4,将所有项目更新为 python3.6 是一个不错的选择,全部!


在ubuntu14.04、Apache 2.4.7、Django 2.0.2上

这就是我安装 python3.6:

的方式
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.6 python3.6-dev python3.6-venv

和apache2:

sudo apt-get update
sudo apt-get install apache2 apache2-dev

创建虚拟环境:

python3.6 -m venv environment