I know that the owner of the files in /var/www must be www-data to properly run by apache. But when i run the chown -R www-data:www-data /var/www command i can't the php files in my editor.

How can i edit the files without messing the permissions?


Take care of ownership:

chown -hR www-data:www-data /var/www

Ensure the group can read and write the files. This does not enable it to alter any permissions though:

chmod  -R g+rw /var/www

The x bit. Ensure the group is permitted to list the directories, but not to execute the files:

find /var/www -type d -ls -exec chmod g+x {} ';'

Add yourself to the group www-data so you can use the permissions of this group:

usermod -aG www-data your-user        
  • 6
    +1 and you can also set x for directories only with chmod -R g+rwX /var/www (capital X) on the second step and discard 3rd command
    – laurent
    Jan 14 '12 at 20:45

The owner of the files does not have be www-data for apache to work well. The requirement is that the apache server which runs as www-data must be able to read the files. Most systems default to allowing group read access, so having the group on the files be www-data is sufficient.

The apache server needs to be able to write dynamic content such as wikis, content upload directories, and caches. Otherwise it is more secure to prevent the apache server from writing its content directories and files.

Consider changing the ownership to the user who needs to modify the content. Add them to the group www-data. You could also set the group sticky bit on the /var/www directory. If you are the user you can execute these commands.

sudo chmod g+s /var/www
sudo usermod -aG www-data $LOGNAME
sudo chown -R $LOGNAME /var/www

Logout and login again to gain access to the www-data group. You will then be able to modify the content.

chown -R www-data:www-data /var/www
chmod  766 /var/www  -R 

This will give write permission to your user.

  • 1
    This will give rwx to user (www-data), rw to group (www-data) and `r to others. This won't work to write the file with another user than www-data
    – laurent
    Jan 14 '12 at 20:42
  • My bad , i have updated the answer.
    – daya
    Jan 14 '12 at 20:47
  • Now ok for the access but this will make all the files readable and writable by any user which is not good for security - downvote removed as it works but this is not secure
    – laurent
    Jan 14 '12 at 20:49
  • then make chmod 760 and add yourself to www-data group as a member
    – jet
    Jan 15 '12 at 2:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.