10

The error from apache after a 504 page

[info] mod_wsgi (pid=): Python home /var/venv/mybox.
[info] mod_wsgi (pid=): Initializing Python.
ImportError: No module named site

This is with a barely configured app.

<IfModule mod_wsgi.c>
WSGIDaemonProcess myapp python-home=/var/venv/mybox
WSGIProcessGroup myapp
WSGIScriptAlias / /var/www/html/web/myapp/wsgi.py
WSGISocketPrefix /var/run/wsgi

<Directory /var/www/html/web>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</IfModule>

Followed every post and tutorial I can. I am on CENTOS6 . using virutal env python 2.7 the default system env is 2.6

$ ldd /etc/httpd/modules/mod_wsgi.so
  linux-vdso.so.1 =>  (0x00007ffc06174000)

mywsgi.py

 import os,sys     
 from django.core.wsgi import get_wsgi_application     
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
 sys.path.insert(0,'/var/www/html/web')
 activate_this = '/var/venv/mybox/bin/activate_this.py'
 execfile(activate_this, dict(__file__=activate_this))
 application = get_wsgi_application()

PYHTONHOME is not set

Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
Jabda
  • 1,752
  • 5
  • 26
  • 54

5 Answers5

10

The documentation for using virtual environments with mod_wsgi can be found at:

Most important in your case is the section:

In that section it states:

When using a Python virtual environment with mod_wsgi, it is very important that it has been created using the same Python installation that mod_wsgi was originally compiled for. It is not possible to use a Python virtual environment to force mod_wsgi to use a different Python version, or even a different Python installation.

You cannot for example force mod_wsgi to use a Python virtual environment created using Python 3.5 when mod_wsgi was originally compiled for Python 2.7. This is because the Python library for the Python installation it was originally compiled against is linked directly into the mod_wsgi module.

So most likely what is happening is that mod_wsgi is compiled for Python 2.6. You cannot in this case force it to use a Python virtual environment created from Python 2.7. When you do this, you will get the error you see about site module being missing.

You will need to uninstall that mod_wsgi from system packages and install mod_wsgi from source code, compiling it against Python 2.7. The easiest way to do this might be to use the pip install method as described in:

Run pip install to install it in your virtual environment and then follow instructions in section 'Connecting into Apache installation' about configuring Apache to use it.

Community
  • 1
  • 1
Graham Dumpleton
  • 57,726
  • 6
  • 119
  • 134
  • Cannot load /var/venv/mybox/lib/python2.7/site-packages/mod_wsgi/server/mod_wsgi-py27.so into server: cannot open shared object file: Permission denied. I tried changing permission – Jabda Dec 06 '16 at 23:10
  • Your SELinux profile is most likely not allowing Apache to use stuff in that location. Try having virtual environment under ``/var/www``. – Graham Dumpleton Dec 06 '16 at 23:18
  • Installing mod_wsgi using pip and moving the virtualenv to /var/www worked. This is an internal site so we can make that compromise – Jabda Dec 07 '16 at 00:03
8

Solved in CentOS 7 with Apache 2.4.6

My whole server uses Python 2.7, but I've already installed Python 3.6 and my virtualenv is using Python 3.6.

After configured djang.conf (/etc/httpd/conf.d/django.conf) with this code:

<VirtualHost *:80>

WSGIDaemonProcess myProj python-home=/home/user/django-site/env python-path=/home/user/django-site
WSGIProcessGroup myProj
WSGIScriptAlias /myProj /home/user/django-site/my-project/wsgi.py


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

<Directory /home/user/django-site/my-project>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

</VirtualHost>

And restarted my apache

sudo systemctl restart httpd

I got this error a thousand lines (/var/log/httpd/error_log)

ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site

The solution

First:

sudo grep wsgi /var/log/httpd/error_log

I got this:

[mpm_prefork:notice] [pid 62324] AH00163: Apache/2.4.6 (CentOS) PHP/7.0.33 mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations

Note the Python version (2.7.5). What I did to get mod_wsgi according to my Python 3.6 is using:

yum list *mod_wsgi*
Installed packages
mod_wsgi.x86_64                          3.4-18.el7                          @base
Disponible packages
python35u-mod_wsgi.x86_64                4.6.2-1.ius.centos7                 ius
python36u-mod_wsgi.x86_64                4.6.2-1.ius.centos7                 ius

and then I installed the package python36u-mod_wsgi.x86_64:

sudo yum install python36u-mod_wsgi.x86_64

Then I restarted Apache service:

sudo systemctl restart httpd

And got this new line from logs:

[Fri Mar 29 12:33:26.788716 2019] [mpm_prefork:notice] [pid 76317] AH00163: Apache/2.4.6 (CentOS) PHP/7.0.33 mod_wsgi/4.6.2 Python/3.6 configured -- resuming normal operations

And everything works! :-)

Hope it helps you. C ya!

1

this is taken fron the Documentation write this: WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

and this is specifically for the virtual env, you need to write the path to the site packeges of your python virtual env:

WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages

the problem may also be in the - PYTHONHOME

Change the location of the standard Python libraries. By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion, where prefix and exec_prefix are installation-dependent directories, both defaulting to /usr/local.

When PYTHONHOME is set to a single directory, its value replaces both prefix and exec_prefix. To specify different values for these, set PYTHONHOME to prefix:exec_prefix.

Try to clean up your PYTHONHOME:

user$ export PYTHONHOME=
Ariel Livshits
  • 591
  • 1
  • 7
  • 23
1

On Ubuntu, if you want to use python3, you will have to uninstall libapache2-mod-wsgi-py and install libapache2-mod-wsgi-py3 instead if you obtained your mod_wsgi from the repository.

mateuszb
  • 1,072
  • 13
  • 26
1

I was on centos6.X with python 2.7, reinstall of mod_wsgi has fixed the issue for me.

yum remove mod_wsgi.x86_64
yum install mod_wsgi.x86_64
/etc/init.d/httpd restart 
Naren
  • 11
  • 1