DJango找不到虚拟环境

时间:2017-04-03 17:17:04

标签: python django apache

我是一个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 config:

<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

如何在apache / ubuntu上运行django?

3 个答案:

答案 0 :(得分:0)

你可以尝试这个配置,并看到它的工作原理..问题是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>

答案 1 :(得分:0)

这是一个基于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

上执行此命令

答案 2 :(得分:0)

两者:

    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.prefix并查看该值时,它应该是sys为Python虚拟环境提供的内容。

你也有其他错误。

您缺少WSGIProcessGroup myproject指令或process-group=myproject选项WSGIScriptAlias。这意味着您不会委托尝试设置Python虚拟环境的守护程序进程组。

您也不应将DocumentRoot设置为包含任何敏感内容的目录,因为Apache配置中的内容可能会公开所有文件以供下载。

最后,在将单个WSGI应用程序委派给守护程序进程组时,请设置:

WSGIApplicationGroup %{GLOBAL}

这可以避免一些第三方Python软件包无法在子解释器中正常工作的问题。