Configuring PHP-FPM for WordPress on Nginx

Contents

Introduction

WordPress is one of the most widely used CMS platforms in the world. When paired with Nginx and PHP-FPM, you achieve a highly performant, scalable and efficient environment for your site. This article explores in detail how to configure PHP-FPM for WordPress on Nginx, covering installation, tuning, security and monitoring.

Prerequisites

  • Linux server (Ubuntu/Debian/CentOS) with root or sudo access
  • Nginx installed and running
  • PHP (7.4, 8.0, or later) with PHP-FPM package
  • Basic familiarity with shell, file permissions and editing config files
  • WordPress files present in your web root or a subdirectory

1. Installing PHP and PHP-FPM

  1. Update packages
    sudo apt update ampamp sudo apt upgrade (Debian/Ubuntu)
    or sudo yum update (CentOS/RHEL).
  2. Install PHP-FPM
    sudo apt install php-fpm php-mysql
    (package names may vary: php7.4-fpm, php8.0-fpm).
  3. Verify service
    systemctl status php7.4-fpm (adjust version).

2. Configuring PHP-FPM Pool

PHP-FPM uses pool definitions typically in /etc/php/7.4/fpm/pool.d/www.conf. Key directives:

Directive Description
user / group System user and group to run PHP processes (e.g., www-data).
listen Socket or TCP address (unix:/run/php/php7.4-fpm.sock or 127.0.0.1:9000).
pm Process manager: static, dynamic, or ondemand.
pm.max_children Max simultaneous PHP-FPM processes.
pm.start_servers Initial processes (dynamic).
pm.min_spare_servers Minimum idle processes.
pm.max_spare_servers Maximum idle processes.

Example Pool Configuration

[www]
user = www-data
group = www-data
listen = /run/php/php7.4-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
php_admin_value[upload_max_filesize] = 64M
php_admin_value[post_max_size] = 64M

3. Nginx Configuration for WordPress

Add or edit your server block, typically in /etc/nginx/sites-available/wordpress:

server {
nbspnbsplisten 80
nbspnbspserver_name example.com
nbspnbsproot /var/www/wordpress
nbspnbspindex index.php index.html index.htm
nbspnbsplocation / {
nbspnbspnbspnbsptry_files uri uri/ /index.phpargs
nbspnbsp}
nbspnbsplocation ~ .php {
nbspnbspnbspnbspinclude fastcgi_params
nbspnbspnbspnbspfastcgi_param SCRIPT_FILENAME document_rootfastcgi_script_name
nbspnbspnbspnbspfastcgi_pass unix:/run/php/php7.4-fpm.sock
nbspnbspnbspnbspfastcgi_buffers 16 16k
nbspnbspnbspnbspfastcgi_index index.php
nbspnbsp}
nbspnbsplocation ~ .(jscsspngjpgjpeggifico) {
nbspnbspnbspnbspexpires max
nbspnbspnbspnbsplog_not_found off
nbspnbsp}
}

Enable and test:

  • ln -s /etc/nginx/sites-available/wordpress /etc/nginx/sites-enabled/
  • nginx -t ampamp systemctl reload nginx

4. Performance Optimizations

  1. Enable OPcache in /etc/php/7.4/fpm/php.ini
    opcache.enable=1
    opcache.memory_consumption=128
  2. Microcaching in Nginx:
    fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=WORDPRESS:100m inactive=60m
    fastcgi_cache WORDPRESS
  3. Gzip Compression in Nginx:
    gzip on gzip_types text/css application/javascript
  4. Database Indexes: optimize WP tables via official guide.

5. Security Best Practices

  • Deny access to sensitive files:
    location ~ /(wp-config.phpreadme.html) { deny all }
  • Limit methods:
    if (request_method !~ ^(GETPOST)) { return 444 }
  • Hide PHP version: set expose_php = Off in php.ini
  • Use SSL/TLS: obtain certificates via Lets Encrypt and enforce HTTPS

6. Monitoring amp Troubleshooting

Monitoring

  • PHP-FPM Status: enable in pool config (pm.status_path = /status) and add Nginx location.
  • Nginx Stub Status: location /nginx_status { stub_status on allow 127.0.0.1 deny all }.
  • Use htop, netdata or Prometheus exporters.

Common Issues

  • 502 Bad Gateway: PHP-FPM not running or wrong socket path check listen and fastcgi_pass.
  • Permission Denied: ensure www-data:www-data owns web root and socket.
  • Slow queries: enable MySQL slow query log.

Conclusion

By carefully installing and configuring PHP-FPM for your WordPress site on Nginx, you lay the foundation for a high-performance, secure and scalable platform. From pool tuning to Nginx directives, caching strategies, and security hardening, each step contributes to reliability. For further reading, consult the Nginx FastCGI module docs and the official PHP-FPM manual.



Acepto donaciones de BAT's mediante el navegador Brave 🙂



Leave a Reply

Your email address will not be published. Required fields are marked *