1

I'm running my django app on apache2 (with mpm prefork) with a mysql database. It works fine, except when multiple users use it, and it takes around 40-45m RES for each user.

httpd.conf:

WSGIScriptAlias / /home/myproject/apache/django.wsgi
Alias /blog /var/www/blog/
Alias /maintain_wp_database/phpmyadmin_secure /usr/share/phpmyadmin

django_wsgi:

import os
import sys
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

path2 = '/home'
if path2 not in sys.path:
sys.path.append(path2)

path = '/home/myproject'
if path not in sys.path:
sys.path.append(path)

config file:

<VirtualHost *:80>
ServerName myproject.com
    ServerAlias www.myproject.com
    #
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www\.myproject\.com
    RewriteRule (.*) http://myproject.com$1 [R=301,L] 
    #
        DocumentRoot /home

    WSGIScriptAlias / /home/myproject/apache/django.wsgi

    <Directory /home/myproject/static>
    Order deny,allow
    Allow from all
    </Directory>
    <Directory />
        Options FollowSymLinks
        AllowOverride None
    </Directory>
    Alias /static/ /home/myproject//static/
    Alias /robots.txt /home/myproject/templates/robots.txt
    Alias /favicon.ico /home/myproject/static/images/favicon.ico

    AliasMatch ^/([^/]*\.css) /home/myproject/styles/$1

    Alias /media/ /home/myproject/media/

    ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
    <Directory "/usr/lib/cgi-bin">
        AllowOverride None
        Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
        Order allow,deny
        Allow from all
    </Directory>

    <Directory /home/myproject/apache>
    Order allow,deny
    Allow from all
    </Directory>

    ErrorLog /var/log/apache2/error.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined


</VirtualHost>

apache2.conf:

KeepAlive On
Timeout 300
MaxKeepAliveRequests 100
KeepAliveTimeout 15
<IfModule mpm_prefork_module>
    StartServers        5
    MinSpareServers     5
    MaxSpareServers     10
    MaxClients          150
    MaxRequestsPerChild 0

output of top:

top - 11:24:10 up 3 days,  1:08,  3 users,  load average: 0.00, 0.01, 0.05
Tasks: 140 total,   1 running, 134 sleeping,   5 stopped,   0 zombie
Cpu(s):  0.0%us,  0.0%sy,  0.0%ni,100.0%id,  0.0%wa,  0.0%hi,  0.0%si,  0.0%st
Mem:   1011248k total,   491532k used,   519716k free,    11380k buffers
Swap:   262140k total,   119884k used,   142256k free,   127244k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
 7678 www-data  20   0  228m  42m 6812 S    0  4.3   0:01.16 apache2            
 7665 www-data  20   0  232m  41m 4956 S    0  4.2   0:00.75 apache2            
 7676 www-data  20   0  212m  40m 4628 S    0  4.1   0:00.27 apache2            
 7956 www-data  20   0  230m  39m 4416 S    0  4.0   0:00.65 apache2            
 7677 www-data  20   0  230m  39m 4452 S    0  4.0   0:00.69 apache2            
 7667 www-data  20   0  224m  36m 4836 S    0  3.7   0:00.48 apache2            
 2539 mysql     20   0  321m  28m 2584 S    0  2.9   0:12.32 mysqld             
 7661 root      20   0  181m 9772 4588 S    0  1.0   0:00.12 apache2            
 7724 www-data  20   0  183m 9412 2228 S    0  0.9   0:00.01 apache2            
 7666 www-data  20   0  183m 9360 2240 S    0  0.9   0:00.01 apache2            
 7668 www-data  20   0  183m 9108 2108 S    0  0.9   0:00.00 apache2            
 7981 www-data  20   0  183m 8596 1768 S    0  0.9   0:00.00 apache2            
 4645 root      20   0 71120 3580 2532 S    0  0.4   0:00.07 sshd               
 7993 root      20   0 70664 3296 2556 S    0  0.3   0:00.02 sshd     

Do I need to optimize my config settings or is this a memory leak issue (something wrong in the code maybe?). Because of each user taking 40-45m (is this normal??) in apache, most of the memory in my 1gb linode gets consumed pretty fast. Any help on this would be great! Thanks

0

Try lowering the MaxSpareServers down to around 5, and MinSpareServers to 1. This will reduce the number of apache processes. ~40 MB is normal in Apache. This configuration is optimal for a low memory server:

StartServers 1
MinSpareServers 1
MaxSpareServers 5
ServerLimit 50
MaxClients 50
MaxRequestsPerChild 5000

  • Thanks for the response. I have a 1gb ram server. If I do free -m, I get free memory to be around 600mb, so shouldn't the MaxClients be equal to 15? And on a different note, should I use prefork or worker? – user_2000 Jun 10 '13 at 16:21
  • prefork is the preferred method. The setup I gave varies on your environment, but don't set the MaxClients too low or you'll run out of workers. – Nathan C Jun 10 '13 at 17:51
0

Read:

and watch:

In short. Don't use mod_wsgi in embedded mode with Apache if you are using prefork MPM unless you know how to configure Apache properly for Python web applications.

  • Thanks, I already changed to worker and using mod_wsgi in the daemon mode. These are my daemon settings - WSGIDaemonProcess abc.com processes=2 threads=3 stack-size=1048576 maximum-requests=5000 inactivity-timeout=900 display-name=%{GROUP} WSGIProcessGroup abc.com worker settings - StartServers 2 ServerLimit 12 MinSpareThreads 25 MaxSpareThreads 75 ThreadLimit 25 ThreadsPerChild 25 MaxClients 300 MaxRequestsPerChild 0 ThreadStackSize 1048576 every apache instance takes up 40-50 mb, how to reduce this? – user_2000 Jun 11 '13 at 12:11
  • Using 40-50 MB for a Django process isn't particularly abnormal. Having addressed Apache configuration issues to ensure you don't have more copies than you need, then you need to work on what your code itself is doing. – Graham Dumpleton Jun 11 '13 at 22:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.