CentrioHost Blog

Stories and News from IT Industry, Reviews & Tips | Technology Blog


How to Configure Nginx and php-fpm for High Traffic Websites

This tutorial will help you to configure your cpnginx server for managing high traffic websites. In other words this will help you to configure your nginx+php-fpm in tcp mode. The Cpnginx software ( A complete nginx plugin for cpanel servers ) by default use the unix sockets for php-fpm. So if you have a site with high traffic in your server, then the unix socket configuration will span hundreads of php-fcgi process , which may result in increasing cpu and memory usage. You can see the cpu and memory usage of the php process using the linux command “top”.

Suppose if your site gets 1000+ concurrent connections , then spanning 100+ php process is not a good idea. At this point it is better to configure the php-fpm and nginx in TP mode. So what is TCP mode ? In this mode, the php-fpm will listen on a particular port ( eg : 9000 ) as a TCP service. All requests from nginx will pass to this port only. This method won’t increase the cpu usage or memory usage.

This article will help you to configure your high traffic website in TCP mode so that you can utilize your server resource in a better way. You may need to do the following steps in your cpnginx server.

  • 1. Configure php-fpm for user on specific php version
  • 2. Create a custom template for that domain

Let us assume we have a website fun.com with cpanel user “fun” and he use php56 as his php.

 

Configure php-fpm for the cpanel user “fun” with php version php56

You can see the php-fpm configuration file of this user is located on /opt/cpanel/ea-php56/root/etc/php-fpm.d/fun.conf . Now copy this file as follows,

# cp -f /opt/cpanel/ea-php56/root/etc/php-fpm.d/fun.conf /opt/cpanel/ea-php56/root/etc/php-fpm.d/fun_tcp.conf

Now edit this file /opt/cpanel/ea-php56/root/etc/php-fpm.d/fun_tcp.conf and make the changes as follows,

[fun]
user = fun
group = fun
listen = 127.0.0.1:9000     # This is the tcp port configuration
listen.owner = fun
listen.group = nobody
listen.mode = 0660
pm = ondemand
pm.max_children = 256
pm.process_idle_timeout = 15
pm.start_servers = 8
pm.min_spare_servers = 5
pm.max_spare_servers = 10 
pm.max_requests = 6000
php_admin_value[sys_temp_dir] = "/tmp"
php_admin_value[sys_temp_dir] = "/tmp"
php_value[session.save_path] = "/tmp" 
env[TMPDIR] = "/tmp"

Now restart the php56-fpm service

# systemctl restart ea-php56-php-fpm.service

If there is no errors in the configuration you can see a php-fpm tcp-connection listening on port 9000 . You can use the following command to see this .

# netstat -pant | grep :9000

Now we have php56-fpm on port 9000 as a tcp service for the cpanel user “fun”. Next step is to configure nginx with this port.

Create a custom nginx vhost template for the domain fun.com

As cpnginx is based on templates , you can create a vhost template file (funcom.conf). You need to follow the vhost template creation documentation from here.

So now you have a custom nginx vhost template file in /etc/cpnginx/templates/custom/funcom.conf . you need to make the following changes in this file.

Replace every occurrence of the following line in this file ,

fastcgi_pass unix:${FPM_SOCK};

With the following line

fastcgi_pass 127.0.0.1:9000;

Now go to WHM -> Plugins -> Cpnginx -> List Domains and select the domain fun.com, then assign this new template and php version 5.6 for that domain. That will do the rest of the job.

This is how you change a unix socket based php-fpm to tcp in Cpnginx server. So the question is how you can create this configuration for more than one cpanel users. It is simple during the php-fpm tcp configuration time, increae the value of tcp port ( eg: 9001 ), so that you will have different php-fpm tcp ports for different users.

Subscribe Now

10,000 successful online businessmen like to have our content directly delivered to their inbox. Subscribe to our newsletter!

Archive Calendar

SatSunMonTueWedThuFri
 1
2345678
9101112131415
16171819202122
23242526272829
30 

Over 20000 Satisfied Customers!

  • web hosting reviewer
    Valerie Quinn
    CTO, Acteon Group

    Centriohost staff were fantastic, I had a concern with a domain and they got back to me very quickly and they helped me to resolve the issue! ~ . . . Read more

  • Joomla hosting reviewer
    Collin Bryan
    Photographer, Allister Freeman

    I'm using centrio for my portfolio since 2006. The transition was seamless, the support was immediate, and everything works perfectly. ~ . . . Read more

  • dedicated server reviewer
    Harry Collett
    Actor, A&J Artists

    Very easy to understand & use even though I am not very technologically minded. No complications whatsoever & I wouldn't hesitate to recommend it to all. ~ . . . Read more

  • vps web hosting reviewer
    Porfirio Santos
    Technician, Diageo PLC

    Centrio support team have been amazingly responsive and helpful to any of my queries, thank you so much to the Centriohost have been amazingly responsive and helpful to any of my queries 👍👍👍 ~ . . . Read more

  • wordpress hosting plans reviewer
    Catherine Auer
    Doctor, SmartClinics

    Anytime I've had a problem I can't solve, I've found Centriohost to be diligent and persistent. They simply won't let an issue go until the client is happy. ~ . . . Read more

  • reseller hosting reviewer
    Effectivo Social
    Freelancer, Fiverr

    Recommend their shared hosting for all my SME web design clients. Their cloud or VME offerings are too great to deal with. Pricing is perfect and suitable for all users (͠≖ ͜ʖ͠≖) 👌 ~ . . . Read more

Top