PHP default_socket_timeout Explained: Configuration, Network Timeouts, and Best Practices

Published: 2026-07-08
Google AdSense 广告位 (待申请通过后开启)

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

Operating System : AlmaLinux 10
PHP Version      : PHP 8.3
Web Server       : Nginx
PHP-FPM          : Enabled
Architecture     : x86_64
Last Tested      : July 2026

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.

PHP Script
      │
      ▼
HTTP Request
      │
      ▼
Remote Server
      │
 ┌────┴────┐
 │         │
Response  No Response
 │         │
 ▼         ▼
Continue  Wait
             │
             ▼
default_socket_timeout
             │
             ▼
Timeout Error

If the remote server does not respond within the configured timeout, PHP stops waiting and returns an error.

Check the Current Value

Run:

php -i | grep default_socket_timeout

Example output:

default_socket_timeout => 60 => 60

Locate the Active php.ini File

php --ini

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.

sudo nano /etc/php.ini

Find:

default_socket_timeout = 60

Example:

default_socket_timeout = 120

Save the file.

Restart PHP-FPM

Apply the new configuration.

sudo systemctl restart php-fpm

Verify:

sudo systemctl status php-fpm

Expected output:

Active: active (running)

Verify the Configuration

Run:

php -i | grep default_socket_timeout

Or create a PHP file:

<?php
phpinfo();

Search for:

default_socket_timeout

Common Functions Affected

The following PHP functions may use the default socket timeout when no custom timeout is specified:

file_get_contents()
fopen()
fsockopen()
stream_socket_client()

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.

| Directive                | Purpose                                         |
| ------------------------ | ----------------------------------------------- |
| `default_socket_timeout` | Limits how long PHP waits for network data      |
| `max_execution_time`     | Limits the total execution time of a PHP script |

Example:

PHP Script
      │
      ▼
Waiting for API Response
(default_socket_timeout)
      │
      ▼
Processing Response
(max_execution_time)

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

| Application               | Recommended Value |
| ------------------------- | ----------------- |
| General websites          |        60 seconds |
| Internal APIs             |     30–60 seconds |
| External APIs             |    60–120 seconds |
| Slow network environments |   120–300 seconds |

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

Technology Guides →

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

Southeast Asia Insights →