Can’t Save Files in /var/www with LAMP Installed via Tasksel

fileslampserversudo

I just installed lamp with tasksel and it works. But

  1. inside /var/www I can create files only with sudo from terminal, I
    don't know why. In another Ubuntu installation I didn't have such problem.
  2. I created a file named index.php and I can't even save it.

That's the error I get from Sublime Text:

Unable to save /var/www/index.php
Error: unable to create tmp file in /var/www

And that's from Geany:

Error opening file '/var/www/index.php': Permission denied
The file on disk may now be truncated!

Best Answer

If you are going to be the user maintaining the site, then make it so that your userid can edit the files.

cd /var/www && sudo chown -R `whoami`:`whoami` .

If you do this, then you will be able to modify the files under /var/www because the files will belong to you and your personal group. The -R runs the command recursively - it runs on the current file (the directory) and all the files in and under the directory (included the hidden files)

If you have not run a sudo command recently (a few minutes), it will ask you for your personal password.

It is also possible to set up groups and all that. However, this is the simplest way.

Make sure that the files are readable by the web server. If this is a problem, then - because you own the files now you can use chmod command:

cd /var/www && chmod -R o+r .

If this does not make the directories executable, you may want to also do:

find /var/www -type d -print0 | xargs -0 chmod o+x

This will (find) collect the list of directory names and pass them on (as a null delimited list) to xargs, which will execute the chmod to make the directories executable.

As reported at related: Whats the simplest way to edit and add files to "/var/www"?, – Desmond Hume Nov 12 '12 at 14:42 suggested to @DonFaulkner

For security reasons, it's probably better keep /var/www owned by root:root, so instead of sudo chgrp -R www-data /var/www it better be sudo chgrp -R www-data /var/www/*

Related Question