尝试将apache2与Django 1.11集成时出错[ImportError:没有名为'django'的模块]

问题描述 投票:0回答:1

我真的需要帮助,因为我一直试图深入了解它。我试图让Django使用apache2服务器。但是,当我尝试登录到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]输入“帮助”,“版权”,“信用”或“许可证”以获取更多信息。


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


dpkg -l | grep wsgi ii libapache2-mod-wsgi-py3 3.4-4ubuntu2.1.14.04.2 amd64用于Apache的Python 3 WSGI适配器模块


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


[2月27日02:32:43.409875 2018] [:错误] [pid 3211:tid 140311519147904] AssertionError:[2月27日02:32:32.388403 2018] [:warn] [pid 3567:tid 139906080114560] mod_wsgi:编译为的Python / 3.4.0。 [Tue Feb 27 02:32:44.388477 2018] [:warn] [pid 3567:tid 139906080114560] mod_wsgi:使用Python / 3.4.3运行时。 [Tue Feb 27 02:32:44.389364 2018] [mpm_event:notice] [pid 3567:tid 139906080114560] AH00489:Apache / 2.4.7(Ubuntu)mod_wsgi / 3.4 Python / 3.4.3 co $ [Tue Feb 27 02:32 :44.389393 2018] [核心:通知] [pid 3567:tid 139906080114560] AH00094:命令行:'/ usr / sbin / apache2'[Tue Feb 27 02:32:56.461421 2018] [:error] [pid 3570:tid 139905975727872 ] [remote 127.0.0.1:10860] mod_wsgi(pid = 3570):目标WSGI脚本'/ ho $ [Tue Feb 27 02:32:56.461514 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1 :10860] mod_wsgi(pid = 3570):异常发生proc $ [Tue Feb 27 02:32:56.474525 2018] [:error] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] Traceback(最近一次调用last ):[2月27日02:32:56.476070 2018] [:错误] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860]文件“/home/serverio/server.io_Tools/server.i$ [2月2日] 27 02:32:56.476112 2018] [:错误] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860]来自django.core.wsgi import get_wsgi_ap $ [Tue Feb 27 02:32:56.4 76194 2018] [:错误] [pid 3570:tid 139905975727872] [remote 127.0.0.1:10860] ImportError:没有名为'django'的模块[Tue Feb 27 02:32:59.026294 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] mod_wsgi(pid = 3570):目标WSGI脚本'/ ho $ [Tue Feb 27 02:32:59.026362 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0。 0.1:30046] mod_wsgi(pid = 3570):异常发生proc $ [Tue Feb 27 02:32:59.026402 2018] [:error] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] Traceback(最近一次通话)最后):[2月27日02:32:59.026461 2018] [:错误] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046]文件“/home/serverio/server.io_Tools/server.i$ [Tue 2月27日02:32:59.026480 2018] [:错误] [pid 3570:tid 139905858123520] [远程127.0.0.1:30046]来自django.core.wsgi import get_wsgi_ap $ [Tue Feb 27 02:32:59.026507 2018] [:错误] [pid 3570:tid 139905858123520] [remote 127.0.0.1:30046] ImportError:没有名为'django'的模块[Tue Feb 27 02:33:07.853432 2018] [:e rror] [pid 3570:tid 139905975727872] [remote 127.0.0.1:30046] mod_wsgi(pid = 3570):目标WSGI脚本'/ ho $


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


ServerName服务器

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建议我得到错误的步骤时:当我运行configure命令时,我收到以下错误:__检查___________________________________________________________________________ apxs2 ...没有检查apxs ...没有检查gcc ... gcc检查是否C编译器工作...是检查C编译器默认输出文件名... a.out检查可执行文件的后缀...检查我们是否正在交叉编译...没有检查目标文件的后缀... o检查我们是否正在使用GNU C编译器...是检查gcc是否接受-g ...是检查gcc选项是否接受ISO C89 ...无需检查prctl ...是检查Apache版本.... / configure:line 2765:apxs:command not found ./configure:line 2765:apxs:command not found ./configure:line 2766:apxs:command not found ./configure:line 2769:/:是一个目录

./configure:第2964行:apxs:找不到命令configure:创建./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:not found 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
django apache2
1个回答
0
投票

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

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

另请注意,您不能将针对一个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不能同时与djangopython3.4一起运行python3.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
© www.soinside.com 2019 - 2024. All rights reserved.