Encountering a persistent “Upload failed” error, as shown in the attached image. Although uploads were previously successful, this issue began occurring after several days of normal application functionality.
Why it happens:
Systemd Sandboxing: Debian’s tomcat9 package uses systemd to manage the Tomcat service. Systemd employs sandboxing techniques to enhance security by restricting the resources and access that a service has. This includes limiting file system access.
Default Restrictions: By default, Tomcat is only granted write access to specific directories like conf, log, work, and webapps. This is a security measure to prevent Tomcat from potentially writing to sensitive areas of the file system.
Configuration Overrides: When you directly modify Tomcat’s main service file, your changes can be overwritten during package updates or service restarts. This is because the package maintainers provide the default configuration, and your direct edits are seen as deviations.
How to fix it:
The correct way to add or modify ReadWritePaths is to create an override file for the Tomcat service. This ensures that your changes are preserved across updates.
Create the override directory:
Based on your Tomcat version, update the version in the code, which is highlighted in red. In my case, it’s Tomcat9
sudo mkdir -p /etc/systemd/system/tomcat9.service.d/
Create the override file:
Create a file named override.conf inside the directory you just created: Based on your Tomcat version, update the version in the code, which is highlighted in red. In my case, it’s Tomcat9
sudo nano /etc/systemd/system/tomcat9.service.d/override.conf
Add your ReadWritePaths:
Inside override.conf, add the following, making sure to replace /dspace/ with the actual path you need:
[Service]
ReadWritePaths=/dspace/
(Optional)If you need to grant access to multiple paths, list them separated by spaces:
[Service]
ReadWritePaths=/dspace/ /another/path/ /yet/another/path/
Reassign the permission
sudo chown -R tomcat:tomcat /dspace/
Reload systemd and restart Tomcat:
Apply the changes and restart Tomcat:
sudo systemctl daemon-reload
sudo systemctl restart tomcat9
Please try uploading files again. It should work now.