Editing files in /var/www

5,636

Solution 1

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        

Solution 2

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.

Share:
5,636

Related videos on Youtube

Adriano Resende
Author by

Adriano Resende

Updated on September 18, 2022

Comments

  • Adriano Resende
    Adriano Resende over 1 year

    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?

  • laurent
    laurent over 12 years
    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
    laurent over 12 years
    +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