django + apache2,每个用户占用大量内存

django + apache2,每个用户占用大量内存

我在 apache2 (使用 mpm prefork) 上使用 mysql 数据库运行我的 django 应用程序。它运行良好,但当多个用户使用它时除外,每个用户大约需要 40-45m RES。

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)

配置文件:

<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

顶部输出:

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     

我是否需要优化我的配置设置,或者这是一个内存泄漏问题(代码中可能存在错误?)。由于每个用户在 apache 中占用 40-45 分钟(这是正常的吗?),我的 1gb linode 中的大部分内存消耗得相当快。任何帮助都非常好!谢谢

答案1

尝试将内存降低MaxSpareServers到 5 左右,然后MinSpareServers降低到 1。这将减少 Apache 进程的数量。在 Apache 中,~40 MB 是正常的。此配置对于低内存服务器来说是最佳的:

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

答案2

读:

并观看:

简而言之。如果您使用 prefork MPM,请不要在 Apache 中以嵌入模式使用 mod_wsgi,除非您知道如何为 Python Web 应用程序正确配置 Apache。

相关内容