Optimal settings for VPS with 512MB ram (memory issue)

Arkshine

Active member
Hi,

Recently I've noticed my forum starting to slow-down and it would be seem server eats too much memory that it uses almost all the available swap most of time.
I guess it's time to configure properly all the softwares since I got more hits than at the start.

Just as note, I don't know much and the current configuration is from different tutorials here and there, but I am willing to do any manipulations. I want to have a fast forum !

Here the relevant informations :

General
  • VPS with 512MB RAM + 362 MB for swap
  • Contains only one XenForo forum and don't plan to add more
  • Small community, don't have much visits for now since forum is fairly new
Technical
  • OS : Debian 6 32 bits (2.6.32-xenU-7428-i386)
  • PHP-FPM 5.3.23-1~dotdeb.0
  • MariaDB 5.5.30
  • varnish 3.0.3 as frontend
  • nginx 1.2.7 as backend
  • memcached 1.4.15 (with Libmemcached 1.0.10)
  • APC 3.1.13
The relevant settings which are uncommented :
  • About PHP-FPM

    php-fpm.conf
    Code:
    pid = /var/run/php5-fpm.pid
    error_log = /var/log/php5-fpm.log
    log_level = notice
    emergency_restart_threshold = 10
    emergency_restart_interval = 1m
    process_control_timeout = 5s

    pool.d/www.conf
    Code:
    user = www-data
    group = www-data
    listen = /var/run/php5-fpm.sock
    listen.owner = www-data
    listen.group = www-data
    listen.mode = 0666
    pm = dynamic
    pm.max_children = 10
    pm.start_servers = 1
    pm.min_spare_servers = 1
    pm.max_spare_servers = 3
    pm.process_idle_timeout = 10s;
    pm.max_requests = 1000
    slowlog = /var/log/php5-fpm/$pool.log.slow
    request_slowlog_timeout = 5s
    request_terminate_timeout = 120s
  • About MariaDB

    Probably this one eating too much memory.

    my.cnf
    Code:
    [client]
    port= 3306
    socket= /var/run/mysqld/mysqld.sock
    
    [mysqld_safe]
    socket= /var/run/mysqld/mysqld.sock
    nice= 0
    
    #
    # * Basic Settings
    #
    user= mysql
    pid-file= /var/run/mysqld/mysqld.pid
    socket= /var/run/mysqld/mysqld.sock
    port= 3306
    basedir= /usr
    datadir= /srv/d_cs-amx/mysql
    tmpdir= /tmp
    lc_messages_dir= /usr/share/mysql
    lc_messages= en_US
    skip-external-locking
    
    #
    # * Fine Tuning
    #
    max_connections= 20
    connect_timeout= 5
    wait_timeout= 28800
    max_allowed_packet= 32M
    thread_cache_size    = 50
    sort_buffer_size= 4M
    bulk_insert_buffer_size= 16M
    tmp_table_size= 32M
    max_heap_table_size= 32M
    join_buffer_size= 256K
    #
    # * MyISAM
    #
    # This replaces the startup script and checks MyISAM tables if needed
    # the first time they are touched. On error, make copy and try a repair.
    myisam_recover          = BACKUP
    key_buffer_size= 128M
    open-files-limit= 5000
    table_open_cache= 2000
    myisam_sort_buffer_size= 128M
    concurrent_insert= 2
    read_buffer_size= 2M
    read_rnd_buffer_size= 1M
    
    #
    # * Query Cache Configuration
    #
    query_cache_type= OFF
    
    #
    # * Logging and Replication
    #
    log_warnings= 2
    slow_query_log= 1
    slow_query_log_file= /srv/d_cs-amx/mysql/log/mariadb-slow.log
    long_query_time= 5
    log_slow_verbosity= query_plan
    log_bin= /srv/d_cs-amx/mysql/log/mariadb-bin
    log_bin_index= /srv/d_cs-amx/mysql/log/mariadb-bin.index
    sync_binlog= 1
    expire_logs_days= 3
    max_binlog_size        = 100M
    
    #
    # * InnoDB
    #
    default_storage_engine= InnoDB
    innodb_buffer_pool_size= 128M
    innodb_log_buffer_size= 8M
    innodb_file_per_table= 1
    innodb_open_files= 1024
    innodb_io_capacity= 1024
    innodb_flush_method= O_DIRECT
    innodb_thread_concurrency= 0
    innodb_stats_on_metadata= off
    innodb_old_blocks_time= 1000
        
    [mysqldump]
    quick
    quote-names
    max_allowed_packet= 16M
    
    [mysql]
    #no-auto-rehash# faster start of mysql but no tab completition
    
    [isamchk]
    key_buffer= 16M
  • About Varnish

    default.vcl

    http://pastebin.com/uZNceAnj (takes too much caracters :p)

    etc/default/varnish
    Code:
    # Should we start varnishd at boot?  Set to "no" to disable.
    START=yes
    
    # Maximum number of open files (for ulimit -n)
    NFILES=131072
    
    # Maximum locked memory size (for ulimit -l)
    # Used for locking the shared memory log in memory.  If you increase log size,
    # you need to increase this number as well
    MEMLOCK=82000
    
    ## Alternative 2, Configuration with VCL
    #
    # Listen on port 6081, administration on localhost:6082, and forward to
    # one content server selected by the vcl file, based on the request.  Use a 1GB
    # fixed-size cache file.
    #
    DAEMON_OPTS="-a :80 \
                -T localhost:6082 \
                -f /etc/varnish/default.vcl \
                -S /etc/varnish/secret \
                -s malloc,256m"
  • About nginx

    nginx.conf

    Code:
    userwww-data www-data;
    
    # As a thumb rule: One per CPU. If you are serving a large amount
    # of static files, which requires blocking disk reads, you may want
    # to increase this from the number of cpu_cores available on your
    # system.
    #
    # The maximum number of connections for Nginx is calculated by:
    # max_clients = worker_processes * worker_connections
    worker_processes2;
    
    # Change these paths to somewhere that suits you!
    error_log  /var/log/nginx/error.log;
    pid/var/run/nginx.pid;
    
    # Maximum file descriptors that can be opened per process
    # This should be > worker_connections
    worker_rlimit_nofile9000;
    
    events 
    {
        # When you need > 8000 * cpu_cores connections, you start optimizing
        # your OS, and this is probably the point at where you hire people
        # who are smarter than you, this is *a lot* of requests.
        worker_connections8000;
    }
    
    http 
    {  
        includemime.types;
    
        default_typeapplication/octet-stream;
        
        log_format            main '$remote_addr - $remote_user [$time_local] $request '
                                '"$status" $body_bytes_sent "$http_referer" '
                                '"$http_user_agent" "$http_x_forwarded_for" "$gzip_ratio"';
    
        set_real_ip_from127.0.0.1; 
        real_ip_headerX-Forwarded-For; 
    
        sendfileon;
        tcp_nopushoff;
        tcp_nodelayon;
        keepalive_timeout5;
        types_hash_max_size2048;
        server_tokensoff;
        #keepalive_requests0;
    
        # Click tracking!
        #access_log/var/log/nginx/access.log main;
    
        gzipoff;
        gzip_staticon;
        gzip_varyon;
        gzip_disable"MSIE [1-6]\.(?!.*SV1)";
        gzip_min_length1100;
        gzip_buffers32 8k;
        gzip_http_version1.1;
        gzip_comp_level9;
        gzip_proxiedany;
        gzip_typestext/plain 
                            text/css 
                            text/javascript 
                            text/xml 
                            text/x-component
                            application/x-javascript
                            application/xml 
                            application/xml+rss
                            application/octet-stream 
                            application/vnd.ms-fontobject 
                            application/x-font-opentype 
                            application/x-font-woff
                            image/png 
                            image/gif 
                            image/jpeg
                            image/svg+xml 
                            image/x-icon;
    
        client_body_buffer_size 10m;
        client_max_body_size16m;
        client_body_temp_path  /var/spool/nginx/client_temp 1 2;
    
        port_in_redirect off;
        
        include /etc/nginx/sites-enabled/*;
    }

    site-available/default

    http://pastebin.com/u0cd58LS (takes too much caracters :p)
  • About memcached

    With session/igbinary/json support enabled.
    Used with XF :

    /library/config.php
    Code:
    $config['cache']['enabled'] = true;
    $config['cache']['cacheSessions'] = true;
    $config['cache']['frontend'] = 'Core';
    $config['cache']['frontendOptions'] = array(
        'caching'=> true,
        'cache_id_prefix'=> 'xf_',
        'automatic_serialization'=> true,
        'lifetime'=> 0
        );
    $config['cache']['backend'] = 'Libmemcached';
    $config['cache']['backendOptions'] = array(
        'servers' => array(
            array(
            'host'=> '127.0.0.1',
            'port'=> 11211,
            'weight'=> 1
            )
        )
    );

    etc/default/memcached
    Code:
    DAEMON_OPTS="-u nobody \
              -m 16"

    etc/php5/conf.d/memcached.ini
    Code:
    extension = memcached.so
    session.save_handler = "memcached"
    session.save_path = "127.0.0.1:11211"
    
    [memcached]
    memcached.sess_locking = On
    memcached.sess_lock_wait = 150000
    memcached.sess_prefix = "memc.sess.key."
    memcached.sess_binary = Off
    memcached.compression_type = "fastlz"
    memcached.compression_factor = "1.3"
    memcached.compression_threshold = 2000
    memcached.serializer = "igbinary"
    memcache.hash_strategy = "consistent"
  • About APC

    etc/php5/conf.d/apc.ini
    Code:
    extension=apc.so
    
    apc.cache_by_default= 1
    apc.canonicalize= 1
    apc.coredump_unmap= 0
    apc.enable_cli= 0
    apc.enabled= 1
    apc.file_md5= 0
    apc.file_update_protection= 0
    apc.gc_ttl= 1200
    apc.include_once_override= 0
    apc.lazy_classes= 0
    apc.lazy_functions= 0
    apc.max_file_size= 10M
    apc.mmap_file_mask= /tmp/apc-mmap.XXXXXX
    apc.num_files_hint= 1000
    apc.report_autofilter= 0
    apc.rfc1867= 1
    apc.rfc1867_freq= 512k
    apc.rfc1867_name= APC_UPLOAD_PROGRESS
    apc.rfc1867_prefix= upload_
    apc.rfc1867_ttl= 1200
    apc.serializer= igbinary 
    apc.shm_segments= 1
    apc.shm_size= 64M
    apc.shm_strings_buffer= 4M
    apc.slam_defense= 0
    apc.stat= 1
    apc.stat_ctime= 1
    apc.ttl= 0
    apc.use_request_time= 1
    apc.user_entries_hint= 4096
    apc.user_ttl= 0
    apc.write_lock= 1
I think that's all.

You will agree with a VPS/512MB and one forum, it should be fine fairly up to medium community. So, it's a real problem it uses alread all the swap with only few connections.

As note, I'm willing to hire someone to configure/optimize this configuration. So, for people who know well this stuff, it would be some easy money, if interested send me a PC.

Anyway, many thanks in advance for people who will advive/suggest/help me. :)
 
Well, I'm surely I wasn't the expert about this. But I just want to ask some questions:

1. Why use 32-bit Debian? Not 64-bit?
2. You use APC + Memcached. I think that's enough. Why you use Varnish too?
 
1. Don't remember exactly but considering the memory (only 512), and because I wanted to avoid any problem related x32/x64, I've prefered x32.
2. Not sure why. I've read it was really fast to use Libmemcached as cache for $config['cache']['backend'] but honestly I'm not sure what does XF with this "cache". When I look the usage, it doesn't use much, that's why, I've allocated only 16MB for that. But I've noticed a small diffrence with and without, and would seem better with.
 
Stick with 32bit unless you have 4+ GB of memory. 32bit apps will use slightly less memory so I'd stick with it for a 512.
 
1. Don't remember exactly but considering the memory (only 512), and because I wanted to avoid any problem related x32/x64, I've prefered x32.
2. Not sure why. I've read it was really fast to use Libmemcached as cache for $config['cache']['backend'] but honestly I'm not sure what does XF with this "cache". When I look the usage, it doesn't use much, that's why, I've allocated only 16MB for that. But I've noticed a small diffrence with and without, and would seem better with.

1) You should use x64, you won't have any problems with it. I guess from your x86 choice your new to managing your own server?

2) If your a new startup board your setup is overkill and probably misconfigured to serve up a small XenForo site.

I would reload with a 64 bit OS. Load up apache, with APC and percona, and see where that takes you for the time being.
 
1) You should use x64, you won't have any problems with it. I guess from your x86 choice your new to managing your own server?
I don't believe there are any benefits of x64 on a system with only 512mb memory. 64-bit OS come with some memory overhead, and 32 bit works just as well.
 
Apache ? Is nginx that bad ? I've heard it was running better than Apache, at least is lighter.

As it is the forum runs fairly fine, mainly because there are not much visits, even when the swap is used and sometimes you feel some unexpected waiting, but I'm afraid if a lot more, things won't follow.

If possible, before reinstall all from scratch, I would like to try and expect more some advice to adjust configuration settings for the current softwares and for 512MB, especially for mysql, as after many read here and there, varnish + nginx + mariaDB + php-fpm were the very good & fast config to use ; or it's actually bad ? :p

Thanks for the answers.
 
Stick with 32bit unless you have 4+ GB of memory. 32bit apps will use slightly less memory so I'd stick with it for a 512.
I think this is not an issue in server enviroment. But this can be an issue in dekstop.

CMIIW.
ps: I run a small VPS (Debian 6 64-bit, 512MB) to serve a WP and had no problem so far

And yes, I think your setup is over kill. APC + Memcached + Varnish? I would prefer use APC only first.

Apache ? Is nginx that bad ? I've heard it was running better than Apache, at least is lighter.

As it is the forum runs fairly fine, mainly because there are not much visits, even when the swap is used and sometimes you feel some unexpected waiting, but I'm afraid if a lot more, things won't follow.

If possible, before reinstall all from scratch, I would like to try and expect more some advice to adjust configuration settings for the current softwares and for 512MB, especially for mysql, as after many read here and there, varnish + nginx + mariaDB + php-fpm were the very good & fast config to use ; or it's actually bad ? :p

Thanks for the answers.
NGINX is good. (y)
 
Apache ? Is nginx that bad ? I've heard it was running better than Apache, at least is lighter.

As it is the forum runs fairly fine, mainly because there are not much visits, even when the swap is used and sometimes you feel some unexpected waiting, but I'm afraid if a lot more, things won't follow.

If possible, before reinstall all from scratch, I would like to try and expect more a generic base configuration for the current softwares for 512MB, especially for mysql, as after many read here and there, varnish + nginx + mariaDB + php-fpm were the very good & fast config to use ; or it's actually bad ? :p

Thanks for the answers.

I just think your trying to overkill a setup which doesn't need overkilling.

If I was going to start out a new VPS for a brand new forum, I would load it with

CentOs 6.4 x64, Apache/Free Litespeed + PHP, Percona with XtraDB, and thats about it in reality.

Just my preference at least.
 
Sheratan, I've tried APC only, it was so slow. :p Once you put varnish, page loads in a blink eye, it's significant faster. I guess I could remove Memcached but since allocated to 16MB only, I'm not sure it's an issue too, but It's true it sounds like something I could remove.

@Slavik, I don't mind to start config from scratch, but is it really worth ? At least for a XF forum, it would way faster and eating way less memory ?
After all I've read, people praising varnish/php-fm/nginx/MariaDB, you advice something totally different, I'm not sure what to do now. :P
 
I've tried APC only, it was so slow. :p Once you put varnish, page loads in a blink eye, it's significant faster. I guess I could remove Memcached but since allocated to 16MB only, I'm not sure it's an issue too, but It's true it sounds like something I could remove.
I don't and never use APC (xCache FTW). But maybe you have some misconfiguration in xF cache setup or APC.
 
@Slavik, I don't mind to start config from scratch, but is it really worth ? At least for a XF forum, it would way faster and eating way less memory ?
After all I've read, people praising varnish/php-fm/nginx/MariaDB, you advice something totally different, I'm not sure what to do now. :p


Apples and oranges, all people have their own preferences. My preference on a new startup site is keep it simple.
 
I've had problems with memory with 512M RAM and 64bit OS. In my opinion, I'd stick with 32bit. I've had good luck with just 512M RAM, nginx, APC, and MySQL (or MariaDB), and ignoring memcache and varnish until more memory is available for it.

I eventually had a lot of fragmentation in APC with this 512M RAM setup, but that was because I was trying to serve a number of different sites with it. If you just have one, I suspect you wouldn't have many problems. If you do, I might suggest to restart php-FPM as needed, weekly or monthly when fragmentation gets out of control..

This is what I use for 512M RAM VPS's in my.cnf. Not sure what the equivalent is for MariaDB, assuming it uses the same db configuration.

Code:
symbolic-links=0
max_allowed_packet = 5M
thread_stack = 64K
innodb_buffer_pool_size=110M
innodb_thread_concurrency=3
join_buffer_size=2M
key_buffer_size=128M
max_connections=35
max_heap_table_size=21M
query_cache_limit=1M
query_cache_size=24M
query_cache_type=1
read_buffer_size=2M
skip-networking
sort_buffer_size=3M
net_buffer_length=4K
table_cache=1024
thread_cache_size=286
tmp_table_size=21M
 
Apache ? Is nginx that bad ? I've heard it was running better than Apache, at least is lighter.

As it is the forum runs fairly fine, mainly because there are not much visits, even when the swap is used and sometimes you feel some unexpected waiting, but I'm afraid if a lot more, things won't follow.

If possible, before reinstall all from scratch, I would like to try and expect more some advice to adjust configuration settings for the current softwares and for 512MB, especially for mysql, as after many read here and there, varnish + nginx + mariaDB + php-fpm were the very good & fast config to use ; or it's actually bad ? :p

Thanks for the answers.
Nginx is loads better than Apache, I wouldn't get rid of it. Even without optimal settings it performs better than Apache.

What's most likely to be consuming resources is mysql/mariadb. That is what I would work on tuning. I wish I could offer more help but tuning mysql has never been easy for me.

I'd also dump varnish and memcached if your site is not that big. Use APC for cache instead since you already have it installed.
 
@Slavik : I like when It's simple too but I'm worried about performance. Will take a look to Litespeed. Btw, MariaDB uses XtraDB too, and I've read benchmark MariaDB was the best for mixed InnoDB and MyISAM usage environments. Any thoughts on it ?

@SneakyDave : Thanks ! What do you use then in the backend cache in XF config ? I might try to reduce varnish memory or removing, not sure yet.

Random question, about mysql, I can turn off safely the replication thing (not sure what is about), because it ates spaces a lot ?

EDIT:

@Jeremy, thanks for your advises. I guess I will give a try without varnish again, it will free 256 MB, I guess it will help for sure. About Mysql, I would like someone proposes good default values for 512 MB. :P
 
MariaDB vs Percona is more of a preference thing imo. I personally prefer MariaDB. Wouldn't waste time switching to Percona as it's not likely to solve the problem at hand.

I also would stick with nginx over litespeed, if for no other reason than it's already installed and running and performs very well itself.

Use APC for the backend cache in XF config. I would remove Varnish and focus on optimizing your site without it first.
 
@SneakyDave : Thanks ! What do you use then in the backend cache in XF config ? I might try to reduce varnish memory or removing, not sure yet.

You can use a File cache backend. It's not as glamorous, but it gets the job done.

In config.php
Code:
$config['cache']['backend'] = 'File';
$config['cache']['backendOptions']['cache_dir'] = '/some/place/to/store/this/file/that/should/be/writable/by/the/webserver/xf_backend_cache';
 
Something is very, very wrong here. We have clients run cPanel with dozens of websites on 512mb VPSs, and still have plenty of power left. It's not recommended, but I've seen it done. There is no way a brand new forum with nothing else installed should be having an issue on that VPS.

You sure you're actually running out of RAM? http://www.linuxatemyram.com/
 
It's just starting to be an issue ; when you see the forum is not reactive as before, sometimes with obvious slowdown, and when you check memory, all is used including most of the swap, for me it's clear it's out of RAM at this point, right ?
For now it's minor issue, but I would like to prevent any further problems when I will have more hits and would like to fix configuration while I'm around.

I think I will take another VPS just for one month to test suggestions done above.
@Slavik, just seen Litespeed is not free, at least the full version, is the standard version running well ? because I've read some comment it was not really good.

Meanwhile, I would like to have some concrete settings with 512 MB for at least mysql/nginx, because still not sure how to configure them properly for my use (one XF forum) and it's probably one the reason after some times swap is used. Will try SneakyDave's suggestion about msysql but more suggestions/explanations to have a good base configuration for this amount of memory would be very welcomed. Is there some expert around ? :p
 
Did you check out the link from my previous post? It explains RAM usage in linux. Your RAM might show itself as being used, when it's actually being used for disk caching or such.

A lot of people simply don't understand how memory usage works in linux, and as a result, you get all sorts of wacky advice. If your forum is the one in your profile (currently with 2 users online), there is no possible way that should be using all 512mb of RAM, assuming that's all you have running on your VPS. I would be absolutely shocked if it actually was using all that RAM.
 
Back
Top Bottom