1

I'm running some Python code which uses urllib2.urlopen which works fine when run as the user, root (Linux).

However, it doesn't work when run as user apache (we're running it through apache version 1) - I get this error:

Traceback (most recent call last):
  File "/srv/www/vm/custom/ipn.py", line 118, in ?
    a = confirm_paypal(f, f1)
  File "/srv/www/vm/custom/ipn.py", line 47, in confirm_paypal
    response = urllib2.urlopen(PP_URL, params)
  File "/usr/lib64/python2.4/urllib2.py", line 130, in urlopen
    return _opener.open(url, data)
  File "/usr/lib64/python2.4/urllib2.py", line 358, in open
    response = self._open(req, data)
  File "/usr/lib64/python2.4/urllib2.py", line 376, in _open
    '_open', req)
  File "/usr/lib64/python2.4/urllib2.py", line 337, in _call_chain
    result = func(*args)
  File "/usr/lib64/python2.4/urllib2.py", line 1040, in https_open
    return self.do_open(httplib.HTTPSConnection, req)
  File "/usr/lib64/python2.4/urllib2.py", line 1006, in do_open
    raise URLError(err)
URLError: <urlopen error (13, 'Permission denied')>

Is there a way to give the user, apache, access to this function?

Update 1

Running Python 2.4.3 and Apache 2.2.3

2 Answers 2

1

I am not sure what the answer is, but I think I would try trouble shooting it like this.

Use su to change into the apache users context, and then run the python script with strace. I would guess that it is trying to access a file or create a temporary file or something somewhere.

If I was to make a wild guess I would see that it appears like you are trying to use https. Perhaps it is trying to load the CA certificates and doesn't have access?

It doesn't run as the apache user, will your python script run as a different non-root user?

3
  • Ah, ok, when using su apache, the script runs fine. The problem seems to occur when it's invoked through the apache server (running as apache user). Sep 15, 2009 at 1:42
  • I would look for differences in the environment. Check to see that PATH and TMP are the same. Check that apache is using the same version of python. Since the script runs for you, it sure seems like it should be working.
    – Zoredache
    Sep 15, 2009 at 6:15
  • Hmm, TMP isn't defined in either environments. And the two PATH values are slightly different - but can't spot anything wrong per say. Both Apache and the terminal are using the same versions of Python. I think it's a case of reconfiguring Apache, but can't think what. Sep 15, 2009 at 16:45
1

Turns out SELinux was stopping apache from using ports; after turning SELinux off, the problem went away.

2
  • 3
    Just had this issue. If anyone should wish to keep selinux running, the command setsebool -P httpd_can_network_relay=1 will fix this ...
    – EMiller
    Sep 1, 2010 at 20:42
  • Yes, please keep SELinux on. Don't just turn off security features because they are inconvenient.
    – siride
    Jan 15, 2018 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .