alexwatever
alexwatever

Reputation: 594

Python/Flask app using apache/fastcgi producing 500 Error

I've been doing Flask microblog tutorial by Miguel Grinberg and have got stuck on trying to deploy to my linux VPS.(http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-xvii-deployment-on-linux-even-on-the-raspberry-pi)

I'm getting a 500 internal server error produced by apache, not flask, and I can't figure it out. It works when running it using the interpreter, but I can't launch it with apache. I've read through so many google searches and SO questions and I'm lost. I'm relatively new to linux/python/flask but I'm willing to learn if someone can point me in the right direction.

Setup:

I'm running a fresh CentOS 6.6 install with Python 2.6.6 and Apache 2.2.15. I'm running it off sqlite. I'm using these flask modules if you're interested: http://pastebin.com/bPnH83bs

Basic App Structure: (left out things for brevity)

Located in: /home/apps/portal

I have the whole directory chown'd to: chown -R apps:apache

User 'apps' is a member of the apache group

flask\ (virtualenv)
app\
  static\
  templates\
  __init__.py
  models.py
  views.py
  forms.py
  decorators.py
db_repository\
search.db\
tmp\
app.db
config.py
run.py
runp.py
runp-sqlite.fcgi


Apache conf file setup:

FcgidIPCDir /tmp
AddHandler fcgid-script .fcgi
<VirtualHost *:80>
    DocumentRoot /home/apps/portal/app/static
    Alias /static /home/apps/portal/app/static
    ScriptAlias / /home/apps/portal/runp-sqlite.fcgi/
    ErrorLog /var/log/httpd/error_log
    CustomLog /var/log/httpd/access_log combined
</VirtualHost>


runp-sqlite.fcgi Contents:

#!flask/bin/python
from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()


Error from apache logs when trying to access page and getting 500 error:

[Mon Dec 15 22:21:44 2014] [warn] [client *.*.*.*] mod_fcgid: read data timeout in 40 seconds
[Mon Dec 15 22:21:44 2014] [error] [client *.*.*.*] Premature end of script headers: runp-sqlite.fcgi


Error when I run "runp-sqlite.fcgi" from the console:

[root@**** portal]# sudo -u apache ./runp-sqlite.fcgi
Traceback (most recent call last):
    File "./runp-sqlite.fcgi", line 6, in <module>
        WSGIServer(app).run()
    File "/home/apps/portal/flask/lib/python2.6/site-packages/flipflop.py", line 938, in run
        sock.getpeername()
socket.error: [Errno 88] Socket operation on non-socket


Things I've checked:

Sorry for the wall of text, I just don't know what you'll need to see. If anyone can help with this I'll be really greatful. If it's something dumb, I apologise. :)

Update 1:

Changed runp-sqlite.fcgi to call virtualenv:

#!flask/bin/python

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

from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()

Now apache errors_log has a new error message:

[Fri Dec 19 13:43:03 2014] [notice] Apache/2.2.15 (Unix) DAV/2 mod_fcgid/2.3.9 PHP/5.3.3 mod_wsgi/3.2 Python/2.6.6 configured -- resuming normal operations
[Fri Dec 19 13:43:05 2014] [warn] [client 110.143.38.80] (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
[Fri Dec 19 13:43:05 2014] [error] [client 110.143.38.80] Premature end of script headers: runp-sqlite.fcgi

Upvotes: 2

Views: 1702

Answers (1)

Moses Schwartz
Moses Schwartz

Reputation: 7149

Do you have Flask installed in a virtuelenv? If so, the problem may be that your WSGI file isn't activating it. This causes an ImportError or something similar when Apache tries to serve the site, which results in a not-very-helpful 500 Error.

The fix is to activate the virtualenv in your WSGI file before importing the app like this:

#!/bin/python

VENV_DIR = 'your_app/your_venv'
activate_this = os.path.join(VENV_DIR, 'bin', 'activate_this.py')
execfile(activate_this, dict(__file__=activate_this))

from flipflop import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app).run()

See also this previous SO question: Running Python from a virtualenv with Apache/mod_wsgi, on Windows

Upvotes: 2

Related Questions