Photo by David Bruno Silva on Unsplash
Fixing Apache Document Root Directory Permissions on Ubuntu 20.04
I recently set up a LAMP (Linux, Apache, MySQL, PHP) virtual private server in the cloud. However, I was unable to edit the files in the document root directory (i.e., /var/www/html
) as a regular user. I definitely did not want to be logged in as the root user all the time, as that is really risky and not recommended. So I had to edit the file and directory permissions.
I first changed the group of the /var/www/html
directory to www-data
. This was done with the following command:
sudo chgrp -R www-data /var/www/html
www-data
is the default group used in Linux by web servers like Apache. Assigning it as the owner group ensures Apache can serve the files in the directory.
The html
directory itself contained only regular files at that point, i.e., no subdirectories. So I cd
-ed into the /var/www/html
directory and ran the command below to give the owning group read access to the files:
sudo chmod g+r *
Next, I ran the following commands, still being in the /var/www/html
directory:
sudo chown -R maija /var/www/html
sudo chmod u+rw *
The first command made me (the user maija
) the owning user of the /var/www/html
directory. The second command gave me read/write access to the files.
The last thing I wanted to do was to automatically give the www-data
group ownership of all new files and directories. This was accomplished with the following command:
sudo chmod g+s /var/www/html
I then tested that the last command worked by creating both a new file and a directory. The output of ls -l
in the html
directory was the following, dir
and src.js
being the added dummy directory and file, respectively:
Now I can copy and create files in the document root directory as a regular, non-root user, and Apache is able to serve the files and directories there.
Sources:
AskUbuntu: Permissions problems with /var/www/html
Linux man pages: chgrp
Tekopolis: Add user to Apache group www-data in Ubuntu