[Fixed]-DJango won't find virtual env

1👍

Both of:

    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/

were wrong attempts. The commented out one was closest but still wrong.

Go read:

on how to set up Python virtual environments.

The python-home option should specify a single directory (not a list) which is the root of the Python virtual environment (not site-packages). It should be what sys.prefix gives for the Python virtual environment when you import sys and look at that value.

You also have other things wrong as well though.

Your are missing WSGIProcessGroup myproject directive or process-group=myproject option to WSGIScriptAlias. This means you aren’t delegating to the daemon process group where trying to set Python virtual environment.

You also should not set DocumentRoot to a directory which includes anything sensitive as a stuff up in Apache configuration could then expose all the files for download.

Finally, when delegating a single WSGI application to a daemon process group, set:

WSGIApplicationGroup %{GLOBAL}

This avoids problems with some third party Python packages not implemented to work correctly in sub interpreters.

0👍

Can you try this config, and see it works.. The issue is apache is not able to find the python modules path. WSGIPythonPath should be taken care.

# 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>

0👍

Here is a solution based on RHEL7, Python3.5, Apache 2.4. Change accordingly. The compiling part of mod_wsgi is the important one. This is using 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>

Then execute this command on the shell systemctl restart httpd

👤nomad

Leave a comment