27

screenshot from inside atom UPDATE** The screenshot is within atom, but when I navigate to the directory using the file explorer, and right click, the option to rename or create a new folder are restricted and I cannot click on them.

I just finished setting up the LAMP stack on my fresh UBUNTU 18.04 installation. I have everything working, the default /var/www/html/index.html page from Apache2 is being served on localhost, no port forwarding or any unique domain name, i just wanna run this on my network from my computer for now.

If there is a simple way to create multiple websites and easily choose which folder to serve than that's fine, but I want to serve just one website for now.

When I go to my /var/www/html folder and try to edit the index.html file it says permission denied. What do I need to do in order to work inside this directory for the remaining time that I am building the website. I am signed in as the root user on my system.

Also, if I do change permissions to allow me to work in this directory, what does it mean for people trying to access my server if it was available to the public. (RIGHT NOW JUST ON LOCALHOST).

Lemme know if you need more info or explanation thanks!

2
  • 1
    You're logged in as root and still get permission denied?
    – tkausl
    May 16, 2018 at 19:34
  • yup, that's correct. I also try to open it in my ATOM editor which is the one I will be working on my website in and it doesn't let me make edits.
    – jose reyes
    May 16, 2018 at 19:36

9 Answers 9

82
sudo chown -R $USER:$USER /var/www

this works, it changes the owner to my user instead of root user. I still don't understand because my user already had sudo rights and all those permissions. It was the user I created during the ubuntu18.04 setup, so there shouldn't be an issue, or idk.

5
  • this works thanks
    – NMukama
    Oct 13, 2021 at 5:21
  • 2
    Works for me, 2022 on Ubuntu 20 on an instance running on AWS LightSail
    – Charles
    Apr 28, 2022 at 12:01
  • Its also working for me shorter syntax sudo chown -R $USER /var/www/html Feb 21, 2023 at 3:49
  • That didn't works for me, Ubuntu 22 @ localhost May 18, 2023 at 9:22
  • I'm now getting the Configuration was not read or initialized correctly, not overwriting /var/www/html/config/config.php error; how can I revert?
    – Andrei
    Apr 10 at 14:58
15

File ownership issues can be fixed at the command line by typing:

sudo chmod 777 /var/www/html -R

One caveat from turnkeyLinux.com:

Changing file permissions is a trade off (often increasing security reduces user-friendliness and/or usability). For security 'best practice' only the folders that require write access by the webserver should be owned by the webserver. If your webserver has write access everywhere and your server is compromised it makes it easier to hack your WordPress install) but for ease of use giving the webserver ownership should resolve all your issues...


This article on Understanding File Permissions was great, too.

2
  • would this be a temporary or a permanent solution? i want my browser to run html files as long as am working with those. is there way for permissions to get reset automatically next time every fresh browser launch, or every reboot/screen unlock? Mar 11, 2020 at 10:25
  • 8
    OMG!!! Opening execute to the WORLD is a HORRIBLE idea for a public-facing website! Jan 20, 2022 at 16:00
6

This will help you.

sudo chgrp -R www-data /var/www/html
sudo gpasswd -a username www-data
sudo chmod -R 777 /var/www/html
3
  • Thanks. This spared me lots of grey hairs! :-)
    – luisdev
    May 26, 2021 at 18:35
  • Works. This is the relevant answer. Nov 26, 2021 at 12:37
  • 7
    Works fine -until your webserver is hacked and taken hostage for Bitcoin! Jan 20, 2022 at 16:39
3

The permission error is occurring because the folder does not have the rights and rights are reserved with different user. (you can inspect this by doing ls -l folderName)

The solution for your problem can be handled in different ways following are the few :

WAY1:

  1. Find out who is running apache by running the command apachectl -S
  2. Locate the user name (say www-data)
  3. Change the ownership of your folder as chown -R www-data:www-data /var/www/html (this will allow only your apache to play with files)
  4. Run the following command ln -s /var/www/html /home/username/html (this will create a soft link for your folder, where you can edit/delete/read which will reflect on your apache)

WAY2:

  1. goto /var/www/
  2. sudo chown -R www-data:${USER} html (Now both apache and your loged-in user will have rights to play with file).
1

If you are not root or you don't have a permission on some things(folder,files..), know that your actions are limited.

Take folder as example:

  • first of all verify permissions of your folder
    ==>ls -ld linkto/folder
    
  • and after give it a permissions it need or type the command bellow to add all permissions
    ==>sudo chmod -R 777 /var/www/html
    

verify permissions of your folder again if it is correct then try to copy again

0

Edit the file as root. Or better yet fix your permissions so you don’t have to worry.

2
  • How do I change my permission the correct way? I know it's gotta do something with chmod, but I can't find the correct protocol for doing this.
    – jose reyes
    May 16, 2018 at 19:38
  • Surely someone has setup the LAMP stack on their machine before, given there are millions of sites running it, why is this a problem?
    – jose reyes
    May 16, 2018 at 19:38
0

Just write

Blockquote

And give read and write rights to the folder ( not user )

0

Try the following the command

For Read and Write:

sudo chmod -R a+rw /var/www

For Read, Write and Execute:

sudo chmod -R a+rwx /var/www
0

All solutions I find here either make the current user or www-data both owners and groups of the html directory, but it's much, much better to make the user the group and www-data the owner:

sudo chown -R $USER:www-data /var/www

If not, applications which you don't completely control, like WordPress, won't be able to get sufficient permissionsto work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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