2

I'm running a 512MB and i have lot of ram issues. I think is due to my configuration.

root@s1:~# free -m
             total       used       free     shared    buffers     cached
Mem:          1024       1022          1          0          0          0
-/+ buffers/cache:       1022          1
Swap:            0          0          0
root@s1:~# ps aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1  23760  1756 ?        Ss   15:33   0:00 init
root      1086  0.0  0.0  21304  1016 ?        Ss   15:33   0:00 /sbin/udevd --daemon
root      1153  0.0  0.2  49988  2908 ?        Ss   15:33   0:00 /usr/sbin/sshd -D
root      1199  0.0  0.1  14924  1104 ?        Ss   15:33   0:00 /usr/sbin/xinetd -dontfork -pidfile /var/run/xinetd.pid -stayalive -inetd_com
root      1201  0.0  0.0  19068  1020 ?        Ss   15:33   0:00 cron
syslog    1235  0.0  0.0  12704   816 ?        Ss   15:33   0:00 /sbin/syslogd -u syslog
root      1276  0.0  0.0   4352   748 ?        S    15:33   0:00 /bin/sh /usr/bin/mysqld_safe
mysql     1345  0.0  4.0 512600 42444 ?        Sl   15:33   0:00 /usr/sbin/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/li
root      1435  0.0  0.1  76844  1404 ?        Ss   15:33   0:00 nginx: master process /usr/sbin/nginx
www-data  1436  0.0  0.1  77164  1928 ?        S    15:33   0:00 nginx: worker process
www-data  1437  0.0  0.1  77164  1960 ?        S    15:33   0:00 nginx: worker process
www-data  1440  0.0  0.1  77164  1960 ?        S    15:33   0:00 nginx: worker process
www-data  1443  0.0  0.1  77164  1940 ?        S    15:33   0:00 nginx: worker process
root      1466  0.0  0.5 337844  6232 ?        Ss   15:33   0:00 php-fpm: master process (/etc/php5/fpm/php-fpm.conf)      
www-data  1468  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1471  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1472  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1473  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1474  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1475  0.0  0.5 337844  5316 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1476  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1477  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1478  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1479  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1483  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1484  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1486  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1488  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
www-data  1490  0.0  0.5 337844  5320 ?        S    15:33   0:00 php-fpm: pool www                                         
root      1592  0.0  0.1 117216  1436 ?        Ss   15:33   0:00 /usr/sbin/varnishd -P /var/run/varnishd.pid -a :80 -T localhost:6082 -f /etc/
www-data  1593  0.0  0.2 593584  2672 ?        Sl   15:33   0:00 /usr/sbin/varnishd -P /var/run/varnishd.pid -a :80 -T localhost:6082 -f /etc/
root      1690  0.0  0.2  89132  2248 ?        Ss   15:33   0:00 sendmail: MTA: accepting connections          
root      1751  0.0  0.3  73384  3672 ?        Rs   15:35   0:00 sshd: root@pts/1    
root      1767  0.0  0.2  20920  2132 pts/1    Ss   15:35   0:00 -bash
root      1833  0.0  0.1  18112  1248 pts/1    R+   15:51   0:00 ps aux

And my configuration was:

/etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    multi_accept on;
    use epoll;
}

http {

    # Let NGINX get the real client IP for its access logs
    set_real_ip_from 127.0.0.1;
    real_ip_header X-Forwarded-For;

    # Basic Settings
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 20;
    client_max_body_size 15m;
    client_body_timeout 60;
    client_header_timeout 60;
    client_body_buffer_size  1K;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 8k;
    send_timeout 60;
    reset_timedout_connection on;
    types_hash_max_size 2048;
    server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    # Logging Settings
    # access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # Log Format
    log_format main '$remote_addr - $remote_user [$time_local] '
    '"$request" $status $body_bytes_sent "$http_referer" '
    '"$http_user_agent" "$http_x_forwarded_for"';

    # Gzip Settings
    gzip on;
    gzip_static on;
    gzip_disable "msie6";
    gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 512;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/css text/javascript text/xml text/plain text/x-component 
    application/javascript application/x-javascript application/json 
    application/xml  application/rss+xml font/truetype application/x-font-ttf 
    font/opentype application/vnd.ms-fontobject image/svg+xml;

    # Virtual Host Configs
    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

php.ini

short_open_tag = Off
ignore_user_abort = Off
post_max_size = 15M
upload_max_filesize = 15M
default_charset = "UTF-8"
allow_url_fopen = Off
default_socket_timeout = 30
mysql.allow_persistent = Off

[apc]
apc.stat = "0"
apc.max_file_size = "1M"
apc.localcache = "1"
apc.localcache.size = "64"
apc.shm_segments = "1"
apc.ttl = "3600"
apc.user_ttl = "7200"
apc.gc_ttl = "3600"
apc.cache_by_default = "1"
apc.filters = ""
apc.write_lock = "1"
apc.num_files_hint= "512"
apc.user_entries_hint="4096"
apc.shm_size = "64M"
apc.mmap_file_mask=/tmp/apc.XXXXXX
apc.include_once_override = "0"
apc.file_update_protection="2"
apc.canonicalize = "1"
apc.report_autofilter="0"
apc.stat_ctime="0"
;This should be used when you are finished with PHP file changes.
;As you must clear the APC cache to recompile already cached files.
;If you are still developing, set this to 1.
apc.stat="0"

/etc/php5/fpm/pool.d/www.conf

user = www-data
group = www-data
listen = /var/run/php-fpm.socket
listen.owner = www-data
listen.group = www-data
listen.mode = 0666
listen.allowed_clients = 127.0.0.1
pm = dynamic
pm.max_children = 50
pm.start_servers = 15
pm.min_spare_servers = 5
pm.max_spare_servers = 25
pm.process_idle_timeout = 60s
request_terminate_timeout = 30
security.limit_extensions = .php

php_flag[display_errors] = off
php_admin_value[error_reporting] = 0
php_admin_value[error_log] = /var/log/php5-fpm.log
php_admin_flag[log_errors] = on
php_admin_value[memory_limit] = 128M

/etc/default/varnish

DAEMON_OPTS="-a :80 \
    -T localhost:6082 \
    -f /etc/varnish/default.vcl \
    -u www-data -g www-data \
    -S /etc/varnish/secret \
    -p thread_pools=2 \
    -p thread_pool_min=25 \
    -p thread_pool_max=250 \
    -p thread_pool_add_delay=2 \
    -p session_linger=50 \
    -p sess_workspace=262144 \
    -p cli_timeout=40 \
    -s malloc,256m"
2
  • You seem to have 1 GB RAM, not 512 MB.
    – etagenklo
    May 15, 2013 at 15:13
  • I have burst memory = 1024 MB. But my vps should work with 512 mb only.
    – ufo
    May 15, 2013 at 15:18

3 Answers 3

2

Even after the changes suggested by @etagenklo, you have up to ten servers using 128 MB of PHP RAM apiece, plus another 64 MB of RAM for APC… that's more than 1G, right there, before you take into account any other sort of either overhead or per-process RAM usage.

At a guess, you need to trim down the memory usage of your PHP application; a memory limit of 128 MB is a limit, not a hard allocation, so you're actually using a large amount per process. A tool like XHProf or TraceView could help you identify where all that RAM is going. Otherwise, setting pm.max_children = 4 should keep you below 512 MB, even in the pathological case.

Of course, another solution to this is simply to get a bigger server. How many hours have you spent or will you spend debugging this? How much is your time worth? How much does a bit more memory cost?

1

The values in your fpm-pool.conf seem way too high. Try something more like this:

pm.max_children = 10
pm.start_servers = 2
pm.min_spare_servers = 2
pm.max_spare_servers = 2
3
  • root@s1:~# free -m total used free shared buffers cached Mem: 1024 1021 2 0 0 0 -/+ buffers/cache: 1021 2 Swap: 0 0 0
    – ufo
    May 15, 2013 at 15:15
  • Is this after editing you config? Did you restart php-fpm? What does the processlist look like afterwards?
    – etagenklo
    May 15, 2013 at 15:21
  • the process list decreased but still using to much ram..
    – ufo
    May 15, 2013 at 15:38
0

use php-fpm

pm = ondemand
pm.max_requests = 10000

and create some kind of swap

https://www.varnish-software.com/static/book/Tuning.html#storage-backends

these days there is no problem to get a bigger server/vps.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .