Script timed out before returning headers: index.php

Script timed out before returning headers is one of the most common errors you will see while developing web applications, or if you are a system administrator. It can be caused by many different things, but it’s always related to timeout configurations (or a very bad programmed script).

How to fix Script timed out before returning headers error?

First: Script timed out before returning headers is just a normal issue on web development, and it can affect many kind of programming languages.

We will focus on php timeous, that are often the cause of the issue, however if your programming language is python, perl or another you should explore the error from that point.

As we said before, there are a few reasons to explain this issue, and it’s related to the timeouts set in various web services like: Apache, PHP and MySQL. Let’s start exploring each one of the options.

Apache Timeouts

Where can I find the TimeOut variable in Apache service configuration? On cPanel servers you can modify and see the current value in

WHM > Apache Configuration > Global Configuration area.

There is a chance you have a very low timeout and this may be causing the famous “Script timed out before returning headers” issue. Try raising the value up to 60 or 120 for example.

You can also modify the same variable by editing /usr/local/apache/conf/httpd.conf

nano -w /usr/local/apache/conf/httpd.conf

After you are done editing, save and run this commands to apply the changes:

/usr/local/cpanel/bin/apache_conf_distiller --update
service httpd restart

MySQL timeouts

Another reason for this error can be low MySQL timeouts, many times I’ve seen MySQL queries requiring more execution time than the one set in the MySQL configuration, it’s not common at all, but on badly programmed queries, or huge databases it can happen.

In this scenario “Script timed out before returning headers” error may appear together with anoter one: “MySQL server has gone away” may appear. In order to solve it, you just need to adjust the wait_timeout variable in /etc/my.cnf file:

wait_timeout = 120 

Then restart MySQL to apply changes:

service mysqld restart

PHP timeouts

When your script timeout before completing its execution one of the varibles to check is max_execution_time in PHP server configuration. On cPanel servers you can find this variable at:

WHM > PHP Configuration Editor > Options & Information > max_execution_time

Or you can also edit using the terminal:

nano -w /usr/local/lib/php.ini

Restart to apply changes:

service httpd restart

If you don’t have access to the shell, you may get the max_execution_time value using a simple PHP script:

<?php
echo ini_get('max_execution_time');
?>

Then you can ask your hosting provider to raise the limit a little bit more for your account.

FastCGI / mod_fcgid timeouts (running with Apache)

FastCGI is an alternative way to execute PHP scripts than using mod_php (the traditional/default way to do it with Apache web server). mod_fcgid is responsible for executing PHP scripts if you are using this FastCGI configuration, and is also the one you should look at when you are facing this kind of timeout issues.

On old mod_fcgid configurations you may find this variables, be sure to set each one high as you see it here, or raising up to 3600.

<IfModule mod_fcgid.c>
IdleTimeout 1800
IPCCommTimeout 1800
BusyTimeout 1800
ProcessLifeTime 1800
IPCConnectTimeout 20
</IfModule>

On newer versions of mod_fcgid variables are as you see below:

<IfModule mod_fcgid.c>
FcgidIdleTimeout 1800
FcgidProcessLifeTime 1800
FcgidBusyTimeout 1800
FcgidIOTimeout 1800
FcgidConnectTimeout 20
</IfModule>

This variables must be placed inside your Apache configuration. After you are done, restart httpd to apply changes:

service httpd restart

PHP-FPM timeouts (running with Nginx)

PHP-FPM means FastCGI Process Manager, and it’s an alternative way to execute PHP scripts, especially high traffic websites.

How can I increase PHP execution time if I’m using PHP-FPM?

First: if you are using php-fpm there is a highly chance that you are not using any control panel at all, and that your server is just a plain Linux one.

Second: find your www.conf file and set this variable up to 30, 60 or 120, depending on your needs. On CentOS systems you can find it at /etc/php-fpm.d/www.conf

Edit the file and configure this variable:

request_terminate_timeout = 0

This variable sets the timeout for serving a single request after which the worker process will be killed. Set to 0 for unlimited time or up to 30, 60 or more as you need.

Edit your traditional php.ini file, usually located at /etc/php.ini and also raise max_execution_time as we explained before at the beginning of this article.

Finally let’s move to Nginx configuration, it’s recommended to raise a little bit the fastcgi_read_timeout variable, this variable is responsible for reading a response from the FastCGI server.

Edit your nginx configuration file

nano -w /etc/nginx/conf.d/nixcp.com.conf

Find your PHP-FPM configuration and set/add this variable:

fastcgi_read_timeout 150;

It should look like this:

# PHP-FPM configuration for nixcp.com
location ~ \.php$ {
root /var/www/nixcp.com;
try_files $uri =404;
fastcgi_pass unix:/tmp/nixcp.com_php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_buffer_size 128k;
fastcgi_read_timeout 150;
fastcgi_buffers 256 4k;
fastcgi_busy_buffers_size 256k;
fastcgi_temp_file_write_size 256k;
}

Conclusion

As we’ve seen on this article, Script timed out before returning headers is a very usual error while developing or troubleshooting website errors, it can affect any kind of programming languages, not only PHP.

However, on this time we just focused on PHP, which is the most widely used language nowadays. Remember this error is always related to timeouts, explore the timeouts of your web services and you’ll find a fix for sure.

About the Author: Martin Keler

1 Comment

Leave a Reply

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