Configure php-fpm and nginx to run in low memory server

It was pain in the ass to have php-fpm and nginx together to serve php app, especially when you’re running on low-memory server, especially when you’re running cms like wordpress which basically heavy duty. I had a Centos server running with memory only 1024Mb (1Gb). My web kept crushed every single time. And the problem stil remains, memory leak. I don’t what the root cause was. I still don’t know what that is but I think it’s something to do with php-fpm configuration or even nginx.

Just several days ago I had my blog up and running with wordpress in the same type of server, same OS (Centos) with the same memory (1Gb) and since I didn’t want to use apache, guess I had to get my nginx and php-fpm working together again. I thought my web would be running very smoothly since the blog was still unlikely to receive much traffic. But I was wrong, somehow the memory was leaked again. My server only running with 1024Mb, but I thought It didn’t matter. The only way to get this problem mitigated is to tune up both the php-fpm and nginx configurations which I never liked this. But I need this, so let’s get this done.

I’m running Centos 6.8 with nginx 1.12.0 & php 5.3.3

After some time, I think I come up with this nicely php-fpm and nginx configured.

nginx configuration :

server {
  listen 80;
  server_name www.pulpn.com pulpn.com;
  charset utf-8;
  gzip on;
  gzip_vary on;

  server_name_in_redirect off;
  server_tokens off;
  client_header_buffer_size 128;
  client_max_body_size 8m;

  fastcgi_connect_timeout 300;
  fastcgi_send_timeout 300;
  fastcgi_read_timeout 300;
  fastcgi_buffer_size 32k;
  fastcgi_buffers 4 32k;
  fastcgi_busy_buffers_size 32k;
  fastcgi_temp_file_write_size 32k;

  location / {
    root /srv/www/myblog;
    index index.php;
    try_files $uri $uri/ /index.php?q=$uri&$args;
    location ~* \.php {
      try_files $uri =404;
      include fastcgi_params;
      fastcgi_index index.php;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_pass  127.0.0.1:9000;
    }
  }
}

php-fpm configuration :

[www]
; this config is necessary
listen = 127.0.0.1:9000
listen.allowed_clients = 127.0.0.1
listen.owner = nginx
listen.group = nginx
listen.mode = 0666
user = nginx
group = nginx
pm = static
pm.max_children = 8
pm.start_servers = 10
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 1024
request_terminate_timeout = 0

; this config is just preference, you can change if you want
slowlog = /var/log/php-fpm/www-slow.log
php_admin_value[error_log] = /var/log/php-fpm/www-error.log
php_admin_flag[log_errors] = on
php_value[session.save_handler] = files
php_value[session.save_path] = /var/lib/php/session

My server after configured, just a quick peek :


Leave a Reply

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