Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks - Comment Page: 2

I wrote before a guide Howto install Nginx/PHP-FPM on Fedora 29/28, CentOS/RHEL 7.5/6.10, but this guide is just installation guide and many cases Nginx and PHP-FPM basic configuration is good enough, but if you want to squeeze all the juice out of your VPS or web server / servers and do your maintenance work little bit easier, then this guide might be useful. These tips are based entirely on my own experience, so they may not be an absolute truth, and in some situations, a completely different configuration may work better. It's also good to remember leave resources for another services also if you run example, MySQL, PostgreSQL, MongoDB, Mail server, Name server and/or SSH server on same...

168 comments on “Nginx and PHP-FPM Configuration and Optimizing Tips and Tricks - Comment Page: 2

1 2 3 4 6
    1. HI JR ,

      We noticed that our PHP-FPM processes is burning through at 300Mb+ each : here are the screenshots :
      http://grab.by/cUnc
      http://grab.by/cUng

      as u can see we are running out of 8 GB ram pretty quick…not sure whats causing this issue

      please let us know what do here – we are hosting an e-commerce shopping cart based on Magento.

      Let us know.

      Reply
      • Hi again Karan,

        Thank you for this information.

        Actually VIRT is not right column to follow in this case. VIRT column represents how much memory the program is able to access at the present moment. All memory usage combined memory it has mapped into itself, files on disk that have been mapped into it and memory shared with other processes.

        RES stands for the resident size, which is an accurate representation of how much actual physical memory a process is consuming. (and also %MEM column value is calculated directly from RES).

        So RES is right / much better column to follow. Memory usage per process is therefore around 75-90 Mb (~1 percent of 8 Gb). Let’s calculate again:
        4096 / 90 = 45.51

        Based on this information I recommend to try following values then:

        
        pm.max_children = 46
        pm.start_servers = 6
        pm.min_spare_servers = 6
        pm.max_spare_servers = 12
        pm.max_requests = 250
        

        And

        
        emergency_restart_threshold 10
        emergency_restart_interval 1m
        process_control_timeout 10s
        

        You should definitely adjust these values​​, when you see the real situation.

        However, the problem is indeed Magento, rather than the configuration of the server. Magento is just too large and too heavy to any environment. Magento hosting is really tedious and unpleasant task.

        Please let me know if these values ​​work, or if you have problems?

        Reply
        • Thanks JR ,

          Seems some improvements.

          Please see below :

          Cpu(s): 11.6%us, 3.6%sy, 0.0%ni, 84.8%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
          Mem: 8163952k total, 3282776k used, 4881176k free, 375324k buffers
          Swap: 4192956k total, 0k used, 4192956k free, 1316412k cached
          PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
          27992 apache 16 0 340m 80m 41m R 40.3 1.0 0:07.54 php-fpm
          27985 apache 15 0 334m 73m 40m S 16.0 0.9 0:14.53 php-fpm
          27984 apache 16 0 338m 77m 40m S 2.7 1.0 0:04.81 php-fpm
          5114 mysql 15 0 1534m 708m 5996 S 2.0 8.9 226:07.99 mysqld
          27991 root 15 0 12760 1176 828 R 0.3 0.0 0:00.32 top
          1 root 15 0 10320 688 580 S 0.0 0.0 0:00.68 init
          2 root RT -5 0 0 0 S 0.0 0.0 0:00.00 migration/0

          Few days back my total memory went right upto 7.5 GB and there was only a free memory of 500-700 MB

          Then i typed this command to clear cache
          sudo sync
          sudo echo 3 | sudo tee /proc/sys/vm/drop_caches and i had 6 GB free. Do you know how do i limit this cache. It seems to be eating up all the memory on the server, if i dont regularly flush it.

          Do you think this heavy caching could be caused by varnish cache / APC cache etc?? is there anyway i can limit this.

          Reply
          • You are welcome!

            Actually ~3.2 Gb used from 8 Gb looks very low, normally Linux use ~90 % all available memory. So that 7.5 Gb / 8 Gb is much more normal and better situation.

            Do you have Varnish running and PHP APC installed on this server?

            Reply
            • Hi JR,

              Varnish Cache was running, we disabled it after the server crashed thinking we needed to save some memory or the cache was being put on the memory and thus crashing the server. APC is enabled on this server.

              Few mins back we were again not able to access the server. I guess my server guys were running a stress test on the server. They gave me back with these details.
              Total transferred: 2976410 bytes
              HTML transferred: 240762 bytes
              Requests per second: 96.36 [#/sec] (mean)
              Time per request: 10377.283 [ms] (mean)
              Time per request: 10.377 [ms] (mean, across all concurrent requests)
              Transfer rate: 28.01 [Kbytes/sec] received

              Kindly suggest what do you advice.

              Thanks again for all the help provided.

              Reply
    2. Thank you so Much JR ,

      we are going to take a look and get back to you.

      Reply
    3. Hello JR,

      I would greatly appreciate if you could take a look at my Nginx-FPM config http://pastebin.com/93FQXLq0 The server is similar to Karan’s (Xeon 3210 QUAD with 8GB RAM) and has heavy traffic (around a thousand concurrent connections average), so any suggestion to improve performance would be greatly appreciated.

      Regards

      Reply
      • Hello Jim,

        I’m very sorry, I forgot to answer your question…do you have some problems with current configuration?

        Reply
    4. Since you are in a localhost environment, instead of using tcp 127.0.0.1 to connect Nginx with PHP-FPM, just use socket connections? Faster!

      Reply
    5. Thank you! This is a great post.

      Reply
    6. Really good article – thanks for the effort, it really helped me a lot! :)

      Reply
    7. Hello JR,
      Can you help me configurating an optimal nginx pm? I am really confused with the formula.
      I have a VPS of 1GB RAM and 10k daily visitors. Please help

      Thanks :)

      Reply
    8. Hey :)
      i was wondering if the listen in the php-fpm.conf can take an ipv6 address plus the port instead of the 127.0.0.1
      because i can start the php-fpm but after that i can see that the @ where its listening is really strange and is totaly different from the ipv6 that i gave to listen …

      Thank you for your answers !!!

      Reply
      • Hi Nao,

        Very good question :) Unfortunately I think that the php-fpm can’t listen/handle ipv6 requests, if we believe PHP (php-fpm) manual:

        listen string
        The address on which to accept FastCGI requests. Valid syntaxes are: ‘ip.add.re.ss:port’, ‘port’, ‘/path/to/unix/socket’. This option is mandatory for each pool.

        So it looks like only the ipv4 address + port, port or unix socket are supported…

        Reply
    9. Just wanted to mention that on Ubuntu, after installing FPM with aptitude, its config file is in /etc/php5/fpm (the blog post mentioned some different paths).

      Thanks for an informative guide!

      Reply
    10. Hi JR, great explanation! I’m using nginx since a few years ago, but your post helped me migrate to php 5.3!
      Keep up the good work!!

      Reply
    11. Hi!

      Can I use multiple Unix socket connection, means for every site defined by /etc/nginx/sites-available/.conf I use a deditcated Unix socket connection?

      Does this make sense in the context of security?

      THX

      Reply
      • Hi Thomas,

        It shouldn’t be a problem, if you configure also one Unix socket per PHP-FPM pool.

        In security context this doesn’t make big difference. If you run this setup just one server then this setup is just as secure as your server with one Unix socket, on the other hand you can restrict permissions of PHP-FPM pool users and groups to access only selected files.

        Reply
        • So, in this context of security it makes sense to have a single Unix sockets for all connections, but specific user accounts with restricted permissions for every site.

          THX

          Reply
    12. alright atm i have nginx installed in a few vm’s so of course port 80 will be in use how can i change nginx.conf so i can access saidsite.com instead of saidsite.com:port

      Reply
      • Hi derp,

        Use listen 80; following inside you server block:

        
        server {
                ...
        
                listen   80;
        
                ...
        }
        
        Reply
    13. Hi, thanks for the post. Please I have a question.

      I’m running nginx on one of 3 jails on a Freebsd installation server.
      Now one of the applications is giving this error:

      increase post_max_size and upload_max_filesize to 10M
      Upload

      I don’t how how to increase this.
      It’s simple enough in Centos where I do this in php.ini.

      In Freebsd, I don’t know where to change it. I’ve found several copies of php.ini here and there and increased the value there. I restarted nginx, but no luck.

      Kindly help, thanks.

      Reply
      • Hi ginger,

        Actually you should restart php-fpm instead nginx.

        I can’t say directly what php.ini your system uses, but it might be simply /etc/php.ini or /usr/local/etc/php.ini.

        Reply
    14. I found the php.ini in /usr/local/etc/php.ini

      I increased the limit as instructed, “increase post_max_size and upload_max_filesize to 10M”

      and then went to /usr/local/sbin and did service php-fpm restart

      It restarted but did not solve the problem

      I updated two copies of php.ini – one in the jail housing nginx, and the other in the main root of the server.

      Any ideas please? Thanks.

      Reply
      • Hi again ginger,

        One thing came to my mind. So some application shows:
        increase post_max_size and upload_max_filesize to 10M

        The problem might also be nginx, not PHP, so you can try to set client_max_body_size in your nginx config.

        Like:

        
        client_max_body_size 10m;
        
        Reply
    15. You obviously have no idea what you’re talking about — stop misinforming people. Some examples…


      location ~* \.php$ {
      try_files $uri /index.php;

      Max request per process is unlimited by default, but it’s good to set some low value, like 200 and avoid some memory issues. This style setup could handle large amount of requests, even if the numbers seems to be small.

      Reply
      • Hi Derek,

        And thanks, I updated first.

        Second is based entirely on my own experience (as I tell at the beginning of this page) and if you read PHP-FPM manual then you know why this pm.max_requests even exists. From PHP-FPM configuration manual:

        The number of requests each child process should execute before respawning. This can be useful to work around memory leaks in 3rd party libraries. For endless request processing specify ‘0’. Equivalent to PHP_FCGI_MAX_REQUESTS. Default value: 0.

        Do you think that PHPs manual will lead people astray?

        And you (with arrogant attitude) obviously think that PHPs 3rd party libraries does never have any memory leaks?
        Or you maybe could tell some better method avoid PHP and PHPs 3rd party libraries memory leaks?

        Reply
1 2 3 4 6

Leave a Reply to Chemi Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close