This one has also been bugging me for a while but I was too busy to hunt down a solution. Your question got me motivated to fix it. I found the answer on Stack Overflow.
In short, the solution is to change the umask
of the Apache process. The link above mentions two possible places to make the change: you add umask 002
to
/etc/init.d/apache2
/etc/apache2/envvars
(Debian/Ubuntu) or /etc/sysconfig/httpd
(CentOS/Red Hat), or
Edit
I recently upgraded from Ubuntu 12.04 32-bit to 14.04 64-bit and, to my great irritation, I could not get this to work. It worked for some PHP scripts but not others - specifically, a short test script I wrote worked fine, but the Laravel caching code did not. A co-worker put me on to another solution: bindfs.
By mounting my project directory (/var/www/project
) in my home directory (~/project
) with the appropriate user mapping, all my problems were solved. Here's my fstab
entry:
/var/www/project /home/username/project fuse.bindfs map=www-data/username:@www-data/@usergroup
Now I work in ~/project
- everything looks like it's owned by username:usergroup
and all filesystem changes work as if I own the files. But if I ls -la /var/www/project/
, everything is actually owned by www-data:www-data
.
Perhaps this is an overly-complicated solution, but if you have trouble getting the umask
solution to work, this is another approach.