3

I have a django website using Apache and mod_wsgi. Since the last 4-5 days I keep getting this type of error on my admin e-mail.

OperationalError: (1040, 'Too many connections')

I have installed django-debug-toolbar and I have an average of 100-150 queries for each page and less than 300ms response time. I do not think that I overload my MySql server. On the other hand, this happened some time after I tried to re-configure my httpd.conf settings.

My site receives 800-1000 hits per day.

These are my httpd.conf settings

KeepAlive Off
Listen 13646
MaxSpareThreads 15
MinSpareThreads 8
MaxClients 20
ServerLimit 20
SetEnvIf X-Forwarded-SSL on HTTPS=1
ThreadsPerChild 5
MaxRequestsPerChild 1000000
StartServers 20
WSGIDaemonProcess <project> processes=7 threads=12 python-path=<path>
WSGIProcessGroup <project>
WSGIRestrictEmbedded off
WSGILazyInitialization On

Any help would be greately appreciated.

PS. I am on a shared hosting plan. I have 512MB RAM for my hosting plan 300MB of them are free.

xpanta
  • 159
  • 2
  • 9

1 Answers1

6

A few comments about your configuration.

  1. MaxSpareThreads and MinSpareThreads should really be a multiple of ThreadsPerChild. If it isn't, with some combinations of values you can get Apache cycling between creating new processes because it thinks it needs them and then deciding the next moment to kill them again because it then changes its mind and sees them as excess. So, change MinSpareThreads to 5.

  2. MaxClients says that can have at most 20 concurrent requests across all Apache child worker processes. Yet, the mod_wsgi daemon processes to which they are proxying, have the capacity to handle 7*12=84. Thus you are over allocated on daemon process/threads because the Apache child worker processes are never going to be able to pass through more than 20 requests at the same time. A better configuration, and one which uses less memory is processes=4 and threads=5.

  3. StartServers set to 20 is way to high if you are actually using worker MPM. Technically StartServers cant be more than MaxClients/ThreadsPerChild for worker MPM. So, set StartServers to 2.

  4. MaxRequestsPerChild is set arbitrarily high and with your request throughput rate, you may as well set it to the normal default of 0, which means keep the processes around rather than recycle them on some maximum number of requests. Since the Python web application is running in mod_wsgi daemon mode, there should not be a need to recycle the Apache child worker process anyway.

Anyway, this is unlikely to change your original problem but would create a more optimal configuration.

Graham Dumpleton
  • 5,990
  • 2
  • 20
  • 19
  • Thank you, Your post was very very informative. I will mark the answer as correct because you imply that my server settings do not affect mysql server and I don't see anyone else disagree. – xpanta Jul 26 '12 at 07:22