Wsgi 未找到 python 模块

Wsgi 未找到 python 模块

尝试使用 Apache 和 WSGI 运行 Django 服务器

这是我的 wsgi.py

import os

from django.core.wsgi import get_wsgi_application

sys.path.append('/home/rohan/Desktop/narsil/narsil') 
# adjust the Python version in the line below as needed 
sys.path.append('/home/rohan/Desktop/narsil/narsilenv/lib/python3.6') 

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

application = get_wsgi_application()

这是我的 /etc/apache2/sites-enabled conf 文件

<VirtualHost *:80> 
 ServerName narsil.mti.local 
 DocumentRoot /home/rohan/Desktop/narsil 
 WSGIScriptAlias / /home/rohan/Desktop/narsil/narsil/wsgi.py 

 # adjust the following line to match your Python path 
 WSGIDaemonProcess narsil.mti.local processes=2 threads=15 display-name=%{GROUP} python-home=/home/rohan/Desktop/narsil/narsilenv/lib/python3.6/site-packages
 WSGIProcessGroup narsil.mti.local 

 <directory /home/rohan/Desktop/narsil> 
   AllowOverride all 
   Require all granted 
   Options FollowSymlinks 
 </directory> 

 Alias /static/ /home/rohan/Desktop/narsil/static/ 

 <Directory /home/rohan/Desktop/rohan/static> 
  Require all granted 
 </Directory> 
</VirtualHost>

这是来自错误日志

Current thread 0x00007f0d45637bc0 (most recent call first):
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ModuleNotFoundError: No module named 'encodings'

我尝试了这里提到的类似解决方案,但它们似乎不起作用。我正在使用 virtualenv 和 pip install mod_wsgi。Django 返回 404 Not Found 这是来自 apachectl -S 的日志

AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
VirtualHost configuration:
*:80                   is a NameVirtualHost
         default server 127.0.1.1 (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost 127.0.1.1 (/etc/apache2/sites-enabled/000-default.conf:1)
         port 80 namevhost narsil.mti.local (/etc/apache2/sites-enabled/narsil.conf:1)
ServerRoot: "/etc/apache2"
Main DocumentRoot: "/var/www/html"
Main ErrorLog: "/var/log/apache2/error.log"
Mutex default: dir="/var/run/apache2/" mechanism=default 
Mutex watchdog-callback: using_defaults
PidFile: "/var/run/apache2/apache2.pid"
Define: DUMP_VHOSTS
Define: DUMP_RUN_CFG
User: name="www-data" id=33
Group: name="www-data" id=33

答案1

也许你的问题只是你写的

WSGIDaemonProcess narsil.mti.local processes=2 threads=15 display-name=%{GROUP} python-home=/home/rohan/Desktop/narsil/narsilenv/lib/python3.6/site-packages

代替

WSGIDaemonProcess narsil.mti.local processes=2 threads=15 display-name=%{GROUP} python-path=/home/rohan/Desktop/narsil:/home/rohan/Desktop/narsil/narsilenv/lib/python3.6/site-packages

(两个区别是,它是 python-path 而不是 home,并且我还在路径中添加了代码的根文件夹)

仅供参考,这是我的 site.conf 请注意,我不使用虚拟环境,我在 docker 容器中运行单个项目

LoadModule wsgi_module /usr/local/lib/python3.6/site-packages/mod_wsgi/server/mod_wsgi-py36.cpython-36m-x86_64-linux-gnu.so

WSGIRestrictEmbedded On
<VirtualHost *:443>

    SSLEngine on
    SSLCertificateFile /etc/apache2/ssl/${CERT_NAME}.crt
    SSLCertificateKeyFile /etc/apache2/ssl/${CERT_NAME}.key

    ServerAdmin no-reply@localhost
    DocumentRoot /var/www/html

    WSGIScriptAlias / /code/${PROJECT_NAME}/wsgi.py process-group=bdchem_project
    WSGIDaemonProcess bdchem_project python-path=/code:/usr/local/lib/python3.6/site-packages
    WSGIProcessGroup bdchem_project

    Alias ${STATIC_URL}/ /code/.static/
    Alias ${MEDIA_URL}/ /code/.media/
    Alias /favicon.ico /code/.static/favicon.ico
    Alias /robots.txt /code/.static/robots.txt
    Redirect permanent "/apple-touch-icon-precomposed.png" "${STATIC_URL}/favicon-256.png"
    Redirect permanent "/apple-touch-icon.png" "${STATIC_URL}/favicon-256.png"


    <Directory /code/${PROJECT_NAME}>
        WSGIApplicationGroup %{GLOBAL}
        WSGIScriptReloading On
        Require all granted
    </Directory>

    <Directory /code/.static>
        Require all granted
    </Directory>

    <Directory /code/.media>
        Require all granted
    </Directory>

    <Location />
        ExpiresActive On
        ExpiresByType image/jpg                "access plus 1 month"
        ExpiresByType image/jpeg               "access plus 1 month"
        ExpiresByType image/gif                "access plus 1 month"
        ExpiresByType image/png                "access plus 1 month"
        ExpiresByType text/css                 "access plus 1 year"
        ExpiresByType text/js                  "access plus 1 year"
        ExpiresByType text/x-javascript        "access plus 1 year"
        ExpiresByType application/x-javascript "access plus 1 year"
        ExpiresByType application/javascript   "access plus 1 year"
        ExpiresByType image/x-icon             "access plus 1 year"
        ExpiresByType application/json         "access plus 0 seconds"
        ExpiresByType text/csv                 "access plus 0 seconds"
        #ExpiresByType application/ld json     "access plus 0 seconds"
        ExpiresByType application/xml          "access plus 0 seconds"
        ExpiresByType text/xml                 "access plus 0 seconds"
        ExpiresByType text/html                "access plus 0 seconds"
        ExpiresDefault                         "access plus 1 day"
    </Location>
</VirtualHost>

相关内容