Sacha
Sacha

Reputation: 569

Apache can't write to path

Overview

I'm using Laravel 4.2 with an image upload feature. I have it set up on Homestead, but recently I removed Nginx and serve the site with Apache instead (due to the need to use Server Sent Events).

Before I moved from Nginx to Apache the file upload functionality worked fine. It also works fine on a staging server with Apache.

The Error

I'm using the Intervention image library to handle image uploads. When I try to upload a file, I get the following in my laravel.log file:

Can't write image data to path (/home/vagrant/projects/projectname/public/assets/pics/profile/photos/f55f0ae2-2d1a-4fdd-b9be-39d8a509baa3.jpg)

What I've Tried

I thought it was just a simple permissions error. I've gone so far as to chmod my asset directories to 0777, change the directory owner to www-data, create a new group called web with www-data and change the owner group to that, (basically everything in this answer and more) and still it doesn't work.

The directory definitely exists, and like I mentioned, it worked before the move to Apache (I'm not entirely sure if it's related but it seems likely).

My /var/log/apache2/error.log is empty, too.

The staging server works fine, but I don't want to encounter the same problem when I provision the production server. I'd like to fix the problem and understand it more so I can fix it if I ever come across it in the future. Why is this happening and what can I do to fix it/debug further?

Example Folder Permissions

drwxr-xr-x 1 vagrant vagrant  272 Jan 26 11:07 assets/pics
drwxr-xr-x 1 vagrant vagrant  170 Jan 26 10:11 assets/pics/defaults
drwxr-xr-x 1 vagrant vagrant  714 Jan 26 14:10 assets/pics/forums
drwxr-xr-x 1 vagrant vagrant  646 Jan 26 14:10 assets/pics/forums/thumbs
drwxr-xr-x 1 vagrant vagrant  170 Jan 20 18:02 assets/pics/gallery
drwxr-xr-x 1 vagrant vagrant 3026 Jan 26 13:24 assets/pics/messages
drwxr-xr-x 1 vagrant vagrant 2992 Jan 26 13:24 assets/pics/messages/thumbs
drwxr-xr-x 1 vagrant vagrant  136 Jan  8 17:27 assets/pics/profile
drwxr-xr-x 1 vagrant vagrant  136 Feb 10 14:48 assets/pics/profile/photos
drwxr-xr-x 1 vagrant vagrant  102 Feb 10 14:51 assets/pics/profile/photos/thumbs

For some reason the owner/group is still vagrant, even when I chown them. I've tried with sudo chown and by doing sudo -s.

Upvotes: 0

Views: 871

Answers (2)

Bogdan
Bogdan

Reputation: 44586

Try modifying your Vagrantfile to include this line, before the end of the Vagrant.configure(VAGRANTFILE_API_VERSION) do |config| block:

config.vm.synced_folder "/path/to/laravel/app", "/home/vagrant/projects/projectname", :owner => "www-data", :group => "www-data", :mount_options => ["dmode=775", "fmode=664"]

The string "/path/to/laravel/app" must point to dir you're working on outside the Vagrant machine. This should force the file owner and permissions to be the ones you specify for all the project files (so they're not overridden by vagrant). You'll need to restart the VM after you make the change.

Upvotes: 1

Peter B
Peter B

Reputation: 477

How are you doing your chown? It should be

chown USERNAME GROUP -R /path/to/file

Upvotes: 0

Related Questions