24

I'm using Apache 2.2.15 on CentOS 6.5. I'm trying to set up a Django app using mod_wsgi. I'm using a virtual environment, and mod_wsgi was configured with --with-python=/path/to/virtualenv/bin/python3.4.

I've added this to my httpd.conf:

WSGIPythonPath /srv/myproject:/path/to/virtualenv/lib/python3.4/site-packages
WSGIPythonHome /path/to/virtualenv
<VirtualHost *:80>
WSGIScriptAlias / /srv/myproject/myproject/wsgi.py
...
</VirtualHost>

In wsgi.py, I added

sys.path.insert(1, "/path/to/virtualenv/lib/python3.4/site-packages")

The problem is that when I try to open the app in my browser, it loads indefinitely. Here's the Apache error log:

Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'
[Mon Jun 30 17:37:28 2014] [notice] child pid 19370 exit signal Aborted (6)
[Mon Jun 30 17:37:28 2014] [notice] child pid 19371 exit signal Aborted (6)
...
[Mon Jun 30 17:37:28 2014] [notice] child pid 19377 exit signal Aborted (6)
Fatal Python error: Py_Initialize: Unable to get the locale encoding
ImportError: No module named 'encodings'

What's interesting is that in both the system installation of Python (2.6) and the virtual environment version (3.4), import encodings works fine! I've tried using the example WSGI script from the mod_wsgi CheckingYourInstallation page to confirm which version of Python is being used by Apache, but I get the same ImportError.

Does anyone have a suggestion for next steps? I've scoured the docs but I don't know where to go from here.

tao_oat
  • 1,011
  • 1
  • 15
  • 33
  • could be a problem with access rights. does the apache user have read access to everything within your virtualenv (including execute rights for directories and parent directories)? – mata Jun 30 '14 at 17:29
  • Yes, it does. Even setting everything in my virtual environments folder to 777 didn't work! – tao_oat Jun 30 '14 at 18:19
  • I am having the same problem with CentOS 6.5 and Apache 2.2.15 - Py_Initialize cannot import encodings. When I compiled Python 3.4.1 it gave me some warning about an exec prefix. Perhaps these are related. If you have a solution I would love to know. – Michael Miller Jul 15 '14 at 21:59
  • 1
    I solved my problem by using virtualenv rather than pyvenv. It turns out that pyvenv did not symlink all the local modules like encoding, but virtualenv did. [More information about pyvenv vs virtualenv for Python 3.4](https://community.webfaction.com/questions/17074/understanding-pyvenv-and-pip-in-virtual-environments-with-python-34-and-above) – Michael Miller Jul 16 '14 at 03:23

3 Answers3

7

So with some help with my friends (IE: SysAdmins), we got this figured out last night. I learn best by example, so let's assume you're running Apache with mod_wsgi as Linux group apache with user flipperpa. Let's assume you're hosting in /home/my_project with wsgi.py in my_project/wsgi.py.

At the top level (ls -l /):

drwxr-xr-x.  47 root root  4096 Jul  9 09:43 home

In the home directory (ls -l /home):

drwxrwsr-x   7 flipper        apache 4096 Jul 29 10:22 my_project

This was the key. The lower case "s" means the apache group's setgid bit is set, and the execute bit is set. The final "x", of course, means anyone can execute.

Check your permissions down the tree; this did the trick for us.

FlipperPA
  • 13,607
  • 4
  • 39
  • 71
  • This was the issue for me, a python file in the same directory as `application` wasn't being imported. Changed permissions on the file so group and others could read and it was. – user1063287 Nov 26 '14 at 00:10
  • 2
    i have applied all the steps you have mentioned here, but still getting same error – Sahadev Jun 02 '18 at 07:05
  • Are you running CentOS/RedHat with SELinux enabled? If so, you'll see to set the security context on files as well. – FlipperPA Jun 02 '18 at 14:14
2

The issue was caused by SELinux. What I did was this:

  • moved the project files into /home/admin/myproject
  • changed permissions as in FlipperPA's answer
  • and, crucially I think, set the httpd_enable_homedirs SELinux boolean to on using setsebool -P httpd_enable_homedirs on.

The SELinux guide on the CentOS wiki was very helpful. For example, to debug you can run setenforce Permissive so SELinux won't enforce its rules, but still write to the log. Just remember to setenforce Enforcing again afterwards!

tao_oat
  • 1,011
  • 1
  • 15
  • 33
1

On Windows, I solved this problem by putting the Lib directory of my Python installation directly on the PYTHONPATH. This is necessary even though, if you run Python directly in the same shell, it doesn't require this to find the encodings package:

set PYTHONPATH=C:\WinPython-64bit-3.4.4.6Qt5\python-3.4.4.amd64\Lib;%PYTHONPATH%
c:\apache24\bin\httpd.exe

Also be sure to use the correct mod_wsgi version. I downloaded mine from the Unofficial Binaries page and chose it to match the Python (64 bit 3.4 in the example above). My website works correctly with Python after the above change to the .bat file starting Apache.

Note my version of Python, WinPython, is installed by unzipping the install and placing it in the C: drive. Thus is it not installed in the Registry in the usual Windows way, which should minimize Registry interaction considerations.

Lars Ericson
  • 1,952
  • 4
  • 32
  • 45
  • That shouldn't be necessary. This may workaround issue for you, but is unlikely to fix the real problem. Are you sure mod_wsgi is even built from that Python installation? It is also possible your registry entries have been broken since Python was installed, or Python wasn't installed for all users as is needed if using mod_wsgi and Apache is run as a service. – Graham Dumpleton Jan 03 '18 at 22:29
  • WinPython isn't installed in the usual Windows way: You just drop it into the C: drive after unzipping it. The mod_wsgi was downloaded from the [Unofficial Binaries page](https://www.lfd.uci.edu/~gohlke/pythonlibs/) and chosen to match the Python (64 bit 3.4 in the example above). The website works correctly with Python after the fix. Figuring out why it did the job on Windows is beyond my Windows skill level. – Lars Ericson Jan 04 '18 at 01:12