500 internal server error mod_wsgi apache "importerror: No Module named 'django'

500 internal server error mod_wsgi apache "importerror: No Module named 'django'

问题 运行 django 和 apache2/mod_wsgi。我不断收到 500 Internal Server Error。我尝试了很多 none 有效的修复组合。任何帮助是极大的赞赏。这是我的设置:

Ubuntu 16.04
django 1.10.5
apache 2.4.18
python 3.4(virtualenv)
libapache2-mod-wsgi-py3 

我的文件夹结构是:

/home/user/site/venv (virtualenv folder)
    bin
    include
    lib

/home/user/site/mysite
    |- manage.py
    static
    mysite
        |__init__.py
        |settings.py
        |urls.py
        |wsgi.py

site.conf

<VirtualHost *:80>
WSGIDaemonProcess myproject python-home=/home/user/site/venv python-path=/home/user/site/mysite
WSGIProcessGroup myproject
WSGIScriptAlias / /home/user/site/mysite/mysite/wsgi.py

        Alias /static /home/user/site/mysite/static
        <Directory /home/user/site/mysite/static>
            Require all granted
        </Directory>

        <Directory /home/user/site/mysite/mysite>
            <Files wsgi.py>
                Require all granted
            </Files>
        </Directory>


</VirtualHost>

wsgi.py

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

application = get_wsgi_application()

apache2/error.log

[mpm_event:notice] [pid 8908:tid 140560009164672] AH00491: caught SIGTERM, shutting down
[wsgi:warn] [pid 9047:tid 139761898837888] mod_wsgi: Compiled for Python/3.5.1+.
[wsgi:warn] [pid 9047:tid 139761898837888] mod_wsgi: Runtime using Python/3.5.2.
[mpm_event:notice] [pid 9047:tid 139761898837888] AH00489: Apache/2.4.18 (Ubuntu) mod_wsgi/4.3.0 Python/3.5.2 configured -- resuming normal operations
[core:notice] [pid 9047:tid 139761898837888] AH00094: Command line: '/usr/sbin/apache2'
[wsgi:error] [pid 9049:tid 139761776183040] mod_wsgi (pid=9049): Target WSGI script '/home/user/site/mysite/mysite/wsgi.py' cannot be loaded as Python module.
[wsgi:error] [pid 9049:tid 139761776183040] mod_wsgi (pid=9049): Exception occurred processing WSGI script '/home/user/site/mysite/mysite/wsgi.py'.
[wsgi:error] [pid 9049:tid 139761776183040] Traceback (most recent call last):
[wsgi:error] [pid 9049:tid 139761776183040]   File "/home/user/site/mysite/mysite/wsgi.py", line 12, in <module>
[wsgi:error] [pid 9049:tid 139761776183040]     from django.core.wsgi import get_wsgi_application
[wsgi:error] [pid 9049:tid 139761776183040] ImportError: No module named 'django'

我已授予以下文件夹的权限:

sudo chown -R www-data:www-data /home/user/site/venv
sudo chown -R www-data:www-data /home/user/site/mysite

如有任何帮助或批评,我将在此先感谢您。

尝试使用类似这样的东西。只是确认一下,myproject 是用户组吗?

WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /home/user/site/venv/lib/python2.7/site-packages
WSGIDaemonProcess ec2-user processes=1
WSGIProcessGroup ec2-user
WSGIScriptAlias / /home/user/site/mysite/mysite/wsgi.py

在您的虚拟环境中尝试 which django-admin。如果这与您希望使用的位置不同,请在您的虚拟环境中安装 django。 要么 在你的虚拟环境中尝试 pip freeze > requirements.txt 看看 django 是否真的存在。

所以在用头猛烈撞墙之后。结果我需要为我使用的 python 版本编译我自己的 mod_wsgi。我使用的是 ubuntu libapache2-mod-wsgi-py3 的标准存储库,它被编译为与 python3.5.2 一起使用,如我的 error.log 中所示。

我到这里是为了获得最新版本:mod_wsgi_releases

安装时一定要使用下面的命令mod_wsgi

.configure --with-python=/your/virtualenv/bin/python(your python_verion here)

有同样的问题,我通过在我的项目中通过虚拟环境登录时卸载 gunicorn 并重新安装来修复它

mod_wsgi是这个错误的原因,因为C代码中的mod_wsgi模块链接到Python库。因此,它编译的 Python 版本嵌入在模块中。它不只是执行 python 程序。这意味着它必须针对您要使用的 Python 版本进行编译。您不能通过虚拟环境强制它使用不同的 Python 版本.

因此,您需要卸载 mod_wsgi 模块(可能是操作系统打包的)并从源代码中自行安装 mod_wsgi,针对您想要的 Python 版本进行编译使用。

按照以下步骤修复错误:

  1. 卸载 mod_wsgi [source]:
sudo rm /usr/lib/apache2/modules/mod_wsgi.so
  1. 我们自己制作 Python 和 mod_wsgi 的自定义版本 :
## A.requirements:
sudo apt update
sudo apt install build-essential checkinstall
sudo apt install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
sudo apt install libffi-dev

## B.build 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

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

  1. 重新加载 apach2:
sudo systemctl reload apache2