Redmine - 500 error when upload files

For couple years, users could not upload files into our Redmine instance when creating or updating issues (or tickets). We ignored it because most of the tickets were created from the email pulling task (IMAP) and the attachments were created just fine. But when we developed an external tool to manipulate issues with attachment, I think it's time to seriously looking at the problem and fix it.

Here is the command we try (upload file using Redmine's Rest API):

# curl '' -X POST -H 'X-Redmine-API-Key: <myapikey>' -H 'Content-Type: application/octet-stream' --data-binary "@myfile.png"

and errors (/var/log/redmine/default/production.log):

Completed 500 Internal Server Error in 15.4ms
Errno::EACCES (Permission denied - /var/lib/redmine/default/files/2016/12):
  app/models/attachment.rb:108:in `files_to_final_location'
  app/controllers/attachments_controller.rb:88:in `upload'

Reading the error logs you will see there is a permission problem in the file storage. So I went to the folder and check its ownership:

# cd /var/lib/redmine/default/files/

There is some directories that were owned by root. I changed the owner to www-data and everything worked like a charm:

# chown -R www-data:www-data /var/lib/redmine/default/files/