2

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:

  • I've disabled SELinux as it was causing a different problem, didn't fix this issue.
  • Checked that folders were chown'd to the correct user/group.
  • Checked that '/etc/httpd/conf/httpd.conf' and '/etc/sysconfig/httpd' PIDFILE locations are correct
  • Checked that iptables is accepting traffic to ports 80 and 5000(for testing). If I remove the apache configuration I've added I am successfully serving from /var/www/html
  • Lots and lots of googling and playing around

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
alexwatever
  • 576
  • 1
  • 7
  • 20
  • Hello, have you solved this problem? Please post an answer if so! (I'm facing the same problem now...) – nalzok Aug 20 '17 at 13:04
  • @SunQingyao Sorry mate this was too long ago I can't remember how I fixed it or what the cause was. I now do all my work with nginx and it tends to go much smoother then apache, especially for smaller projects like this. – alexwatever Aug 22 '17 at 06:01
  • Thanks for your responding, will try Nginx! – nalzok Aug 22 '17 at 06:10

1 Answers1

2

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

Community
  • 1
  • 1
Moses Schwartz
  • 6,879
  • 3
  • 21
  • 11
  • 1
    Thanks Moses you're a legend, I'm one step closer. I'm still getting the 500 error but now a new 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`
    – alexwatever Dec 19 '14 at 03:45
  • That error probably means that there is still an error in your application when run in Apache's context, but it can get pretty tricky to debug. As a first step, I suggest replacing all of your application code with a simple "Hello, World!" app first, and making sure that gets served through Apache/FastCGI correctly. – Moses Schwartz Dec 19 '14 at 19:30
  • Good thinking Moses, ill do that next. Thanks for the help mate. – alexwatever Dec 19 '14 at 22:05