Increasing Apache + PHP Limits and Timeouts

Apache, PHP, and phpMyAdmin all have their own configuration settings that define and limit:

  • Memory Usage (Allocation)
  • PHP Script Run Times
  • Upload File Sizes

*These instructions will work for both Apache on Linux and for all WAMP-servers (Apache on Windows). But all the directory and file paths used here are specific to WampDeveloper Pro (just substitute in the paths of your environment).

Increasing File Upload Limits

For uploading large files there are Apache and PHP memory limits and timeout values set that you might need to update…

PHP

Open and edit file php.ini – C:\WampDeveloper\Config\Php\php.ini

These two settings define the maximum file upload size:

upload_max_filesize = 256M
post_max_size = 257M

This will increase upload limits to 256 MB, from the default of 2 MB.

post_max_size” is set 1 MB larger than “upload_max_filesize” to take into account for any additional meta-data about the file that is included in the request.

Related PHP Settings

1. file_uploads

file_uploads = On

“file_uploads” has to be on. And usually is by default under most WAMP and LAMP setups.

2. memory_limit

memory_limit = 258M

While the PHP docs state that “memory_limit” should generally be set higher than the “post_max_size” value, is not absolutely required to be so, because file uploads are not stored/output *in their entirety* to memory. Instead, they are buffered part-by-part and written out to a file in the temporary directory (C:\WampDeveloper\Temp\) as the upload progresses.

3. upload_tmp_dir

upload_tmp_dir = "C:/WampDeveloper/Temp"

This is the directory where file uploads are temporarily placed. It has to exist, and has to not have special/restrictive permissions set on it. Also there must be enough free disk space on this partition, and no overly restrictive space quotas set on this directory.

If you are using your own coded scripts to do file uploads, note that the file will be deleted from the temporary directory automatically by PHP at the end of the request. Before the script exists, you must move or copy away the file.

4. max_input_time and max_execution_time

max_input_time = 300

“max_input_time” is the amount of time a PHP script can spend receiving input (the time spent receiving the uploading file). The default is 60 seconds. This time (the input time) does not count towards the “max_execution_time” limit (…you don’t need to modify “max_execution_time” for file uploads).

5. max_file_uploads

max_file_uploads = 20

“max_file_uploads” sets the maximum number of files (default is 20) that can be uploaded by a single POST request (via form submit). If you are uploading more than 1 file at a time, you have to set “post_max_size” to a greater value than “upload_max_filesize” times “max_file_uploads”.

http://php.net/manual/en/features.file-upload.post-method.php
http://php.net/manual/en/features.file-upload.common-pitfalls.php

PHP-FCGI

When using PHP-FCGI (a separate PHP process ran via Apache’s mod_fcgid), mod_fcgid caps uploads to 128KB by default in newer versions (v2.3.6 and above), and usually returns a “500 Server Error” when that limit is reached.

Edit file C:\WampDeveloper\Config\Apache\extra\wampd-php5-fcgi.conf

Inside the <IfModule fcgid_module> block, add in or edit setting:
FcgidMaxRequestLen 268435456

This will set the max upload value to 256 MB. Or for a 1GB (1024MB) upload limit, use value: 1073741824.

You should probably set this to match whatever max upload value you have in php.ini – though it does not have to be so.

Also for uploading and downloading files, this timeout needs to reflect the time it takes to upload/download the file:
FcgidBusyTimeout 300

Apache

Apache sets (by default) “LimitRequestBody” to 0, specifying an unlimited size limit on the request.

*But this can also be set in any .htaccess file to override the default value.

Increasing Execution Timeout Limits

For processing requests there are Apache and PHP timeout values set that you might need to update (the default timeout is usually set to 300 seconds / 5 minutes)…

Apache Wait Time for Input/Output

Edit file C:\WampDeveloper\Config\Apache\extra\httpd-default.conf
Timeout = 300

http://httpd.apache.org/docs/2.2/mod/core.html#timeout

PHP Script Processing & Runtime Limits

Edit file C:\WampDeveloper\Config\Php\php.ini
max_execution_time = 300

http://php.net/manual/en/info.configuration.php#ini.max-execution-time

PHP FastCGI Processing Timeout

When running PHP-FCGI, the FastCGI configuration sets a value on the time a FastCGI application has to respond to the I/O request (default is 40 seconds)…

Edit file C:\WampDeveloper\Config\Apache\extra\wampd-php5-fcgi.conf
FcgidIOTimeout 300

Also for busy scripts, this timeout needs to reflect the time it takes those scripts to finish:
FcgidBusyTimeout 300

Increasing PHP Memory Limits

PHP has a limit set on the amount of memory a PHP script is allowed to allocate.
memory_limit = 258M

Generally you don’t want to set a value above 2000MB (slightly lower than 2GB / 2048MB) as it 1) begins to exhaust the memory space PHP can use running as a 32 bit process and 2) has been known to trigger PHP bugs. Though normally it shouldn’t need to be higher than 1024MB as anything above is usually indicative of bad memory management or memory leakage by the PHP script used.

.htaccess and PHP code that overrides php.ini settings

If you are experiencing an Apache or PHP error message:

Out of memory (allocated X) (tried to allocate Y bytes)

Try using Notepad++ (which has a nice Find-in-files search feature), or your own choice of an Editor, to search your website’s DocumentRoot (\webroot) folder:
C:\WampDeveloper\Websites\domain.name\webroot\

…For all files; And for all occurrences of this text/string:
memory_limit

…To see if “memory_limit” is being set in any .htaccess files (via directive: “php_value”) or PHP files (via function: “ini_set()”). Then manually edit or comment out those lines.

A) An .htaccess file could override php.ini’s memory_limit:
php_value memory_limit 20MB

B) A PHP script could override php.ini’s memory_limit:
ini_set("memory_limit","20M");

Also note that the website’s VirtualHost file can set a value that is not overridable via .htaccess files nor script code by using the “php_admin_value” directive:
php_admin_value memory_limit 258MB

Increasing phpMyAdmin Limits

phpMyAdmin has a default limit of 300 seconds set on the import and export of dump (SQL) files. This setting does not affect the run time of any other phpMyAdmin functions.

Edit file C:\WampDeveloper\Tools\phpMyAdmin\config.inc.php
$cfg['ExecTimeLimit'] = 300;

phpMyAdmin also has a $cfg['MemoryLimit'] setting that limits the amount of memory a script can allocate for importing and exporting SQL files (and on some other internal functions), but its default value is already set with no limit:
$cfg['MemoryLimit'] = '0';

*If you do set this value for some reason (instead of letting PHP handle this limit), to specify megabytes, append an “M” to the value.

4 thoughts on “Increasing Apache + PHP Limits and Timeouts

  1. admin Post author

    There is a lot of confusion on the net and the PHP docs about setting memory_limit in relation to upload_max_filesize and post_max_size.

    Here is the sourcecode to the PHP rfc1867_post_handler / function…
    http://git.php.net/?p=php-src.git;a=blob;f=main/rfc1867.c;hb=refs/heads/PHP-5.3

    I can’t really tell if it’s buffering parts, writing them out to disk, then recycling the buffer; or outputing the whole thing to memory (or has a bug in it that does so anyways).

    On another note here are the listed error messages -

     132 /* Errors */
     133 #define UPLOAD_ERROR_OK   0  /* File upload succesful */
     134 #define UPLOAD_ERROR_A    1  /* Uploaded file exceeded upload_max_filesize */
     135 #define UPLOAD_ERROR_B    2  /* Uploaded file exceeded MAX_FILE_SIZE */
     136 #define UPLOAD_ERROR_C    3  /* Partially uploaded */
     137 #define UPLOAD_ERROR_D    4  /* No file uploaded */
     138 #define UPLOAD_ERROR_E    6  /* Missing /tmp or similar directory */
     139 #define UPLOAD_ERROR_F    7  /* Failed to write file to disk */
     140 #define UPLOAD_ERROR_X    8  /* File upload stopped by extension */
    
    Reply
  2. admin Post author

    If you are finding yourself getting logged out (for example – out of phpmyadmin), you might be hitting a sessions limit…

    If php.ini is set to store sessions in filesystem (default behavior):
    session.save_handler = files

    PHP will clean up (remove) that session data if the modification time for the session file has not changed for 1440 seconds (24 minutes):
    session.gc_maxlifetime = 1440

    And you might get logged out.

    This value can be set in php.ini, htaccess files (php_value), and in code (ini_set()).

    Note -
    1. Do not use subdirectories for sessions in php.ini (session.save_path = “N;/path”) as this will break automatic garbage collection.

    2. The default behavior for session cookie lifetime is to not expire until the Browser is restarted…
    session.cookie_lifetime = 0

    Reply
  3. admin Post author

    Generally speaking, it’s not a good idea to attempt to upload files larger than 2GB via Apache and PHP, due to inherent limits and 32/64 bit integer bugs in the codebases.

    The stateless HTTP protocol was also never ment to handle these types of large file uploads.

    Use FTP, or some other mechanism, to upload files larger than 2GB.

    Reply
  4. admin Post author

    MySQL Server has gone Away

    php.ini defines the amount of time PHP will wait for a connection to MySQL to establish and/or the amount of time to wait between responses to/from the database before dropping the connection.


    mysql.connect_timeout 60
    default_socket_timeout 60

    If your PHP scripts do not close connections properly, or the server is very active (has many connections), setting these value higher will cause overload issues – with too many PHP threads waiting needlessly.

    The only time you should set these values higher is when you have a very long running PHP script doing work while never pinging back MySQL (a script that establishes a connection, does a bunch of work, and then uses that connection at the end).

    http://www.php.net/manual/en/mysql.configuration.php#ini.mysql.connect-timeout

    Reply

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>