FeaturesWP CloudPricingEE CLIContactMy Account
Features
Team Access & User Permissions
Install
Resources
Community
CLEAR
Tutorials
PHP
PHP-FPM: Socket vs TCP/IP and sysctl tweaking
Topics
On this page
-
- Getting Started
- Git and File-System Layout (non-default setup)
- Version Constraints
- Managing Themes and Plugins (for entire WordPress site)
- Updating Themes and Plugins (for entire WordPress site)
- Adding Composer Support to Your Own Themes and Plugins (for developers)
- Using Composer to Manage Own Theme/Plugin Dependencies (for developers)
-
- Assigning Multiple IP Addresses to Single LAN Card
- Disable IPv6 on Ubuntu 12.04
- dsh – distributed shell
- fdupes – find & replace duplicate files with hardlinks
- GPG Keys Cheatsheet
- Increase "Open Files Limit"
- kill all php, nginx, mysql or any kind of processes
- Passwordless Authentication for SSH
- Screen
- search replace in multiple files useing grep xargs sed
- Setup sftp
- sysctl.conf
- Timezone Sync
- Using Google Public DNS on Ubuntu Server
- Using openssl to match private key, cerificate and CSR
- Configure Postfix to Use Gmail SMTP on Ubuntu
- Logrotate Example for Custom Logs
- PHP 5.5, MySQL, Postfix, Nginx and WordPress on Ubuntu
- Serverdensity
- Upgrade Ubuntu 10.04 to 12.04
- Using find and sed to replace strings in multiple files
- Webmin
-
- .my.cnf – mysql user & password
- Analyse slow-query-log using Anemometer
- Improving MySQL Query Cache
- MySQL Query Profiling
- Reset MySQL root password
- tuning-primer.sh – an alternative for mysqltuner
- Using MariaDB 10
- Install/Upgrade to MySQL 5.6 on Ubuntu 12.04 LTS
- Change WordPress Domain Name
- Character Sets and Collations
- Enable innodb_file_per_table
- Enable Remote Access (Grant)
- Installing Percona Toolkit
- Using tmpfs for temporary folder creation
- YARPP and InnoDB
- Convert MyISAM to InnoDB
- Using tmpfs for mysqldump & mysqlimport
- Convert from INNODB to MYISAM
- Analyse slow-query-log using mysqldumpslow & pt-query-digest
- mysqlcheck with cron to optimize automatically
- Using MySQLTuner to Optimize MySQL configuration
-
-
- Directly connect to PHP-FPM
- Generating core-dump for php5-fpm
- GeoIP
- Having php5 and php7 on same system.
- Install Xdebug and configure it with webgrind
- Installation of PHP Code Review Tools
- memcache
- PHP's Zend Opcache Config & Web Viewer
- Setting Sqlite support with PHP
- Using HHVM with PHP-FPM Fallback
- Using Redis for PHP Sessions on Ubuntu Server
- Installing PhpMyAdmin Quickly
- Moving PHP's session storage to tmpfs
- Install Xdebug and configure it with Netbeans
- Nginx – Enable PHP-FPM Status Page
- PHP-FPM: Socket vs TCP/IP and sysctl tweaking
- APC Cache Optimization & Monitoring Using Web Interface
- Checking if PHP/WordPress can send mails
- Debugging PHP Scripts Using slow_log and more
- Increase file upload size limit in PHP-Nginx
- Increase PHP script execution time with Nginx
-
- Performance Optimization
- Remove Query string of css/js files
- Setup ElasticSearch with ElasticPress
- Using Redis for WordPress Object-Cache
- WooCommerce Window Shopping Caching Technique
- Moving WordPress To New Server (Faster)
- Better wp-cron using linux's crontab
- 404 error- Page not Found!!!
- Debugging WordPress with Nginx
Tweaking FPM config
Test
Using TCP/IP for FPM
Changes to Nginx
Sysctl.conf Tweaking
PHP-FPM: Socket vs TCP/IP and sysctl tweaking
Tweaking FPM config
You may also need to tweak PHP-FPM config to match new sysctl.conf settings.
Open PHP-FPM pool config file:
vim /etc/php5/fpm/pool.d/www.conf
Look for line:
;listen.backlog = 128
Change it to:
listen.backlog = 65536
Restart php5-fpm service.
service php5-fpm restart
Additionally, make sure that operating system limit allows for above value.
Run:
sysctl net.core.somaxconn
Should display value 65536 or higher.
If no, then run following commands to verify.
echo "net.core.somaxconn=65536" >> /etc/sysctl.conf
sysctl -p
Test
Check FPM status page via web interface or command-line:
curl localhost/status
It will display parameters like:
pool: www
process manager: ondemand
start time: 06/Nov/2016:19:02:09 +0800
start since: 2
accepted conn: 4
listen queue: 0
max listen queue: 0
listen queue len: 65536
idle processes: 1
active processes: 2
total processes: 3
max active processes: 3
max children reached: 0
slow requests: 0
In the output, listen queue lenvalue should match listen.backlog value set in the config.
Using TCP/IP for FPM
Sockets are slightly faster as compared to TCP/IP connection. But they are less scalable by default.
If you start getting errors like below (as faced ovidiu here)
connect() to unix:/var/run/php5-fpm.sock failed or **apr_socket_recv: Connection reset by peer (104)**
Then it means you need to either switch to TCP/IP or tweak with linux-system parameter so that your OS can handle large number of connections.
Open PHP-FPM pool config file
vim /etc/php5/fpm/pool.d/www.conf
Replace line:
listen = /var/run/php5-fpm.sock
by line:
listen = 127.0.0.1:9000
Changes to Nginx
Next, open Nginx virtual-host config file(s).
Look for line
fastcgi_pass unix:/var/run/php5-fpm.sock;
Replace it with
fastcgi_pass 127.0.0.1:9000;
Important: Reload php-fpm and nginx so that changes can take effect.
service php5-fpm reload && service nginx reload
Sysctl.conf Tweaking
Finally, don’t forget to tweak Linux’s sysctl values by following this – http://rtcamp.com/tutorials/linux/sysctl-conf/
Nginx – Enable PHP-FPM Status Page
PREVIOUS
APC Cache Optimization & Monitoring Using Web Interface
NEXT
Comments
-
How to calculate values of /etc/sysctl.d/custom.conf for my hardware?
-
There is no mathematical formula. I randomly increase limits till I stop getting limit-related errors.
May not be good idea, but finding a logical method to calculate values is on my to-do list.
-
I randomly increase limits till I stop getting limit-related errors.
Just like pros. 😉
I came here looking for a tuning solution for Symfony+nginx+php-fpm setup, my lighty+spawn-fcgi was faster and I couldn’t believe it, just switched Symfony to prod mode and then nginx+php-fpm was faster than lighty, odd.
-
-
-
Where do you expect the limit-related errors to be logged?
-
@Tarjei
Not all limit related errors will be logged. In some cases, process will just crash.
In some cases, errors can be seen in logs like: “Too many open files (24)” (covered in this post directly)
Then there are socket-related errors if system is subjected to, too many concurrent connections.Not all settings in above article are related to error. For example, I don’t thnik any process will complain about values like
net.core.rmem_max
-
-
Hi Rahul,
When I apply these configuration changes as described above for a WordPress site with Nginx + PHP5-FPM my site stops loading entirely. Are there any other changes I should be making to a Digital Ocean box currently setup for sockets for TCP? I’ve done nothing to configure TCP, other than what you advise in the sysctl.conf article above (I made those changes as well).
Thanks,
Paul-
This is really strange. Can you remove changes made to sysctl and check if site starts working back?
If removing changes from sysctl work, you may try adding lines 1-by-1.
All our config and tutorials are tested on DigitalOcean only.
-
I’ve removed the changes to sysctl completely, and still no dice. Here are my various nginx configurations (as they stand prior to implementing the information you provided above):
I’ve pasted nginx.conf here: http://pastebin.com/fD9HjqpB
General wordpress configuration file: http://pastebin.com/CVkHZtvU
And the site-specific config here: http://pastebin.com/KsVECwEaPerhaps you might see something that would conflict with the changes I’m trying to implement?
The entire goal of making this switch is to overcome this error on a site with growing traffic:
connect() to unix:/var/run/php5-fpm.sock failed (11: Resource temporarily unavailable) while connecting to upstream-
Got it sorted! The offending line in my nginx configuration was the shared declaration for phpmyadmin and .php:
location ~* /phpmyadmin/.*\.php$ {Split them up and your configurations work great. Thanks Rahul!
-
Glad to know your issue got fixed. 🙂
-
-
-
-
-
Amit
I came across this post while configuring <a href="https://github.com/afaqurk/linux-dash" rel="nofollow">linux-dashboard</a> which requires php-fpm to be <a href="https://gist.github.com/sergeifilippov/8909839" rel="nofollow">configured</a>. I modified
www.confto use TCP/IP. After restarting nginx and php-fpm services the dashboard does not fetch the actual values. What changes are required in sysctl-conf?-
You need to make sure that fastcgi_pass in nginx matched listen in php-fpm config. You can use any of TCP/IP or Unix-Socket as long as nginx and php5-fpm values match.
In your case, looks like there is a mismatch between 2 values.
-
Features
- Server Setup & Management
- Site Creation & Management
- Create WP Cloud Sites
- Speed & Caching
- Firewall Protection
- Backups & Recovery
- Logs & Monitoring
- Deployments & Developer Tools
- Team Access & User Permissions
Useful Links
Product
Other Products
Subscribe for curated product updates, tips, and announcements.
"*" indicates required fields
Name
This field is for validation purposes and should be left unchanged.
Email*
SIGN ME UP
Δ
© rtCamp, since 2009. All rights reserved.