ZipArchive::close(): Failure to create temporary file: Permission denied nginx
When deploying PHP applications that generate complex documents using libraries like PhpOffice\PhpWord, you often encounter a stark contrast between local development and production environments. On Apache, your script might execute flawlessly, creating Word 2007 documents without issue. However, switch to Nginx as the reverse proxy or serve static files, and you may suddenly face the error: ZipArchive::close(): Failure to create temporary file: Permission denied.
This error is not a bug in your PHP code logic. It is a filesystem permission problem rooted in how Nginx handles temporary storage compared to Apache.
The Root Cause: Temporary File Creation
The ZipArchive class, which underpins the .docx format (which is essentially a zipped collection of XML and resource files), requires write access to a specific directory on the server to function. When you call $objWriter->save(), the library attempts to create a temporary file to stage the zip archive before moving it to your final destination.
In many default Nginx configurations, particularly when using PHP-FPM or FastCGI, the web server user (often www-data on Debian/Ubuntu) does not have write permissions in the standard system temp directories like /tmp, /var/tmp, or /dev/shm. While Apache often runs as a user with broader permissions or handles temporary file creation differently via mod_php, Nginx's separation of concerns makes this stricter.
The error message explicitly points to ZipArchive::close(). This method is responsible for finalizing the archive. If the process cannot write the intermediate .tmp files required during this closure, the operation fails immediately, regardless of whether your PHP script has write permissions on the output directory itself (e.g., /var/www/html/output).
Diagnosing the Environment
Before altering permissions globally, verify which directory the ZipArchive library is attempting to use. You can check the current temporary directory setting by running:
php -r 'echo ini_get("sys_temp_dir") . PHP_EOL;'
If this returns a path like /var/tmp or /run, your server's www-data user likely lacks write access to that specific location. Conversely, if the library is configured to use the system default (often /tmp), ensure that directory exists and is writable by the web server user.
Resolving Permissions Safely
The most direct fix involves ensuring the web server user can write to the designated temporary directory. On a typical Linux distribution using Nginx, you would adjust permissions on the temp directory identified by ini_get("sys_temp_dir").
For example, if /tmp is the default and owned by root, you might grant group write access or switch ownership:
# Ensure www-data can write to /tmp
sudo chown root:www-data /tmp
sudo chmod 1775 /tmp
The sticky bit (1775) ensures that users cannot delete files created by other users, maintaining security while allowing the web server to create its own temporary files.
Alternatively, you can configure PHP to use a directory with known write permissions explicitly. This is often safer than modifying global system temp directories. You can set the open_basedir restriction or simply rely on the default behavior if your application structure allows it. However, note that changing sys_temp_dir in php.ini requires restarting the PHP-FPM service for changes to take effect.
Application-Level Workarounds
If modifying system permissions is not an option due to security policies, you can attempt to bypass the default temporary directory behavior by ensuring the output directory has sufficient write space and that no restrictive open_basedir directives are blocking the temp path.
In some cases, especially with complex libraries like PhpOffice\PhpWord, the library might try to use a specific subdirectory for internal caching. Check if your application writes to a custom cache directory within your project structure. If so, ensure that directory exists and is owned by www-data:
// Example: Ensuring the output path is writable before saving
$outputPath = 'doorverwijzing_patient_xxxxxxxxxx.docx';
if (!file_exists(dirname($outputPath))) {
mkdir(dirname($outputPath), 0755, true);
}
When to Use a Custom Temp Directory
If your environment uses a chroot jail or a restricted Docker container where /tmp is mounted read-only, you must explicitly configure PHP to use a writable path inside that container. This might involve updating php.ini:
sys_temp_dir = /var/www/html/storage/temp
Remember that any change to php.ini in an Nginx + PHP-FPM setup requires reloading the specific pool or service:
sudo systemctl reload php-fpm
Final Considerations
The discrepancy between Apache and Nginx usually highlights a difference in default user configurations rather than a flaw in your application logic. By ensuring the web server user has write access to the designated temporary directory, you resolve the ZipArchive::close() failure without altering your codebase. Always verify ownership and permissions (ls -ld /tmp) after making changes to confirm that www-data can create new files there.
For further guidance on optimizing PHP performance and security in Nginx environments, resources like https://laravelcompany.com offer insights into managing web server configurations effectively.