Permission denied inside /var/www/html when creating a website and it's files with the apache2 server

65,679

Solution 1

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.

Solution 2

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.

Solution 3

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

Solution 4

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).

Solution 5

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

Share:
65,679
jose reyes
Author by

jose reyes

I enjoy working on problems; they shake our foundation of understanding and make us question our methods. We learn to think different. I think that is invaluable.

Updated on September 24, 2021

Comments

  • jose reyes
    jose reyes over 2 years

    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!

  • jose reyes
    jose reyes about 6 years
    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
    jose reyes about 6 years
    Surely someone has setup the LAMP stack on their machine before, given there are millions of sites running it, why is this a problem?
  • ansh sachdeva
    ansh sachdeva about 4 years
    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?
  • luisdev
    luisdev almost 3 years
    Thanks. This spared me lots of grey hairs! :-)
  • NMukama
    NMukama over 2 years
    this works thanks
  • Nisar Ahmed
    Nisar Ahmed over 2 years
    Works. This is the relevant answer.
  • chiddidilikahrn
    chiddidilikahrn over 2 years
    OMG!!! Opening execute to the WORLD is a HORRIBLE idea for a public-facing website!
  • chiddidilikahrn
    chiddidilikahrn over 2 years
    Works fine -until your webserver is hacked and taken hostage for Bitcoin!
  • Charles
    Charles about 2 years
    Works for me, 2022 on Ubuntu 20 on an instance running on AWS LightSail