0

I'm hosting a LAMP EC2 instance via Amazon AWS.

Part of my website allows users to upload files. Unfortunately, the server is not able to store permanent copies in the "uploads" folder because it is lacking necessary permissions.

A PHP script is called that will store a file to the "uploads" folder. The upload will fail while the upload folder has standard 755 and 775 permissions. However, when i change the folder permissions to 777 (world permissions), it works.

For obvious reasons, I don't want to use 777 world permissions. How can i make it so that the server has permission to write files to the "uploads" folder?

Thanks guys.

user3677286
  • 125
  • 2
  • 9

1 Answers1

0

This might be an issue with the Ownership of the upload folder.
The ownership of the folder can be checked by the following command

     ls -l 
     Sample Output:
    -rw-r--r-- 1 root root 0 Aug 31 05:48 demo.txt
. Here it can be seen that the both the USer of the File is root and the Group of the File is root.
Executing this command inside a directory will show the permissions and ownership of all the files and folders in that folder.
Now with LAMP stack you need to make sure the ownership will be with the
Apache User i.e. www-data and Apache Group again www-data. This can be done by going in the root folder of your application and and executing the Command
 chown -R www-data:www-data
     Sample Output:
    -rw-r--r-- 1 www-data www-data 0 Aug 31 05:48 demo.txt

This will recursively change the ownership of all the files and folders inside the Root Directory to the Apache User and Group.
The Common cause of this issue is when you have downloaded the package or files , you have obviously done it as a local or root user and Apache is not having permissions to do it. Or you have created a directory manually.
This is the basic idea to solve the issue, you also just might want to consider to execute this command and change the ownership of just your "uploads" directory .

Maharshi Raval
  • 168
  • 2
  • 10