PHP default_socket_timeout Explained: Configuration, Network Timeouts, and Best Practices
Introduction
The default_socket_timeout directive defines the default timeout, in seconds, for network stream operations performed by PHP. It affects functions and extensions that use PHP streams, such as HTTP requests, FTP connections, and socket-based communication.
A suitable timeout helps prevent applications from waiting indefinitely for slow or unresponsive network services while still allowing enough time for legitimate requests to complete.
This guide explains how default_socket_timeout works, how to configure it, recommended values for different environments, and common troubleshooting scenarios.
Test Environment
What Is default_socket_timeout?
The default_socket_timeout directive specifies how long PHP waits for data during network communication before terminating the operation.
Example:
default_socket_timeout = 60
With this configuration, PHP waits up to 60 seconds when reading data from a network stream before reporting a timeout.
How default_socket_timeout Works
Suppose a PHP application requests data from an external API.
If the remote server does not respond within the configured timeout, PHP stops waiting and returns an error.
Check the Current Value
Run:
Example output:
default_socket_timeout => 60 => 60
Locate the Active php.ini File
Example:
Loaded Configuration File: /etc/php.ini
If your website uses PHP-FPM, modify the configuration file used by PHP-FPM.
Change default_socket_timeout
Open the PHP configuration file.
Find:
default_socket_timeout = 60
Example:
default_socket_timeout = 120
Save the file.
Restart PHP-FPM
Apply the new configuration.
Verify:
Expected output:
Active: active (running)
Verify the Configuration
Run:
Or create a PHP file:
Search for:
default_socket_timeout
Common Functions Affected
The following PHP functions may use the default socket timeout when no custom timeout is specified:
Many libraries built on PHP streams also inherit this value unless they explicitly define their own timeout settings.
default_socket_timeout vs max_execution_time
These directives control different aspects of application behavior.
Example:
A network timeout may occur even when the script has plenty of execution time remaining.
Common Issues
API Requests Time Out
If external services respond slowly, PHP may terminate the connection after reaching the configured socket timeout.
Possible solutions include:
Increase default_socket_timeout if longer waits are acceptable.
Configure a custom timeout in the HTTP client or library.
Investigate whether the remote service is responding slowly.
file_get_contents() Returns a Timeout Error
When using file_get_contents() with a URL, the request may fail if the remote server does not send data before the timeout expires.
Using a stream context with a custom timeout allows individual requests to override the default setting.
SMTP or FTP Connections Fail
Network services such as SMTP, FTP, or other TCP-based protocols may also be affected if they rely on PHP stream functions without specifying a custom timeout.
Recommended Values
Choose a timeout that matches the expected response time of the services your application communicates with.
Best Practices
Use the default timeout for general-purpose applications.
Configure shorter timeouts for latency-sensitive APIs.
Override the timeout for individual requests when appropriate instead of increasing the global value.
Restart PHP-FPM after modifying the configuration.
Monitor network performance before significantly increasing timeout values.
Conclusion
The default_socket_timeout directive controls how long PHP waits for data during network stream operations. It helps prevent applications from hanging indefinitely when communicating with slow or unavailable services.
For applications that rely heavily on external APIs, databases accessed through network sockets, or remote file operations, choosing an appropriate timeout improves reliability and provides a better user experience. When different services require different response times, using request-specific timeout settings is often preferable to increasing the global default.
Explore More
› PHP max_input_time Explained: Configuration, Timeouts, and Best Practices
› PHP max_execution_time Explained: Configuration, Timeouts, and Best Practices
› PHP opcache.max_accelerated_files Explained: Configuration, Performance, and Best Practices
› PHP opcache.validate_timestamps Explained: Configuration, Code Updates, and Best Practices