How to configure LAMP to allow visitor's file upload?

Posted April 24, 2015 20.9k views
UbuntuApachePHPLAMP StackLoggingLinux Commands


I have configured LAMP droplet on Ubunto 14.04.
PHP is running and everything works - except when I try to upload files.
My web site allows users to upload images that should end up being at /var/www/html/userphotos/<date>/.
PHP first upload them to a temp folder, then creates the <date> folder if needed, and then moves it to the destination folder. It all worked on another hosting so I’m sure the PHP script is fine.
I don’t have log files and I’m not a Linux expert, so I’m not even sure how turn them on.
My guess is that it’s a permission thing.
What I have done so far:

  1. chmod to 755 on the entire html folder
  2. manually created html/userphotos folder and chmod to 777 (temporarily, just to make sure it’s not that problem).
  3. defined uploadtmpdir in php.ini to /tmp and verified in info.php that configration applied. I also chmod on that folder to 777 (apparently it was 1777 before my change and now it is 0777 so maybe I ought to change it back)

Any help on how to set up php/apache to allow file upload and folder creation will be highly appreciated.


I have found the log files under /var/log/apache2, and here are the relevant lines:

[:error] [pid 21363] [client] PHP Warning: mkdir(): Permission denied in /var/www/html/uploadphoto.php on line 55

[:error] [pid 21363] [client] PHP Warning: copy(/var/www/html/usersphotos/2015-04-24/241403605.jpg): failed to open stream: No such file or directory in /var/www/html/uploadphoto.php on line 59

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
1 answer

Answering my own question, hopefully this could help someone:

Per this stackoverflow answer, I gave ownership to the apache user and group to the entire www folder, and then gave permission to that group on the folder:

chown -R www-data:www-data /var/www/
chmod -R g+rw /var/www/

  • Thanks for sharing your solution danbars. It worked perfectly for me!

  • Thanks for sharing. I just ran this. I could upload files but I couldnt save changes I made to my code afterwards. Just incase someone tries this and it doesnt work, this was what I did to fix it

    sudo chown -R ceejaiy:ceejaiy /var/www/
    sudo chmod -R 777 /var/www/

    It worked after that