簡體   English   中英

DJango找不到虛擬環境

[英]DJango won't find virtual env

我是n00b,試圖讓Django在新安裝中與apache一起使用。 端口8000上的測試服務器可以正常工作,但是apache不能。 看來apache無法讀取虛擬環境

日志錯誤:

[Mon Apr 03 17:14:32.560566 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] mod_wsgi (pid=10799): Target WSGI script '/home/palace/palace/palace/wsgi.py' cannot be loaded as Python module.
[Mon Apr 03 17:14:32.560944 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] mod_wsgi (pid=10799): Exception occurred processing WSGI script '/home/palace/palace/palace/wsgi.py'.
[Mon Apr 03 17:14:32.561249 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] Traceback (most recent call last):
[Mon Apr 03 17:14:32.561338 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774]   File "/home/palace/palace/palace/wsgi.py", line 12, in <module>
[Mon Apr 03 17:14:32.561386 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774]     from django.core.wsgi import get_wsgi_application
[Mon Apr 03 17:14:32.561470 2017] [wsgi:error] [pid 10799] [client 72.178.8.113:37774] ImportError: No module named 'django'

apache的配置:

<VirtualHost *:80>
        ServerName www.persiaspalace.us

        ServerAdmin webmaster@localhost
        DocumentRoot /home/palace/
        WSGIDaemonProcess myproject python-home=/home/palace/palace/:/home/palace/vpalace/bin:/home/palace/vpalace/lib/python3.5/site-packages/
        #WSGIDaemonProcess myproject python-home=/home/palace/vpalace/lib/python3.5/site-packages/ python-path=/home/palace/palace/
        WSGIScriptAlias / /home/palace/palace/palace/wsgi.py
        #WSGIPythonHome /home/palace/vpalace/
        #WSGIPythonPath /home/palace/palace/
        #LogLevel info ssl:warn

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined
        Alias /static /home/palace/
        <Directory /home/palace/>
        <Files wsgi.py>
                Require all granted
        </Files>
         </Directory>

        #Include conf-available/serve-cgi-bin.conf

</VirtualHost>

django安裝在虛擬環境中:

(vpalace) root@pieceofshiot:/home/palace# pip install django
Requirement already satisfied: django in ./vpalace/lib/python3.5/site-packages

目錄權限結構:文件為0644,目錄為0744

(vpalace) root@pieceofshiot:/home/palace# ls -l
total 12
-rw-r--r-- 1 www-data www-data    0 Apr  3 04:14 index.php.bak
-rwxr-xr-x 1 www-data www-data  807 Apr  2 22:36 manage.py
drwxr--r-- 3 www-data www-data 4096 Apr  2 22:46 palace
drwxr-xr-x 4 www-data www-data 4096 Apr  2 22:32 vpalace

django版本:

>>> django.VERSION
(1, 10, 6, 'final', 0)

mod_wsgi版本:

ii  libapache2-mod-wsgi-py3                    4.3.0-1.1build1                                  amd64        Python 3 WSGI adapter module for Apache

如何在Django / Ubuntu上運行django?

您可以嘗試此配置,然后查看它是否有效。.問題是apache無法找到python模塊路徑。 WSGIPythonPath應該小心。

# this section will be commented in the httpd.conf, uncomment them to use virtualhosts
NameVirtualHost *:80


# Add the WSGI settings
WSGISocketPrefix /var/run/wsgi
WSGIPythonPath /home/vpalace/lib/python3.5/site-packages
WSGIDaemonProcess vpalace processes=1 maximum-requests=500 threads=1
WSGIProcessGroup vpalace


<VirtualHost *:80>
    ServerAdmin webmaster@localhost
    DocumentRoot /home/palace/
    ServerName www.persiaspalace.us
        Alias /static /home/palace/

    <Directory /home/palace/>
        <Files wsgi.py>
              Require all granted
        </Files>
    </Directory>
    WSGIScriptAlias / /home/palace/palace/palace/wsgi.py
    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

這是一個基於RHEL7,Python3.5,Apache 2.4的解決方案。 相應地更改。 mod_wsgi的編譯部分很重要。 這是使用virtualenv

Apache2.4

firewall-cmd --permanent --zone=public --add-service=http
firewall-cmd --permanent --zone=public --add-service=https
firewall-cmd --reload

chgrp -R apache /var/www/html
find /var/www/html -type d -exec chmod g+rx {} +
find /var/www/html -type f -exec chmod g+r {} +

chown -R myuser /var/www/html/
find /var/www/html -type d -exec chmod u+rwx {} +
find /var/www/html -type f -exec chmod u+rw {} +

find /var/www/html -type d -exec chmod g+s {} +

Python3.5

yum install -y https://rhel7.iuscommunity.org/ius-release.rpm
yum install -y python35u python35u-libs python35u-devel python35u-pip
pip3.5 install virtualenv

mod_wsgi的

wget https://github.com/GrahamDumpleton/mod_wsgi/archive/4.5.14.tar.gz
tar -zxvf 4.5.14.tar.gz
cd mod_wsgi-4.5.14
./configure --with-python=/usr/bin/python3.5
make
make install
chmod 755 /usr/lib64/httpd/modules/mod_wsgi.so

myportal.conf

vi /etc/httpd/conf.d/myportal.conf

LoadModule wsgi_module /usr/lib64/httpd/modules/mod_wsgi.so
<VirtualHost *:80>
  ServerAdmin admin@email.com
  ServerName myportal
  ServerAlias myportal.com
  DocumentRoot /var/www/html/myportal/src/

  WSGIDaemonProcess myportal python-path=/var/www/html/myportal/src:/var/www/html/myportal/venv/lib/python3.5/site-packages
  WSGIApplicationGroup myportal
  WSGIScriptAlias / /var/www/html/myportal/src/myportal/wsgi.py process-group=myportal

  <Directory /var/www/html/myportal/src>
    Require all granted
  </Directory>

  <Directory /var/www/html/myportal/src/myportal>
    <Files wsgi.py>
      Require all granted
    </Files>
  </Directory>

</VirtualHost>

然后在shell systemctl restart httpd上執行以下命令, systemctl restart httpd

兩者的:

    WSGIDaemonProcess myproject python-home=/home/palace/palace/:/home/palace/vpalace/bin:/home/palace/vpalace/lib/python3.5/site-packages/
    #WSGIDaemonProcess myproject python-home=/home/palace/vpalace/lib/python3.5/site-packages/ python-path=/home/palace/palace/

是錯誤的嘗試。 評論出來的是最接近但仍然錯誤的。

繼續閱讀:

有關如何設置Python虛擬環境的信息。

python-home選項應該指定一個目錄(不是列表),該目錄是Python虛擬環境的根目錄(不是site-packages )。 導入sys並查看該值時,應該是sys.prefix為Python虛擬環境提供的。

您也有其他錯誤。

你缺少WSGIProcessGroup myproject指令或process-group=myproject選項WSGIScriptAlias 這意味着您不會委托給試圖設置Python虛擬環境的守護進程組。

您也不應將DocumentRoot設置為包含敏感內容的目錄,因為Apache配置中的內容可能會暴露所有文件以供下載。

最后,當將單個WSGI應用程序委派給守護進程組時,請設置:

WSGIApplicationGroup %{GLOBAL}

這樣可以避免某些第三方Python程序包未實現在子解釋器中正常工作的問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM