librelist archives

« back to archive

Help with 1st time mod_wsgi config

Help with 1st time mod_wsgi config

From:
Andreas Christoffersen
Date:
2012-07-14 @ 15:25
So I have some trouble getting my flask app accesible on the interwebs. The
server simply return a 404 not found. - So I guess this is an apache
problem. I am also sure I have made a fairly obvious config mistake
somewhere. So PLEASE help me mend my ways :-)

Below some info about me flawed setup:

Thanks in advance
Sincerely
Andreas

*Info -scenario*

The flask app is a single module: pol_pol.py. This module is inside a dir
called "politikere-i-medierne". From pol_pol.py the app is started like so:

if __name__ == "__main__":
    app.run(host= '0.0.0.0', debug=False)



- I have the dns set up for mediepol.andreas.io

- On server, this folder structure:
/var/www/public_html/mediepol.andreas.io/venv/
/var/www/public_html/mediepol.andreas.io/mediepol.wsgi
/var/www/public_html/mediepol.andreas.io/politikerne-i-medierne/  <- Cloned
flask repo
/var/www/public_html/mediepol.andreas.io/politikerne-i-medierne/pol_pol.py

- sudo pip install -r requirements.txt
- the app is called pol_pol.py and is in the "politikere-i-medierne" folder

*mediepol.wsgi is as follows:*

activate_this = 'venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))
from pol_pol import app as application


*Apache sites-enabled:*

<VirtualHost *:80>

ServerName mediepol.andreas.io

ServerAlias www.mediepol.andreas.io


WSGIDaemonProcess pol_pol.py threads=5

WSGIScriptAlias / /var/www/mediepol.andreas.io/mediepol.wsgi


<Directory /var/www/public_html/mediepol.andreas.io/politikere-i-medierne>

WSGIProcessGroup pol_pol.py

WSGIApplicationGroup %{GLOBAL}

Order deny,allow

Allow from all

</Directory>

</VirtualHost>

Re: [flask] Help with 1st time mod_wsgi config

From:
Jim Gumbley
Date:
2012-07-14 @ 15:42
Both Flask and Apache are listening on port 80. Trying moving Flask onto 
another and adjust the Apache config.

Sent from mobile device.

On 14 Jul 2012, at 16:25, Andreas Christoffersen 
<achristoffersen@gmail.com> wrote:

> So I have some trouble getting my flask app accesible on the interwebs. 
The server simply return a 404 not found. - So I guess this is an apache 
problem. I am also sure I have made a fairly obvious config mistake 
somewhere. So PLEASE help me mend my ways :-)
> 
> Below some info about me flawed setup:
> 
> Thanks in advance
> Sincerely
> Andreas
> 
> Info -scenario
> 
> The flask app is a single module: pol_pol.py. This module is inside a 
dir called "politikere-i-medierne". From pol_pol.py the app is started 
like so:
> 
> if __name__ == "__main__":
>     app.run(host= '0.0.0.0', debug=False)
> 
> 
> - I have the dns set up for mediepol.andreas.io
> 
> - On server, this folder structure:
> /var/www/public_html/mediepol.andreas.io/venv/
> /var/www/public_html/mediepol.andreas.io/mediepol.wsgi
> /var/www/public_html/mediepol.andreas.io/politikerne-i-medierne/  <- 
Cloned flask repo 
> /var/www/public_html/mediepol.andreas.io/politikerne-i-medierne/pol_pol.py
> 
> - sudo pip install -r requirements.txt
> - the app is called pol_pol.py and is in the "politikere-i-medierne" folder
> 
> mediepol.wsgi is as follows:
> activate_this = 'venv/bin/activate_this.py'
> execfile(activate_this, dict(__file__=activate_this))
> from pol_pol import app as application
> 
> Apache sites-enabled:
> <VirtualHost *:80>
> ServerName mediepol.andreas.io
> ServerAlias www.mediepol.andreas.io
> 
> WSGIDaemonProcess pol_pol.py threads=5
> WSGIScriptAlias / /var/www/mediepol.andreas.io/mediepol.wsgi
> 
> <Directory /var/www/public_html/mediepol.andreas.io/politikere-i-medierne>
> WSGIProcessGroup pol_pol.py
> WSGIApplicationGroup %{GLOBAL}
> Order deny,allow
> Allow from all
> </Directory>
> </VirtualHost>
> 
> 
> 
> 
> 
> 

Re: [flask] Help with 1st time mod_wsgi config

From:
Andreas Christoffersen
Date:
2012-07-14 @ 15:55
Thanks Jim!!

Apache now returns the classic "It works" page - it doesn't serve the flask
app... Any ideas?

ps  - for clarity:

I now have (in pol_pol.py):
if __name__ == "__main__":$

     app.run(host= '0.0.0.0', port='5000', debug=False)

and on my sites-available:
<VirtualHost *:5000>$



On Sat, Jul 14, 2012 at 5:42 PM, Jim Gumbley <j.gumbley@gmail.com> wrote:

> Both Flask and Apache are listening on port 80. Trying moving Flask onto
> another and adjust the Apache config.
>
>

Re: [flask] Help with 1st time mod_wsgi config

From:
Jim Gumbley
Date:
2012-07-14 @ 16:02
You need apache to listen on port 80 and reverse proxy to 5000 if that 
makes sense.

Sent from mobile device.

On 14 Jul 2012, at 16:55, Andreas Christoffersen 
<achristoffersen@gmail.com> wrote:

> Thanks Jim!!
> 
> Apache now returns the classic "It works" page - it doesn't serve the 
flask app... Any ideas?
> 
> ps  - for clarity:
> 
> I now have (in pol_pol.py):
> if __name__ == "__main__":$

>      app.run(host= '0.0.0.0', port='5000', debug=False)
> 
> and on my sites-available:
> <VirtualHost *:5000>$ 
> 
> 
> 
> On Sat, Jul 14, 2012 at 5:42 PM, Jim Gumbley <j.gumbley@gmail.com> wrote:
> Both Flask and Apache are listening on port 80. Trying moving Flask onto
another and adjust the Apache config.
> 

Re: [flask] Help with 1st time mod_wsgi config

From:
Audrius Kažukauskas
Date:
2012-07-14 @ 16:38
On Sat, 2012-07-14 at 17:25:00 +0200, Andreas Christoffersen wrote:
> The flask app is a single module: pol_pol.py. This module is inside a dir
> called "politikere-i-medierne". From pol_pol.py the app is started like so:
> 
> if __name__ == "__main__":
>     app.run(host= '0.0.0.0', debug=False)

Maybe I misunderstood, but you shouldn't be running your app using
embedded Flask server.  That's what mod_wsgi is going to be used for.
If it really is running as a separate process, stop it, it won't be
needed.

> activate_this = 'venv/bin/activate_this.py'

Most likely this should be a full path to venv.

> WSGIDaemonProcess pol_pol.py threads=5
> WSGIScriptAlias / /var/www/mediepol.andreas.io/mediepol.wsgi

This path doesn't match the one you listed earlier:

  /var/www/public_html/mediepol.andreas.io/mediepol.wsgi

I suspect that it's your main problem.  Check Apache logs for error
messages.

> <Directory /var/www/public_html/mediepol.andreas.io/politikere-i-medierne>
> WSGIProcessGroup pol_pol.py

I think WSGIDaemonProcess and WSGIProcessGroup directives use first
argument to name mod_wsgi daemon process, it doesn't need to be a
filename.  Feel free to use any descriptive name here.

HTH,
-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] Help with 1st time mod_wsgi config

From:
Andreas Christoffersen
Date:
2012-07-14 @ 17:14
@Jim - It doesn make much sense ;-)  but I am googling and see something
like, "ProxyPassReverse" but don't know exactly what to do with it.

I have several php apps on the server, if this helps?

@Audrius - thanks for the help.


> Maybe I misunderstood, but you shouldn't be running your app using
> embedded Flask server.  That's what mod_wsgi is going to be used for.
> If it really is running as a separate process, stop it, it won't be
> needed.
>

That sound reasonable I guess - but I am (trying to) follow the docs:
http://flask.pocoo.org/docs/deploying/mod_wsgi/ and I don't see any hints
about this? How can I call the app in any other way? As per Jims reply I
thought apache would act as a proxy, handeling the requests, and then
sending those to the flask/werkzeug server?


> > activate_this = 'venv/bin/activate_this.py'
>
> Most likely this should be a full path to venv.
>

Changed - thanks.

>
> > WSGIDaemonProcess pol_pol.py threads=5
> > WSGIScriptAlias / /var/www/mediepol.andreas.io/mediepol.wsgi
>
> This path doesn't match the one you listed earlier:
>
>   /var/www/public_html/mediepol.andreas.io/mediepol.wsgi
>
> I suspect that it's your main problem.  Check Apache logs for error
> messages.
>
> Stupid me - thats fixed. But Apache still shows "It works.
/var/log/apache2/error.log has something like:

[Sat Jul 14 18:50:49 2012] [notice] Apache/2.2.16 (Debian)
PHP/5.3.3-7+squeeze13 with Suhosin-Patch proxy_html/3.0.1 mod_ssl/2.2.16
OpenSSL/0.9.8o mod_wsgi/3.3 Python/2.6.6 configured -- resuming normal
operations



> > <Directory /var/www/public_html/
> mediepol.andreas.io/politikere-i-medierne>
> > WSGIProcessGroup pol_pol.py
>
> I think WSGIDaemonProcess and WSGIProcessGroup directives use first
> argument to name mod_wsgi daemon process, it doesn't need to be a
> filename.  Feel free to use any descriptive name here
>

Thanks.

I am beginning to feel completely lost. What ever the learning experience
will be from this, I think I should apply some of it to the docs...

Re: [flask] Help with 1st time mod_wsgi config

From:
Audrius Kažukauskas
Date:
2012-07-14 @ 18:03
On Sat, 2012-07-14 at 19:14:22 +0200, Andreas Christoffersen wrote:
> @Jim - It doesn make much sense ;-)  but I am googling and see something
> like, "ProxyPassReverse" but don't know exactly what to do with it.

What Jim suggested is another way to set everything up, but it's not
related to mod_wsgi.  It's what you described below, when Apache acts as
a proxy and pass the requests to Flask server.

> That sound reasonable I guess - but I am (trying to) follow the docs:
> http://flask.pocoo.org/docs/deploying/mod_wsgi/ and I don't see any hints
> about this? How can I call the app in any other way? As per Jims reply I
> thought apache would act as a proxy, handeling the requests, and then
> sending those to the flask/werkzeug server?

Well, the docs do have a hint right in the beginning, where it warns not
to call app.run().

Your app will be called by mod_wsgi, which is a WSGI server.  It is
called from .wsgi file, where you import your app object as application.
That's what mod_wsgi is expecting and it will take it from there.

> Stupid me - thats fixed. But Apache still shows "It works.
> /var/log/apache2/error.log has something like:
> 
> [Sat Jul 14 18:50:49 2012] [notice] Apache/2.2.16 (Debian)
> PHP/5.3.3-7+squeeze13 with Suhosin-Patch proxy_html/3.0.1 mod_ssl/2.2.16
> OpenSSL/0.9.8o mod_wsgi/3.3 Python/2.6.6 configured -- resuming normal
> operations

That looks like a harmless notice.

Sorry, but I can't help you more, as I have little experience with
Apache and mod_wsgi.  I can only guess that something is not right with
your VirtualHost, because Apache is returning a welcome page and there
are no mod_wsgi related errors in the log.  That is, mod_wsgi doesn't
even see the requests.

You can also try using pstree or htop in command line to see if httpd
has child processes that belong to mod_wsgi (they should be named
accordingly if memory doesn't fail me).

> I am beginning to feel completely lost. What ever the learning experience
> will be from this, I think I should apply some of it to the docs...

I guess Flask mod_wsgi page is written as a starting point, and one is
expected to read mod_wsgi docs for more detailed explanations.

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] Help with 1st time mod_wsgi config

From:
Andreas Christoffersen
Date:
2012-07-14 @ 22:31
thank you Audrius - It all makes so much more sense to me now. The app was
in a "if __name__ = __main__:" condition though. So I did read the warning.
I just didn't understand it :-)


> Sorry, but I can't help you more, as I have little experience with
> Apache and mod_wsgi.  I can only guess that something is not right with
> your VirtualHost, because Apache is returning a welcome page and there
> are no mod_wsgi related errors in the log.  That is, mod_wsgi doesn't
> even see the requests.
>
>
I changed the port for the virtual host back to 80, and now I get an
"Internal server error". Feels like a victory :-)

The errolog tells me that mod_wsgi can't find the module "pol_pol"

[Sat Jul 14 23:59:04 2012] [error] [client 93.161.97.190]   File
"/var/www/public_html/mediepol.andreas.io/mediepol.wsgi", line 3, in
<module>
[Sat Jul 14 23:59:04 2012] [error] [client 93.161.97.190]     from pol_pol
import app as application
[Sat Jul 14 23:59:04 2012] [error] [client 93.161.97.190] ImportError: No
module named pol_pol


I have tried to make the .wsgi file find the pol_pol module. The change I
had my highest hopes for was adding a sys.path:

activate_this = '/var/www/public_html/
mediepol.andreas.io/venv/bin/activate_this.py'$
execfile(activate_this, dict(__file__=activate_this))$
import sys$
sys.path.insert(0, 'var/www/public_html/
mediepol.andreas.io/politikere-i-medierne/')$

from pol_pol import app as application$

But still no cigar. Tomorrow I will read up on the mod_wsgi documentation:
https://code.google.com/p/modwsgi/wiki/VirtualEnvironments -but as it is
now 00:30 in Denmark, I'll say goodnight. Thank you all for helping a
newcomer!!! - I hope I can manage from here on out!

Sincerely
Andreas

Re: [flask] Help with 1st time mod_wsgi config

From:
Audrius Kažukauskas
Date:
2012-07-15 @ 08:34
On Sun, 2012-07-15 at 00:31:27 +0200, Andreas Christoffersen wrote:
> I have tried to make the .wsgi file find the pol_pol module. The change I
> had my highest hopes for was adding a sys.path:
> 
> activate_this = 
'/var/www/public_html/mediepol.andreas.io/venv/bin/activate_this.py'
> execfile(activate_this, dict(__file__=activate_this))
> import sys
> sys.path.insert(0, 
'var/www/public_html/mediepol.andreas.io/politikere-i-medierne/')

Leading / is missing in the path, that's why this has no effect.

> But still no cigar. Tomorrow I will read up on the mod_wsgi documentation:
> https://code.google.com/p/modwsgi/wiki/VirtualEnvironments -but as it is
> now 00:30 in Denmark, I'll say goodnight. Thank you all for helping a
> newcomer!!! - I hope I can manage from here on out!

Glad we could help.  :-)

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] Help with 1st time mod_wsgi config

From:
Andreas Christoffersen
Date:
2012-07-15 @ 11:14
>
>
> Leading / is missing in the path, that's why this has no effect.
>
> Oh - I am blind... sorry -- should have found that myself!


> Glad we could help.  :-)
>
> So am I... But for the last two hours I've been confounded by this error
message: "The server encountered an internal error and was unable to
complete your request. Either the server is overloaded or there is an error
in the application."

The app works locally. The app works remotely (i think) in so far as I can
call it with python pol_pol.py (the server starts, but since I am on a VPS
I don't think I can actually test it on localhost:5000).

For a second I was joyfull as I thought the problem was a print statement
(mod_wsgi wont allow that) - but even with that print statement removed,
the error is persistent.

I run python 2.7.1 locally and 2.6.6 remotely - but that shouldn't affect
anything. pol_pol.py code is here:

https://bitbucket.org/achristoffersen/politikere-i-medierne/src/b555deb96be3/pol_pol.py

I see nothing in /var/log/apache2/error.log

if I run an interactive python session on the server, I can import pol_pol
and run pol_pol.get_count_pol.

I get an error however if I try to run pol_pol.count_pol():

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "pol_pol.py", line 59, in count_pol
    inf_hts =  count_dict['Information']['HTS']
  File "/usr/local/lib/python2.6/dist-packages/flask/templating.py", line
121, in render_template
    ctx.app.update_template_context(context)
AttributeError: 'NoneType' object has no attribute 'app'


But I guess thats because I am not running the function as part of a flask
instance.

Sorry for all these questions!!!

Sincerely
Andreas

Re: [flask] Help with 1st time mod_wsgi config

From:
Audrius Kažukauskas
Date:
2012-07-15 @ 12:47
On Sun, 2012-07-15 at 13:14:46 +0200, Andreas Christoffersen wrote:
> So am I... But for the last two hours I've been confounded by this error
> message: "The server encountered an internal error and was unable to
> complete your request. Either the server is overloaded or there is an error
> in the application."
> 
> The app works locally. The app works remotely (i think) in so far as I can
> call it with python pol_pol.py (the server starts, but since I am on a VPS
> I don't think I can actually test it on localhost:5000).

If there's no firewall blocking port 5000, you can access your webapp
via http://vps.hostname.or.ip:5000/ URL.  Alternatively you can make SSH
tunnel to your webapp

  $ ssh -N -L 5000:localhost:5000 vps-user@vps-hostname

and open it via usual http://localhost:5000/

> I run python 2.7.1 locally and 2.6.6 remotely - but that shouldn't affect
> anything. pol_pol.py code is here:
> 
https://bitbucket.org/achristoffersen/politikere-i-medierne/src/b555deb96be3/pol_pol.py

One potential problem could be your app trying to read (and later write)
pickled dict from file.  Absolute path may be needed, or there is a
problem with file/directory permissions.

Try adding Flask error handler to see what exception you're getting:

  @app.errorhandler(500)
  def error_500(error):
      from traceback import format_exc
      return format_exc(), 500, {'Content-Type': 'text/plain'}

-- 
Audrius Kažukauskas
http://neutrino.lt/

Re: [flask] Help with 1st time mod_wsgi config

From:
Andreas Christoffersen
Date:
2012-07-15 @ 14:59
Audrius!!!! Thank you so much!

The errorhandler identified just what you suspected! I have added an
absolute path to the pickled object. Now everything works... Thank you VERY
much!!!

Andreas

On Sun, Jul 15, 2012 at 2:47 PM, Audrius Kažukauskas <audrius@neutrino.lt>wrote:

>   @app.errorhandler(500)
>   def error_500(error):
>       from traceback import format_exc
>       return format_exc(), 500, {'Content-Type': 'text/plain'}
>