Ugacomp

How to configure Apache to handle large file uploads?

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

When it comes to handling large file uploads, Apache default configurations might not be sufficient. Uploading large files can strain server resources and impact performance if not properly configured. In this comprehensive guide, we will walk you through the necessary steps to configure Apache to handle large file uploads effectively and securely.

Put Server limitations into consideration

Before diving into configuration settings, it’s crucial to understand your server’s limitations. Factors like available memory, processing power, and network bandwidth significantly influence how Apache handles large file uploads. Make sure your server hardware and network infrastructure can support the expected upload traffic.

Also, you need to ensure you are using the latest version of Apache. Developers frequently release updates with performance improvements and security enhancements.

Configure PHP Settings

If your application relies on PHP for handling file uploads, adjust PHP settings in the php.ini configuration file. Locate and modify the following parameters:

Locate the php.ini file

The location of the php.ini file can vary depending on your operating system and how PHP is installed. Here are common locations for the php.ini file on different systems:

  • Windows

If you are using a web server like Apache, the php.ini file is often located in the PHP installation directory. For example, it might be in:

C:\PHP\php.ini 

or

C:\Program Files\PHP\php.ini.

If you are using PHP in conjunction with a web server like IIS, the php.ini file may be located in the Windows directory, such as

 C:\Windows\php.ini 

or

C:\WinNT\php.ini
  • Unix/Linux

The php.ini file is commonly found in the /etc directory. The full path might be:

/etc/php/7.x/php.ini

Where 7.x represents your PHP version.

Alternatively, on some systems, you might find it directly in the PHP installation directory, such as:

 /usr/local/php/php.ini 

or

/etc/php.ini
  • macOS

On macOS, the location is similar to Unix/Linux systems. You may find the php.ini file in the /etc directory or within the PHP installation directory.

To find the exact location of your php.ini file, you can create a simple PHP script with the phpinfo() function, which will output a lot of information about your PHP configuration, including the path to the php.ini file. Create a file named info.php with the following content:

<?php
phpinfo();
?>

Access this file through your web browser (e.g., http://localhost/info.php) and look for the “Loaded Configuration File” section. It will show the full path to the php.ini file being used by your PHP installation. Once you find it, you can edit the php.ini file to modify PHP configuration settings.

Customizing the php.ini directives

The php.ini file is the configuration file for PHP, and it contains various directives that control the behavior of the PHP runtime. Here are some commonly used PHP directives:

upload_max_filesize = 100M
post_max_size = 110M
memory_limit = 128M
max_execution_time = 300

upload_max_filesize

upload_max_filesize is a directive in PHP configuration settings that determines the maximum size of an uploaded file. It specifies the maximum allowed size, in bytes, for files that users can upload through PHP scripts.

When a user submits a file through a form on a website, PHP checks the size of the uploaded file against the value set for upload_max_filesize. If the file size exceeds this limit, PHP will reject the upload and generate an error.

Set this to the maximum size of files allowed to be uploaded. For example, to allow uploads up to 100 MB, set:

upload_max_filesize = 100M

The values for upload_max_filesize are specified in bytes by default, but you can use shorthand notations like K for kilobytes, M for megabytes, and G for gigabytes.

As for the minimum and maximum values, it depends on your PHP configuration and the platform. However, there are practical limits imposed by the underlying system and server settings.

The upload_max_filesize directive in PHP allows you to set the maximum size, in bytes, for uploaded files. The values for upload_max_filesize are specified in bytes by default, but you can use shorthand notations like K for kilobytes, M for megabytes, and G for gigabytes.

As for the minimum and maximum values, it depends on your PHP configuration and the platform. However, there are practical limits imposed by the underlying system and server settings. Here are some general guidelines:

  • The practical minimum value is 0, effectively disabling file uploads. However, it is common to set a minimum value of 1 byte to allow at least minimal file uploads.
upload_max_filesize = 1
  • The maximum value you can set depends on various factors, including your server configuration and the platform. However, keep in mind that setting an excessively high value may not be practical or may lead to issues with memory and resource consumption. A common approach is to set a value in the order of gigabytes:
upload_max_filesize = 2G

post_max_size

The post_max_size directive in PHP configuration determines the maximum size of POST data that PHP will accept. This includes not only file uploads but also other data submitted through HTML forms using the HTTP POST method. If the size of the POST data exceeds the value set for post_max_size, PHP will reject the request and generate an error.

The post_max_size value should be set to accommodate the total size of the POST request, including all form data and uploaded files. It’s crucial to set it to a value equal to or larger than the upload_max_filesize if your application involves file uploads.

Here is an example of how to set post_max_size in the php.ini file:

post_max_size = 8M

In this example, the post_max_size is set to 8 megabytes. As with upload_max_filesize, you can use shorthand notations like K for kilobytes, M for megabytes, and G for gigabytes.

It’s important to note that if the post_max_size is smaller than the upload_max_filesize, it may lead to issues when handling file uploads, as the overall request size could exceed the allowed limit.

max_execution_time

The max_execution_time directive in PHP configuration determines the maximum amount of time, in seconds, that a script is allowed to run. If a PHP script takes longer than the specified max_execution_time to execute, it will be terminated, and an error will be generated.

This directive is particularly useful to prevent scripts from running indefinitely, which could potentially lead to performance issues on the server or even security concerns in some cases.

Here is an example of how to set max_execution_time in the php.ini file:

max_execution_time = 30

In this example, the max_execution_time is set to 30 seconds. You can adjust the value according to your application’s needs. If you want to allow scripts to run without a time limit, you can set max_execution_time to 0:

max_execution_time = 0

Keep in mind that setting max_execution_time to 0 may not be suitable in shared hosting environments or in situations where you want to enforce reasonable time limits for script execution to prevent abuse or resource hogging. It’s essential to find a balance between allowing scripts enough time to complete their tasks and preventing excessively long-running scripts from causing issues.

You can increase the maximum execution time to prevent script timeouts during the upload process. Set it to a value suitable for your server environment, such as:

max_execution_time = 300

max_input_time

The max_input_time directive in PHP configuration specifies the maximum time, in seconds, that PHP scripts are allowed to spend parsing incoming requests, including handling GET, POST, and other input data. This directive is related to the time PHP allows for processing input data before the script execution begins.

If the time spent processing input data exceeds the value set for max_input_time, the script execution will proceed, and a warning will be issued. This is distinct from max_execution_time, which limits the total execution time of the script, including both input processing and script execution.

Here is an example of how to set max_input_time in the php.ini file:

max_input_time = 60

In this example, the max_input_time is set to 60 seconds, indicating that PHP scripts are allowed to spend up to 60 seconds processing incoming data.

It’s essential to configure max_input_time appropriately based on the requirements of your application. For instance, if your scripts handle large amounts of input data or require more time for input processing, you may need to adjust this value accordingly. As with other time-related directives, finding a balance between allowing sufficient time for processing and preventing potential abuse or resource issues is crucial.

Optimize Apache Configuration

Next, optimize your Apache configuration to handle large file uploads efficiently:

Increase LimitRequestBody

Apache’s LimitRequestBody directive limits the size of the HTTP request body. You need to increase it to match or exceed the upload_max_filesize specified in your PHP configuration. Add or modify this directive in your Apache configuration file (e.g., httpd.conf or an .htaccess file):

  LimitRequestBody 104857600  # 100 MB in bytes

Adjust Timeout and KeepAliveTimeout

Increase the timeout values to ensure that large uploads have sufficient time to complete without being prematurely terminated. Add or modify these directives in your Apache configuration file:

  Timeout 300
  KeepAliveTimeout 15

Enable mod_reqtimeout

This module helps mitigate slow client attacks and ensures that connections are not kept open indefinitely. Add the following line to your Apache configuration file to enable mod_reqtimeout:

  RequestReadTimeout header=20-40,MinRate=500 body=20,MinRate=500

Configure KeepAlive

KeepAlive allows multiple requests to be sent over the same TCP connection, reducing latency. Enable KeepAlive and adjust the MaxKeepAliveRequests and KeepAliveTimeout values:

  KeepAlive On
  MaxKeepAliveRequests 100
  KeepAliveTimeout 5

Use MPM Worker or Event

If your server experiences a high volume of concurrent connections, consider using the MPM (Multi-Processing Module) Worker or Event. These MPMs are more efficient at handling numerous simultaneous connections compared to the Prefork MPM.

Content Delivery Networks (CDNs)

Consider using Content Delivery Networks (CDNs) to offload traffic from your server. CDNs cache and deliver files from servers strategically located around the world, ensuring faster uploads for users regardless of their geographic location.

Implement Chunked Uploads

For exceptionally large files, consider implementing chunked uploads. Chunked uploads allow files to be split into smaller pieces, which are uploaded individually and reassembled on the server side. Several JavaScript libraries like Dropzone.js and Resumable.js facilitate chunked uploads, enhancing user experience and reducing the risk of failed uploads due to network interruptions.

RECOMMENDED READING: How to Implement Chunked Uploads in Apache

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.