24

My Django app on my production server hosted on Webfaction was working fine until I just tried to restart it after pushing a change to the settings.py file. I ran

apache2/bin/restart

as usual. Then I tried to access my app on my browser, and I got a 504 Gateway timeout. I looked into the mod_wsgi logs and saw this:

[Thu Nov 03 23:46:53.605625 2016] [wsgi:error] [pid 8027:tid 139641332168448]
[client 127.0.0.1:34570] Timeout when reading response headers from daemon 
process 'myapp' : /home/<me>/webapps/<myapp>/<ProjectName>/<myapp>/wsgi.py

What does this mean and how do I fix it? The only thing I changed in the settings.py file was moving some variable names around. I can still successfully interact with the app with

python2.7 manage.py shell

But I can't get to it on the web, nor use the API.

EDIT: Here's my wsgi.py file:

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "<myapp>.settings")

application = get_wsgi_application()
Nick
  • 1,864
  • 5
  • 27
  • 49
  • 1
    Can you post some version info about apache/mod_wsgi - those looks like request timeouts or socket timeouts. Can you run that settings file locally? – theWanderer4865 Nov 04 '16 at 00:17
  • I'm pretty unfamiliar with mod_wsgi, how do you get the version info? It's just a directory apache2/, with some start scripts – Nick Nov 04 '16 at 00:20
  • I added my wsgi.py file, if that helps – Nick Nov 04 '16 at 00:33
  • 1
    Were you able to start your app locally? I would recommend having a production like setup locally or a staging environment to experiment with the setup. If you're not running anything that gets outside traffic yet then you'd probably be okay messing with config stuff (just don't use debug mode on your prod server) – theWanderer4865 Nov 04 '16 at 01:01
  • This actually is my "staging" setup. It's running on the Webfaction servers, but it isn't the 'real' web app, it's one I use to test things exactly like this. – Nick Nov 04 '16 at 01:30

6 Answers6

42

Python C extension modules, like numpy, are known to cause timeouts when used under mod_wsgi. There's a clear explanation of the problem (direct from the author of mod_wsgi) available at https://serverfault.com/a/514251/109598

If that sounds like it might be the cause of your problem, then the solution is probably simple - add the following to your httpd.conf:

WSGIApplicationGroup %{GLOBAL}

Be sure to restart your Apache instance after making that change.

Community
  • 1
  • 1
Sean F
  • 817
  • 9
  • 20
  • 1
    Wow that worked! Thank you, I couldn't find that anywhere. Yeah I just recently added Numpy to my project, so this might be the cause. – Nick Nov 04 '16 at 02:08
  • Worked for me too. Python 3.5.1, Ubuntu 14.04.3 LTS, Apache 2.4.7 with mod_wsgi. I import both pandas and numpy. – CoderGuy123 Apr 30 '17 at 18:19
  • 1
    I have seen this answer in different questions but nobody explains what do that change. – Nazkter Sep 29 '17 at 21:05
  • 1
    Worked on Ubuntu 17.10, Apache 2.4.27. If it's helpful, the httpd.conf file may be named something else, like 000-default.conf, and for me is in apache2/sites-enabled. – Tunn Mar 13 '18 at 03:20
  • 1
    Did you mean to edit `apache2.conf`? I don't think anyone uses `httpd.conf` anymore, which doesn't exist on Ubuntu or Debian. – Cerin May 30 '18 at 15:30
  • It works for me, especially because my django web app uses an `sklearn` model. Thanks a lot. – lenhhoxung Jul 23 '18 at 09:42
  • 1
    Another reason might be the limitation of the server. When I use a better server (more memory, more space, etc.), the problem gone. – lenhhoxung Jul 23 '18 at 12:57
  • I have this error come in random and crash my site. I just did what you said (+ extended timeout) and I don't know if it works. Do you have any idea on how I could try to reproduce the error ? – Sam May 03 '20 at 10:44
  • So I was told that that is what enables running in embedded mode, as opposed to daemon mode, which is the suggested way of running mod_wsgi. Would this force me to use embedded mode? How can I use Django, with daemon mode, and still use libraries I need, like OpenCV, Numpy, Boto3 etc? – Jonathan Ma Jan 29 '21 at 18:42
  • I updated the same at the end of ```/etc/apache2/apache2.conf``` and It fixed the issue. Thank You – Abhi Aug 20 '21 at 10:22
7

Try increasing Timeout directive in httpd.conf, which defaults to 60 seconds in Apache 2.4. For example:

TimeOut 600
tuomastik
  • 4,559
  • 5
  • 36
  • 48
  • I have this error come in random and crash my site. I just did what you said and I don't know if it works. Do you have any idea on how I could try to reproduce the error ? – Sam May 03 '20 at 10:44
7

Here is how I was able to find the root cause of my issue.

python manage.py showmigrations

My app could not reach the database server, so it would eventually time out. Running manage.py I could see see the error message on the console.

Marston
  • 101
  • 1
  • 4
0

In my case (Python 3.6), the mimetypes module caused this problem. I did not further investigate this, but removing a call to mimetypes.guess_type solved the problem. The call was made in the related Django view function.

Markus
  • 2,412
  • 29
  • 28
0

I hit the same problem because the home directory of the user under which the wsgi process was running had became unavailable at some point during the server upgrade.

This might help someone.

phep
  • 531
  • 5
  • 8
0

Thank you to lenhhoxung who in the comments to one of the other solutions mentioned upgrading server capabilities. I had been successfully running a demo site on an AWS EC2 Nano instance for a long time, but for some reason it suddenly started erroring out on one page that has some complex computations. I upgraded it to an AWS EC2 Micro instance, problem solved. I think this is worthy of its own answer here considering this took a good chunk of a day. All credit to lenhhoxung, though! Thanks!

Scott
  • 526
  • 6
  • 10