Set up mod_xsendfile module apache to serve files faster in Apache, reduce PHP load, and improve download control with real-world, usable steps. Our Live Support Team is always here to help you.
How to Set Up and Use mod_xsendfile Module in Apache
If you’re running Apache and want to deliver files faster while reducing PHP overhead, the mod_xsendfile module apache is worth your attention. It’s a lightweight solution that offloads file delivery from your app to Apache’s core, making it ideal for CMSs and custom platforms. This post gives you exactly what you need, real steps, no fluff, and fully working code.
An Overview
What Is mod_xsendfile Module and Why Use It?
The mod_xsendfile module apache works by intercepting a special response header, X-SENDFILE, sent by your application. Instead of your app reading and pushing a file to the browser, Apache does it internally using faster system calls like sendfile or mmap.
It’s especially helpful when:
- You’re serving large static files (like PDFs or ZIPs) from PHP or Python.
- You want to verify user access in your code but let Apache handle the heavy lifting.
- You need to record download stats or implement access control logic.
- You want correct caching headers like ETag and If-Modified-Since.
Step 1: Install mod_xsendfile Module
This module doesn’t ship with Apache by default, so you’ll need to install it separately. On Ubuntu or Debian-based systems, you can run:
sudo apt-get install libapache2-mod-xsendfile
Step 2: Enable mod_xsendfile
Once installed, enable it using the Apache a2enmod command:
sudo a2enmod xsendfile
Step 3: Configure Apache
Now, configure Apache to allow mod_xsendfile to handle file delivery. Add the following lines to your Apache configuration file or inside the appropriate virtual host block (see common pitfalls):
XSendFile on
XSendFilePath /path/to/files
Here, XSendFile on enables the module, and XSendFilePath defines which directory Apache is allowed to serve files from. Be sure to replace /path/to/files with the actual directory where your protected files are stored.
Step 4: Use mod_xsendfile in Your App
Your web app needs to generate the correct header to trigger mod_xsendfile. In PHP, you can use the following snippet:
$file = '/path/to/file.pdf';
header('X-Sendfile: ' . $file);
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
This sends the file path to Apache. Just make sure your script checks if the user is allowed to download it before sending the headers.
Step 5: Restart Apache
After everything is set, restart Apache so that the changes take effect:
sudo systemctl restart apache2
[If needed, Our team is available 24/7 for additional assistance.]
Conclusion
Using the mod_xsendfile module apache is one of the cleanest ways to improve file delivery performance. It keeps your PHP code light, speeds up downloads, and ensures proper handling of cache and range headers, like enabling Brotli compression for better performance. Plus, the setup is quick, and the benefits are immediate.
0 Comments