librelist archives

« back to archive

Help integrating app into Apache

Help integrating app into Apache

From:
Robert Schweikert
Date:
2013-11-22 @ 23:02
Hi,

I need some help integrating my app into Apache. Was trying to follow 
the docs found here:

http://flask.pocoo.org/docs/deploying/mod_wsgi/

and here:

http://code.google.com/p/modwsgi/

but am still getting something wrong :(

My vhost file looks like this:

LoadModule wsgi_module /usr/lib64/apache2/mod_wsgi.so

NameVirtualHost *:80

<VirtualHost *:80>
     ServerName myapphost.com

     WSGIDaemonProcess regionInfo
     WSGIScriptAlias /regionInfo /srv/www/regionService/regionInfo.wsgi

     <Directory /srv/www/regionService>
         WSGIProcessGroup regionInfo
         WSGIApplicationGroup %{GLOBAL}
         Order deny,allow
         Allow from all
     </Directory>
</VirtualHost>

the wsgi script contains the following:

from regionInfo import app as application

and regionInfo.py lives in /usr/lib64/python2.6/site-packages

Thus there should be no need for me to fiddle with the path to load the 
module.

I can import the module into the python interpreter and am not getting 
any errors. Also running the application code works, i.e.

# python /usr/lib64/python2.6/site-packages/regionInfo.py

works, i.e. the test server is started and runs and I can reach it via 
127.0.0.1:5000

the apache2 process runs as root thus there should not be a permission 
issue.

I also added some debug code to write to a file in /tmp from 
/srv/www/regionService/regionInfo.wsgi and the file in /tmp is not created.

I also set the executable bit for /srv/www/regionService/regionInfo.wsgi
but that didn't help.

The apache2 error log contains no information.

I think I checked all the obvious places, well at least obvious to me.

Help is much appreciated.

Thanks,
Robert
-- 
Robert Schweikert                           MAY THE SOURCE BE WITH YOU
SUSE-IBM Software Integration Center                   LINUX
Tech Lead
Public Cloud Architect
rjschwei@suse.com
rschweik@ca.ibm.com
781-464-8147

Re: [flask] Help integrating app into Apache

From:
Gary Chambers
Date:
2013-11-23 @ 04:13
Robert,

I'm mostly new to Python web programming, but I had to resolve some similar
issues when I first tried to integrate my application into Apache.  My
application runs inside a virtualenv environment, so YMMV.  Including some
of what appears in your logs would be helpful, too.

> <VirtualHost *:80>
>     ServerName myapphost.com
>
>     WSGIDaemonProcess regionInfo
>     WSGIScriptAlias /regionInfo /srv/www/regionService/regionInfo.wsgi
>
>     <Directory /srv/www/regionService>
>         WSGIProcessGroup regionInfo
>         WSGIApplicationGroup %{GLOBAL}
>         Order deny,allow
>         Allow from all
>     </Directory>
> </VirtualHost>

Anyway, you may be missing some parameters in the WSGIDaemonProcess line.
I've modified what I have in my configuration file for what you provided in
yours.

WSGIDaemonProcess regionInfo user=httpd group=httpd threads=15 \
home=/srv/www/regionService python-path=/srv/www/regionService

--
G.

Re: [flask] Help integrating app into Apache

From:
Robert Schweikert
Date:
2013-11-23 @ 12:31
On 11/22/2013 11:13 PM, Gary Chambers wrote:
> Robert,
>
> I'm mostly new to Python web programming, but I had to resolve some similar
> issues when I first tried to integrate my application into Apache.  My
> application runs inside a virtualenv environment, so YMMV.  Including some
> of what appears in your logs would be helpful, too.

Well the logs are basically empty :(

# cat /var/log/apache2/error_log
[Sat Nov 23 12:18:30 2013] [warn] Init: Session Cache is not configured 
[hint: SSLSessionCache]
[Sat Nov 23 12:18:30 2013] [notice] Apache/2.2.12 (Linux/SUSE) 
mod_ssl/2.2.12 OpenSSL/0.9.8j-fips PHP/5.3.17 mod_wsgi/3.4 Python/2.6.8 
configured -- resuming normal operations

# cat /var/log/apache2/rcapache2.out
Syntax OK

And the access_log is of course empty since I am not trying to connect.
>
>> <VirtualHost *:80>
>>      ServerName myapphost.com
>>
>>      WSGIDaemonProcess regionInfo
>>      WSGIScriptAlias /regionInfo /srv/www/regionService/regionInfo.wsgi
>>
>>      <Directory /srv/www/regionService>
>>          WSGIProcessGroup regionInfo
>>          WSGIApplicationGroup %{GLOBAL}
>>          Order deny,allow
>>          Allow from all
>>      </Directory>
>> </VirtualHost>
>
> Anyway, you may be missing some parameters in the WSGIDaemonProcess line.
> I've modified what I have in my configuration file for what you provided in
> yours.
>
> WSGIDaemonProcess regionInfo user=httpd group=httpd threads=15 \
> home=/srv/www/regionService python-path=/srv/www/regionService

Well, my apache process runs as root and thanks to your hint I found out 
that wsgi does not run as root. Thus I created an httpd user and changed 
all the permissions accordingly, i.e.

/srv/www/regionService is owned by httpd as is 
/srv/www/regionService/regionInfo.wsgi

I still have the suspicion that the wsgi script is not even executed :(

The full content of the script is:

# cat /srv/www/regionService/regionInfo.wsgi
import sys
sys.stdout = sys.stderr
sys.path.append('/srv/www/regionService')
try:
     from regionInfo import app as application
except:
     f = open('/tmp/importFail.txt','w')
     f.write('regionInfo module import failed\n')
     f.close()

f = open('/tmp/import.txt','w')
f.write('tried to import regionInfo\n')
f.close()

print "Executing wsgi script"


Neither the files in /tmp are created nor do I get the message I am 
printing :( and of course

# python /srv/www/regionService/regionInfo.wsgi

works just fine.

I also set the LogLevel in the apache configuration file to debug, but 
still am not getting any useful hints.

# grep LogLevel /etc/apache2/httpd.conf
LogLevel debug


Other ideas about what's going on?

Thanks,
Robert


-- 
Robert Schweikert                           MAY THE SOURCE BE WITH YOU
SUSE-IBM Software Integration Center                   LINUX
Tech Lead
Public Cloud Architect
rjschwei@suse.com
rschweik@ca.ibm.com
781-464-8147

Re: [flask] Help integrating app into Apache

From:
Gary Chambers
Date:
2013-11-23 @ 20:45
Robert,

> I still have the suspicion that the wsgi script is not even executed :(
> The full content of the script is:

> # cat /srv/www/regionService/regionInfo.wsgi
> import sys
> sys.stdout = sys.stderr
> sys.path.append('/srv/www/regionService')
> try:
>     from regionInfo import app as application
> except:
>     f = open('/tmp/importFail.txt','w')
>     f.write('regionInfo module import failed\n')
>     f.close()
>
> f = open('/tmp/import.txt','w')
> f.write('tried to import regionInfo\n')
> f.close()
>
> print "Executing wsgi script"

Tangentially, unless you didn't add it to your Apache configuration, you can
probably remove the sys.path.append line.

What happens to output to stdout if you're reassigning sys.stderr to it?

What do you gain by wrapping the import in try/except?  It seems to me that
it doesn't matter if it fails gracefully.

Here's the (minimal) contents of my .wsgi file:

#!/usr/bin/env python

activate_this = '/apps/virtualenv/flask/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

from app import app as application

Does your web server respond with anything when you try to access the site?

--
Gary Chambers

Re: [flask] Help integrating app into Apache

From:
Robert Schweikert
Date:
2013-11-24 @ 01:04
Hi,

On 11/23/2013 03:45 PM, Gary Chambers wrote:
> Robert,
>
>> I still have the suspicion that the wsgi script is not even executed :(
>> The full content of the script is:
>
>> # cat /srv/www/regionService/regionInfo.wsgi
>> import sys
>> sys.stdout = sys.stderr
>> sys.path.append('/srv/www/regionService')
>> try:
>>      from regionInfo import app as application
>> except:
>>      f = open('/tmp/importFail.txt','w')
>>      f.write('regionInfo module import failed\n')
>>      f.close()
>>
>> f = open('/tmp/import.txt','w')
>> f.write('tried to import regionInfo\n')
>> f.close()
>>
>> print "Executing wsgi script"
>
> Tangentially, unless you didn't add it to your Apache configuration, you can
> probably remove the sys.path.append line.
>
> What happens to output to stdout if you're reassigning sys.stderr to it?
>
> What do you gain by wrapping the import in try/except?

My hope was that if there were any unexpected exceptions I'd at least 
get some output about the failure.

> It seems to me that
> it doesn't matter if it fails gracefully.
>
> Here's the (minimal) contents of my .wsgi file:
>
> #!/usr/bin/env python
>
> activate_this = '/apps/virtualenv/flask/bin/activate_this.py'
> execfile(activate_this, dict(__file__=activate_this))
>
> from app import app as application
>
> Does your web server respond with anything when you try to access the site?

Thanks for the pointers. I got a minimal sample app to work and now feel 
comfortable that I'll get the real thing to work shortly.

The problem in the end was patience ;)

With the small app I figured out that all the debug stuff only gets 
written when a request is received by Apache. Thus my app was acually 
running, I just didn't know. As I was not in the immediate position to 
test via web request and there was no output and the app log was not 
created I was under the impression that things had failed. WHen they 
didn't :D go figure.

Anyway,

Thanks gain for the pointers.
Robert
>
> --
> Gary Chambers
>


-- 
Robert Schweikert                           MAY THE SOURCE BE WITH YOU
SUSE-IBM Software Integration Center                   LINUX
Tech Lead
Public Cloud Architect
rjschwei@suse.com
rschweik@ca.ibm.com
781-464-8147