• OC2PS

    (@sooskriszta)


    I have recently moved my low-moderate traffic (1000 UAUs, 5000 pageviews on a busy day) website from shared hosting to a Centos 6 64-bit VPS with Apache and cPanel running on 4 quad-core processor (likely oversold) and 3GB memory (Xen).

    We’ve had problems from the beginning. The server keeps crashing. It seems PHP keeps expanding till it consumes all the memory and crashes the server.

    Some folks have suggested that I should abandon Apache/cPanel/PHP/mySQL and go with nginX/Varnish/PHP-FPM/SQLite. But that’s just not possible for me as I am not very tech savvy and need a simple GUI like cPanel to be able to manage the mundane management tasks (can’t afford to hire system administrator or get fully managed hosting).

    I have come across several posts discussing optimization of Apache for WordPress. But all of these lead to articles that are pretty dated such as this ~4 year old one from Jan 2009 – https://thethemefoundry.com/blog/optimize-apache-wordpress/

    The article is pretty detailed and seems helpful, but I stumble even on the first step. httpd.conf only has 2 loadmodule commands
    LoadModule fastinclude_module modules/mod_fastinclude.so
    LoadModule bwlimited_module modules/mod_bwlimited.so

    So I go total bust right there. Further, httpd.conf says
    Direct modifications to the Apache configuration file may be lost upon subsequent regeneration of the configuration file. To have modifications retained, all modifications must be checked into the configuration system by running: /usr/local/cpanel/bin/apache_conf_distiller
    I am having trouble finding where to change the modules in WHM.

    Please can someone help me with updated guidelines on how to optimize Apache for WordPress? Many thanks!

    P.S. The WordPress installation also has WP Super Cache installed.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Some folks have suggested that I should abandon Apache/cPanel/PHP/mySQL and go with nginX/Varnish/PHP-FPM/SQLite

    No idea how you’d go with SQLite. That is a completely different database than what WordPress uses. I am considering nginX though. Anyway, you are correct, that kind of conversion may not be the thing to try for someone new to server management.

    Apache2 can run in two ‘modes’– prefork and worker. The latter is a second generation threaded service. The former spawns individual processes. The latter is better in theory but there are a lot of cautions when it comes to using it in combinations with PHP.

    If you open, ” /etc/httpd/conf/httpd.conf”, you should see:

    <IfModule prefork.c>
    StartServers       8
    MinSpareServers    5
    MaxSpareServers   20
    ServerLimit      256
    MaxClients       256
    MaxRequestsPerChild  4000
    </IfModule>
    <IfModule worker.c>
    StartServers         4
    MaxClients         300
    MinSpareThreads     25
    MaxSpareThreads     75
    ThreadsPerChild     25
    MaxRequestsPerChild  0
    </IfModule>

    That is the default configuration for Apache2 on Centos 6 as far as I am aware. At least that is what it looked like when I got to it. It also appears to default to ‘prefork’. The problem is with the ServerLimit and MaxClients lines. Every time a client is spawned it grabs memory. Now, with 256 clients and 3GB RAM, each client can only use about 12MB, and that isn’t allowing any memory for MySQL or the rest of the system. WordPress defaults to 32MB (64MB for multisite) max memory. Meaning that chances are with enough traffic there comes a point when Apache tries to spawn a process and there is no memory to use. The server will try to use swap space if it can and you end up with situation where RAM gets written to swap space and back again over and over and the system chokes on its onw efforts to manage memory and more or less fails. There is a geek cool name for it but I can’t remember– oh the shame!

    So, you need to cut the numbers of clients to the point that you won’t be exhausting memory. You will need to figure out how much the system – Apache uses. Let’s assume 500MB. You have 2500MB available. At 32MB max memory, you should be able to spawn 78 processes without running out of RAM. Or…

    <IfModule prefork.c>
    StartServers       8
    MinSpareServers    5
    MaxSpareServers   20
    ServerLimit      78
    MaxClients       78
    MaxRequestsPerChild  4000
    </IfModule>

    That MaxRequestsPerChild number is how many times the process will be reused before it is closed and a new one started. Reusing is faster but the process doesn’t release memory until it is closed.

    StartServers is the number of processes that are created when Apache starts, whether you need them or not and MinSpareServers is the number of open processes kept in the wings just in case.

    As you may have guessed, I have some familiarity with this. I also run WordPress on a 64bit Centos 6 VPS. I am still working on the solution and this is what I have so far. I can’t promise anything at this point and, please, if anyone can correct me on something do so.

    Thread Starter OC2PS

    (@sooskriszta)

    Thank you so very much for the descriptive and easy-to-understand response.

    My current httpd.conf has the following settings related to what you wrote above

    Timeout 300
    TraceEnable On
    ServerSignature Off
    ServerTokens Full
    FileETag None
    StartServers 5
    <IfModule prefork.c>
    MinSpareServers 5
    MaxSpareServers 10
    </IfModule>
    ServerLimit 256
    MaxClients 150
    MaxRequestsPerChild 10000
    KeepAlive On
    KeepAliveTimeout 6
    MaxKeepAliveRequests 75

    So I will have to try reducing
    MaxClients and MaxRequestsPerChild
    settings.

    Could you please also explain in layman terms (or point me to a resource) the meaning/purpose of

    ServerLimit
    MaxClients
    MaxRequestsPerChild

    Specifically, what are servers, clients and children in this context? What does process mean?

    My wp-config says

    define (‘WP_MEMORY_LIMIT’, ‘2048M’);

    Should I also reduce the wp_memory_limit?

    Again, still working through the details myself but…

    1. ServerLimit: The maximum number of processes that will be created over the life of the server. For prefork, this is basically the maximum possible value for MaxClients.
    2. MaxClients: The maximum possible simultaneous connections. Think of it like cloning. If your Apache server is busy it will make a new copy of itself to help handle the traffic, but will only create copies up ServerLimit. For prefork, this and ServerLimit should be the same or very close with MaxClients never exceeding ServerLimit.
    3. MaxRequestsPerChild: The number of requests a process will handle before being killed, and another one created. Reusing is faster but memory gets ‘locked’ up. Say you have a script that normally uses 5MB memory. Each process will use only 5MB. If, however, one rare function uses 80MB, that process (child) will hang onto 80MB until it gets killed, which is a big waste of memory. So what you want is a number low enough to moderate that memory consumption but high enough that you aren’t spawning processes all the time, which is ineffecient.

    Here is the Apache Docs for these settings: https://httpd.apache.org/docs/2.2/mod/mpm_common.html

    A process is a program that is in the process of executing. It is a running program.

    Yes, 2048MB is much too high. If you need that much you have very serious structural problems with the website. Stock WordPress runs fine with 32MB. Right now, my processes are using 55MB but that is an extensively modified system that does a lot of work– far above and beyond what WordPress would normally need (but still below what multisite requires).

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to optimize Apache for WordPress?’ is closed to new replies.