Internal server error while importing numpy from web.py while using mod_wsgi

853 views
Skip to first unread message

Bhaarat Sharma

unread,
Oct 11, 2013, 11:47:39 AM10/11/13
to mod...@googlegroups.com
I am trying to use numpy in a web.py script being used in mod_wsgi. However, if I try to "import numpy" I get an "Internal Server Error" with nothing in the error_log. I'm out of ideas of how to resolve this problem. I've following the installations instructions and ensured everything. 

I've verified that a simple web.py script without the 'import numpy' works perfectly fine. 

Here are the details of my setup:

Everything works fine from interpreter:

$ python
Python 2.7.3 (default, Oct  8 2013, 15:53:09)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> import numpy
>>> import sys
>>> sys.prefix
 '/usr/local'

I configured mod_wsgi like this:

./configure --with-python=/usr/local/bin/python2.7 --with-apxs=/usr/sbin/apxs
LD_RUN_PATH=/usr/local/lib/ make
sudo make install

Ensuring that mod_wsgi is configured properly:

$ ldd /usr/lib64/httpd/modules/mod_wsgi.so
    linux-vdso.so.1 =>  (0x00007fff36dff000)
    libpython2.7.so.1.0 => /usr/local/lib/libpython2.7.so.1.0 (0x00007f9462710000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f94624e8000)
    libdl.so.2 => /lib64/libdl.so.2 (0x00007f94622e4000)
    libutil.so.1 => /lib64/libutil.so.1 (0x00007f94620e1000)
    libm.so.6 => /lib64/libm.so.6 (0x00007f9461e5c000)
    libc.so.6 => /lib64/libc.so.6 (0x00007f9461ac9000)
    /lib64/ld-linux-x86-64.so.2 (0x00007f9462d15000)

Added the following to httpd.conf

WSGIPythonHome /usr/local
WSGIPythonPath /usr/local/lib/python2.7/site-packages/

Site-packages directory contains the following:

cv2.so
cv.py
distribute-0.6.35-py2.7.egg
easy-install.pth
numpy-1.7.1-py2.7-linux-x86_64.egg
README
setuptools-0.6c11-py2.7.egg-info
setuptools.pth
web.py-0.37-py2.7.egg

Here is my script:

import web
import json
import numpy as np

urls = (
   '.*', 'Sample'
)

class Sample:
     def GET(self):
       user_data = web.input()
       return json.dumps(self.perform(user_data.color, user_data.shade))

    def perform (self,color, shade):
        return {'color': color, 'shade': shade}

application = web.application(urls, globals()).wsgifunc()

I have the following files:

$ sudo find / -name libpython2.7.a
/usr/local/lib/python2.7/config/libpython2.7.a
/usr/local/lib/libpython2.7.a

$ sudo find / -name libpython2.7.so*
/usr/local/lib/libpython2.7.so
/usr/local/lib/libpython2.7.so.1.0
/usr/lib/libpython2.7.so
/usr/lib/libpython2.7.so.1.0

and my Python2.7 config directory has these files:

config.c
config.c.in
install-sh
libpython2.7.a
libpython2.7.so -> ../../libpython2.7.so
libpython2.7.so.1.0 -> ../../libpython2.7.so.1.0
Makefile
makesetup
python.o
Setup
Setup.config
Setup.local

I'll appreciate any help

Bhaarat Sharma

unread,
Oct 11, 2013, 1:34:09 PM10/11/13
to mod...@googlegroups.com
Additionally, Initially i did not install python with --enable-shared but later I did the following:

sudo make distclean
./configure --prefix=/usr/local --enable-shared
make && make altinstall

However still no good. 

Some more information:

>>> numpy.version.version
'1.7.1'
>>> import cv2
>>> cv2.__version__
'2.4.4'

Graham Dumpleton

unread,
Oct 11, 2013, 10:31:00 PM10/11/13
to mod...@googlegroups.com
Try adding to the Apache configuration:

WSGIApplicationGroup %{GLOBAL}

The bumpy module is from memory one which will not work in sub interpreters, so you need to force it to run in the main global interpreter, which is what the directive does.

I would also suggest you look into using daemon mode of mod_wsgi if you aren't already.

Graham

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To post to this group, send email to mod...@googlegroups.com.
Visit this group at http://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/groups/opt_out.

Bhaarat Sharma

unread,
Oct 12, 2013, 8:07:42 AM10/12/13
to mod...@googlegroups.com
Thanks for the reply. However, that did not work. I added the WSGIAplicationGroup to my httpd.conf like this:

WSGIPythonHome /usr/local
WSGIPythonPath /usr/local/lib/python2.7/site-packages/
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /processimages /var/www/webpy-app/service.py

However, the Internal server error is still there when I add "import numpy". Additionally, when I comment out "import numpy" and instead add "import cv2" I see an error in error_log that says "[error] ImportError: numpy.core.multiarray failed to import"

As a daemon service. I tried adding the following to my httpd.conf

WSGIPythonHome /usr/local
WSGIPythonPath /usr/local/lib/python2.7/site-packages/
WSGIApplicationGroup %{GLOBAL}
WSGIDaemonProcess /processimages user=bhaarat group=bhaarat processes=2 threads=25
WSGIProcessGroup /processimages
WSGIScriptAlias /processimages /var/www/webpy-app/service.py

However, after restarting the httpd service and trying to visit that page I got this error: "Service Temporarily Unavailable"

Thanks for help again

Bhaarat Sharma

unread,
Oct 12, 2013, 8:20:24 AM10/12/13
to mod...@googlegroups.com
I found that WSGIApplicationGroup should be under the <Directory tag. So now I have this in my httpd.conf

WSGIPythonHome /usr/local
WSGIPythonPath /usr/local/lib/python2.7/site-packages/
#WSGIDaemonProcess /processimages user=bhaarat group=bhaarat processes=2 threads=3 display-name=%{GROUP}
#WSGIProcessGroup /processimages
WSGIScriptAlias /processimages /var/www/webpy-app/service.py
Alias /processimages/static /var/www/webpy-app/static

<Directory /var/www/webpy-app/>
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

But this change did not help either ;(

Bhaarat Sharma

unread,
Oct 12, 2013, 9:51:13 AM10/12/13
to mod...@googlegroups.com
So I've even started WSGI as daemon process. I got over the "Service temporary unavailable" hurdle by going over the documentation. 

Finally I have the following in my httpd.conf

WSGISocketPrefix run/wsgi
WSGIDaemonProcess /processimages user=bhaarat group=bhaarat processes=20 threads=30 display-name=%{GROUP}
WSGIProcessGroup /processimages
WSGIPythonHome /usr/local
WSGIPythonPath /usr/local/lib/python2.7/site-packages/
WSGIScriptAlias /processimages /var/www/webpy-app/service.py
#Alias /processimages/static /var/www/webpy-app/static

....
<Directory /var/www/webpy-app/>
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

Now, putting "import numpy" results in the following errors:

In error_log: 

[alert] (13)Permission denied: mod_wsgi (pid=8396): Unable to change working directory to '/home/bhaarat'.

In ssl_error_log:

[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128] mod_wsgi (pid=8380): Target WSGI script '/var/www/webpy-app/service.py' cannot be loaded as Python module.
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128] mod_wsgi (pid=8380): Exception occurred processing WSGI script '/var/www/webpy-app/service.py'.
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128] Traceback (most recent call last):
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]   File "/var/www/webpy-app/service.py", line 11, in <module>
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]     import numpy
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]   File "/usr/local/lib/python2.7/site-packages/numpy-1.7.1-py2.7-linux-x86_64.egg/numpy/__init__.py", line 155, in <module>
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]     import ctypeslib
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]   File "/usr/local/lib/python2.7/site-packages/numpy-1.7.1-py2.7-linux-x86_64.egg/numpy/ctypeslib.py", line 60, in <module>
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]     import ctypes
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]   File "/usr/local/lib/python2.7/ctypes/__init__.py", line 555, in <module>
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]     _reset_cache()
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]   File "/usr/local/lib/python2.7/ctypes/__init__.py", line 279, in _reset_cache
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128]     CFUNCTYPE(c_int)(lambda: None)
[Sat Oct 12 13:44:49 2013] [error] [client 10.10.41.128] MemoryError


I'm starting httpd as "sudo service httpd start"

Bhaarat Sharma

unread,
Oct 12, 2013, 11:08:10 AM10/12/13
to mod...@googlegroups.com
Sorry for so many messages but I'm trying to share as much info as possible to get over this hurdle. 

I have been able to resolve the "Unable to change working directory to '/home/bhaarat'." error in error_log by specifying a home directory in WSGIDaemonProcess like this:

WSGIDaemonProcess /processimages user=bhaarat group=bhaarat processes=2 threads=3 home=/var/www/webpy-app/ display-name=%{GROUP}

However, when I access the url: curl --insecure "https://localhost/processimages?color=red&shade=dark" I still get internal server error and the error I posted earlier in ssl_error_log:

[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128] mod_wsgi (pid=10910): Target WSGI script '/var/www/webpy-app/service.py' cannot be loaded as Python module.
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128] mod_wsgi (pid=10910): Exception occurred processing WSGI script '/var/www/webpy-app/service.py'.
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128] Traceback (most recent call last):
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]   File "/var/www/webpy-app/service.py", line 11, in <module>
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]     import numpy
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]   File "/usr/local/lib/python2.7/site-packages/numpy-1.7.1-py2.7-linux-x86_64.egg/numpy/__init__.py", line 155, in <module>
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]     import ctypeslib
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]   File "/usr/local/lib/python2.7/site-packages/numpy-1.7.1-py2.7-linux-x86_64.egg/numpy/ctypeslib.py", line 60, in <module>
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]     import ctypes
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]   File "/usr/local/lib/python2.7/ctypes/__init__.py", line 555, in <module>
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]     _reset_cache()
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]   File "/usr/local/lib/python2.7/ctypes/__init__.py", line 279, in _reset_cache
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128]     CFUNCTYPE(c_int)(lambda: None)
[Sat Oct 12 15:00:54 2013] [error] [client 10.10.41.128] MemoryError

Bhaarat Sharma

unread,
Oct 12, 2013, 1:16:17 PM10/12/13
to mod...@googlegroups.com
I finally got this working. 

There is good discussion on this error here: https://bugzilla.redhat.com/show_bug.cgi?id=645193

"the problem can be bypassed by telling SELinux to allow httpd to execute code from the tmp directory, for which there's apparently now a special flag. I haven't tried myself, but it looks like the command
setsebool httpd_tmp_exec on"

allowing httpd to execute code on tmp directory, in addition to other things I've tried, solved my problem. Now I can import numpy and cv2 both. 

Graham Dumpleton

unread,
Oct 13, 2013, 8:10:45 PM10/13/13
to mod...@googlegroups.com
On 13/10/2013, at 12:51 AM, Bhaarat Sharma <bhaa...@gmail.com> wrote:

So I've even started WSGI as daemon process. I got over the "Service temporary unavailable" hurdle by going over the documentation. 

Finally I have the following in my httpd.conf

WSGISocketPrefix run/wsgi
WSGIDaemonProcess /processimages user=bhaarat group=bhaarat processes=20 threads=30 display-name=%{GROUP}
WSGIProcessGroup /processimages
WSGIPythonHome /usr/local
WSGIPythonPath /usr/local/lib/python2.7/site-packages/

You shouldn't need this WSGIPythonPath directive. That directory should be added automatically by Python for that Python installation.

Graham
Reply all
Reply to author
Forward
0 new messages