How To Optimize Apache Concurrency For Running Wordpress

To a technical savvy person, hosting your ain website on a VPS server tin give notice choose a tremendous feel of accomplishment in addition to a wealth of learning opportunities. If WordPress is what you lot fancy, amongst a minimal monthly fiscal commitment, you lot tin give notice host a WordPress website on the LAMP platform (Linux, Apache, MySQL, PHP). For example, the entry-level, $5 per calendar month conception offered yesteryear DigitalOcean, of which I am an affiliate, volition give you lot a 512MB RAM, single-core VPS.

With such a small-scale RAM capacity, you lot volition ask to optimize how your Apache webserver is configured to run PHP applications such every bit WordPress in addition to Drupal. The finish is to maximize the number of concurrent spider web connections.

This tutorial details the Apache/PHP setup physical care for on Debian 8.2, aka Jessie. The physical care for assumes Apache is yet to hold upward installed. However, if Apache2 is already installed, you lot volition detect practical information below on how to reconfigure Apache2 to run a unlike multi-processing module.

Background knowledge

According to a recent Netcraft webserver survey, Apache powers 46.91% of the meridian 1 yard one thousand busiest websites on the Internet. Busy websites hateful many concurrent spider web connections.

Concurrent connexion requests to Apache are handled yesteryear its Multi-Processing Modules. MPMs tin give notice hold upward loosely classified every bit threaded or non-threaded. Older Apache releases default to a MPM named Prefork. This MPM is non-threaded. Each connexion asking is handled yesteryear a dedicated, self-contained Apache process.

Newer Apache releases default to a threaded MPM, either Worker or Event. The Worker MPM uses 1 worker thread per connection. One number amongst this approach is that a thread is tied upward if the connexion is kept hold upward despite it existence inactive.

The Event MPM, a variant of Worker, addresses the aforesaid keep-alive issue. H5N1 principal thread is used every bit the traffic controller that listens for requests in addition to passes requests to worker threads on demand. In this scenario, an inactive but kept-alive connexion does non necktie upward a worker thread.

Note that MPMs are mutually exclusive: solely 1 MPM tin give notice hold upward active at whatsoever given time.

Traditionally, the Apache total functionality serves static spider web content (e.g., HTML text in addition to images). To serve dynamic content, such every bit PHP pages from WordPress, Apache requires especial modules that execute PHP code.

For the Prefork MPM, each spawned Apache physical care for embeds its ain re-create of the PHP handler (mod_php). Concurrency inward this model is express yesteryear the number of processes that Apache tin give notice spawn given the available memory.

For both Worker in addition to Event MPMs, PHP requests are passed to an external FastCGI process, PHP5-FPM. PHP-FPM stands for PHP-FastCGI Process Manager. Essentially, the webserver in addition to the PHP handler are divide to separate processes. Apache communicates amongst PHP-FPM through an Apache module, either mod_fastcgi or mod_fcgid. Optimizing concurrency inward this model agency configuring both the MPM in addition to the PHP handler (PHP-FPM) to choose pools of processes in addition to threads to grip requests.

The balance of this tutorial covers the cases of installing the Event MPM from scratch every bit good every bit migrating to Event from the Prefork MPM.

Installing Apache2

This tutorial starts amongst the installation of Apache2. If Apache is already installed, you lot should detect out which MPM is currently running using the command apache2ctl -V, in addition to decease along to the side yesteryear side section.

$ sudo apt-get update && sudo apt-get upgrade
$ sudo apt-get install apache2

Next, banknote the Apache version you lot precisely installed in addition to the MPM that is loaded.

$ sudo apache2ctl -V
Server version: Apache/2.4.10 (Debian)
...
Architecture: 64-bit
Server MPM: final result
threaded: aye (fixed thread count)
forked: aye (variable physical care for count)
...

The to a higher house output tells us that nosotros are running Apache release 2.4. Beginning amongst 2.4, Apache runs the Event MPM yesteryear default. If you lot are running an older version of Apache, the default MPM is either Prefork or Worker.

Configuring Apache2

  1. Back upward the Apache configuration file, /etc/apache2/apache2.conf.

    $ sudo cp /etc/apache2/apache2.conf{,.orig}

  2. Edit the configuration file.

    Below is a subset of configuration parameters belonging to the Apache total module. You should adjust their values inward gild to optimize concurrency. The corresponding values are what I work for an entry-level VPS.

    Timeout 100
    KeepAlive On
    MaxKeepAliveRequests 1000
    KeepAliveTimeout v

    For an in-depth explanation of the to a higher house parameters, delight refer to Apache on-line documentation.

  3. Enable mod_rewrite.

    While the mod_rewrite module is non strictly relevant to optimizing concurrency, I've included it hither every bit a reminder to install the module. It is an of import module for running WordPress.

    $ sudo a2enmod rewrite

Installing Event MPM

If you lot are already running the Event MPM, skip to the side yesteryear side section, 'Configuring Event MPM'. Otherwise, follow the physical care for below.

  1. Install the Event MPM.

    $ sudo apt-get install apache2-mpm-event

  2. Disable existing MPM.

    Recall that solely 1 of Prefork, Worker or Event MPM tin give notice hold upward running at whatsoever given time. Therefore, if you lot were previously running Prefork or Worker, you lot must offset disable it, in addition to and then enable Event.

    To disable the Prefork MPM, run this command:

    $ sudo a2dismod mpm_prefork

    To disable the Worker MPM, run this:

    $ sudo a2dismod mpm_worker

  3. Enable the Event MPM.

    $ sudo a2enmod mpm_event

    Note that the to a higher house enable in addition to disable commands are quite 'forgiving'. If you lot stimulate to enable an MPM that is already enabled, or disable an MPM that is already disabled, it volition precisely supply a harmless informational message.

Configuring Event MPM

To configure the Event MPM, modify its configuration file, /etc/apache2/mods-available/mpm_event.conf. Before making whatsoever changes, dorsum it upward using the next command:

$ sudo cp /etc/apache2/mods-available/mpm_event.conf{,.orig}

Edit the file to specify the next parameters:

 <IfModule mpm_event_module>      StartServers two     MinSpareThreads 25     MaxSpareThreads 75     ThreadLimit 25     ThreadsPerChild 25     MaxRequestWorkers 250     MaxConnectionsPerChild 10000     ServerLimit 12   </IfModule>   

The to a higher house configuration is what I recommend for an entry-level VPS (512MB RAM, single-core). You ask to adjust the parameters to satisfy your ain arrangement requirements. For a detailed explanation of the to a higher house parameters, click here. Note that the Event MPM shares the same parameters every bit the Worker MPM.

Installing PHP5 handler

To execute PHP code, Apache requires a PHP handler. PHP5-FPM is the PHP handler to work amongst the Event MPM.

For a novel PHP installation, install php5-fpm followed yesteryear the meta-package php5.

$ sudo apt-get install php5-fpm php5

In add-on to the to a higher house packages, I besides installed other PHP5 packages which WordPress requires. While they are non strictly relevant to optimizing concurrency, I've included them hither for completeness.

$ sudo apt-get install php5-mysql php5-gd php5-curl

Configuring virtual host

Suppose your WordPress website has the domain nurture example.com. To laid upward a virtual host amongst that domain name, follow the steps below:

  1. Create the Apache configuration file for example.com.

    Instead of creating the file from scratch, work the default site every bit a template.

    $ sudo cp /etc/apache2/sites-available/000-default.conf /etc/apache2/sites-available/example.com.conf

  2. Edit the configuration file.

    Customize the next site-specific parameters:

    ServerName example.com ServerAlias www.example.com ServerAdmin info@example.com DocumentRoot /var/www/example.com 
  3. Create DocumentRoot directory.

    $ sudo mkdir /var/www/example.com
    $ sudo chown -R <webuser>:<webuser> /var/www/example.com

    Notes:

    • WordPress should hold upward installed inward the directory /var/www/example.com. For instructions on how to install WordPress, refer to my earlier post.

    • The DocumentRoot directory should hold upward owned yesteryear a non-root user.

  4. Enable the novel site.

    $ sudo a2ensite example.com.conf

  5. Disable the default site.

    $ sudo a2dissite 000-default.conf

Configuring PHP handler

Follow the physical care for below to configure PHP5-FPM.

  1. Create a custom PHP configuration file for example.com yesteryear copying the template from the default site.

    $ sudo cp /etc/php5/fpm/pool.d/www.conf /etc/php5/fpm/pool.d/example.com.conf

  2. Edit the configuration file.

    Customize the next parameters.

    [example.com]
    user = <webuser>
    grouping = <webuser>
    withdraw heed = /var/run/php5-fpm_example.com.sock
    pm = dynamic
    pm.max_children = v
    pm.start_servers = two
    pm.min_spare_servers = 1
    pm.max_spare_servers = iii
    pm.max_requests = 2000

    Notes:

    • The user in addition to group parameters specify respectively the Unix user in addition to grouping names nether which the FPM processes volition run. You should specify a non-root user for both.

    • The listen parameter specifies the source address that the FPM volition withdraw heed to for receiving PHP requests. In this case, it volition withdraw heed to the Unix socket /var/run/php5-fpm_example.com.sock.

    • The balance of the parameters are for an entry-level VPS system. You should adjust their values to satisfy your arrangement requirements.

    • Click here for to a greater extent than details near the to a higher house parameters.

  3. Restart PHP5-FPM.

    $ sudo systemctl restart php5-fpm

Installing FastCGI

Apache requires a FastCGI module to interface amongst the external PHP5-FPM processes. You tin give notice work 1 of two FastCGI modules: mod_fastcgi in addition to mod_fcgid. Click here for a intelligence of their differences. This tutorial uses mod_fastcgi.

Before you lot install mod_fastcgi, you lot must:

  1. Enable non-free.

    Debian pre-packages the mod_fastcgi module inward the non-free archive expanse of its repositories. Make certain that non-free is included inward the /etc/apt/sources.list file.

  2. Disable mod_php.

    If Apache2 was previously installed amongst the Prefork MPM, most likely, it is configured to execute PHP using the mod_php module. In this case, you lot must disable the mod-php module before you lot install mod_fastcgi. Otherwise, the install volition neglect amongst the fault message, 'Apache is running a threaded MPM, but your PHP Module is non compiled to hold upward threadsafe. You ask to recompile PHP.'

    To disable mod_php, run this command:

    $ sudo a2dismod php5

To install mod_fastcgi, execute the next command:

$ sudo apt-get install libapache2-mod-fastcgi

Configuring FastCGI

  1. Back upward configuration file.

    Before you lot edit the configuration file /etc/apache2/mods-available/fastcgi.conf, dorsum it upward using the next command.

    $ sudo cp /etc/apache2/mods-available/fastcgi.conf{,.orig}

  2. Edit the file.

    Insert the next lines:

     <IfModule mod_fastcgi.c>    AddHandler php5-fcgi .php    Action php5-fcgi /php5-fcgi    Alias /php5-fcgi /usr/lib/cgi-bin/php5-fcgi_example.com    FastCgiExternalServer /usr/lib/cgi-bin/php5-fcgi_example.com -socket /var/run/php5-fpm_example.com.sock -pass-header Authorization    <Directory /usr/lib/cgi-bin>        Require all granted    </Directory>  </IfModule> 

    Notes:

    • example.com should hold upward replaced amongst your ain domain name.

    • To access the website, you lot ask to grant the proper permission explicitly using the Require all granted statement. Without it, access to the website volition hold upward denied amongst the fault message 'You don't choose permission to access /php5-fcgi/index.php on this server.'

  3. Enable additional modules.

    $ sudo a2enmod actions fastcgi alias

  4. Restart Apache.

    The concluding footstep is to restart Apache to brand all the to a higher house changes become live.

    $ sudo systemctl restart apache2

Threads inward action

Concurrency for WordPress occurs at both the webserver (Apache2) in addition to the PHP handler (PHP-FPM) levels. You tin give notice work the ps -efL command to monitor the processes in addition to threads at either level.

To monitor Apache processes in addition to threads, execute the next ps command.

$ ps -efL |grep apach[e]
www-data 31681 24441 31681 0 27 03:25 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 31681 24441 31684 0 27 03:25 ? 00:00:00 /usr/sbin/apache2 -k start
...

The minute in addition to the 4th columns are the physical care for ID (PID) in addition to the thread ID respectively. Note that the to a higher house output reports two unlike threads (31681 in addition to 31684) of the same physical care for (31681).

Execute the next command to monitor PHP.

$ ps -efL |grep ph[p]
source 24398 1 24398 0 1 Nov10 ? 00:00:17 php-fpm: master copy physical care for (/etc/php5/fpm/php-fpm.conf)
peter 31519 24398 31519 0 1 03:14 ? 00:00:17 php-fpm: puddle example.com
peter 31520 24398 31520 0 1 03:14 ? 00:00:16 php-fpm: puddle example.com
peter 31827 24398 31827 0 1 04:15 ? 00:00:15 php-fpm: puddle example.com

Conclusion

When traffic to your website increases over time, your webserver must scale upward to grip the increment inward traffic. This tutorial explains how to configure Apache2 in addition to PHP to optimize the number of concurrent connections. After you lot essay it out, if you lot however detect that your website cannot maintain upward amongst the traffic, you lot should reckon upgrading your VPS conception to choose to a greater extent than RAM.

If you lot are interested inward WordPress, delight refer to my before posts.

Berlangganan update artikel terbaru via email:

1 Response to "How To Optimize Apache Concurrency For Running Wordpress"

  1. Succeed! It could be one of the most useful blogs we have ever come across on the subject. Excellent info! I’m also an expert in this topic so I can understand your effort very well. Thanks for the huge help. 꽁머니

    ReplyDelete

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel