I have apache2 and vsftpd installed on Ubuntu server.
Let's say I want to upload test.html file from local computer to /var/www folder on the server
I am uploading the file as ftpuser using FTP client. But when I try to access http://localhost/test.html on the server I am getting "Access denied" error.
When I look to the permissions of test.html file, I see owner of the file is ftpuser and group is ftpuser. If I change the owner to www-data then no more error shown.
Changing owner user of a file:
sudo chown www-data /var/www/test.html
Changing owner and group of all sub files/directories:
sudo chown -R www-data:www-data /var/www/
But when I upload a new test2.html file, then I should change the owner again. This is tyring and boring operation.
So, I tried to find an auto method so no need to change ownership each time.
Here are the steps.
Step 1: Add the ftp user (In this context: ftpuser) to www-data group.
sudo adduser ftpuser www-data
Step 2: Make www-data is primarily group for ftpuser.
sudo usermod -g www-data ftpuser
Step 3: Now we should edit /etc/vsftpd.conf file.
sudo nano /etc/vsftpd.conf
Uncomment below line:
local_umask=022
By this, uploaded files can read-write by owner (ftpuser), and read by group (www-data), and can read by others.
If write/change permission needed for www-data group, you can set local_umask=022
Step 4. Restart vsftpd service:
service vsftpd restart
That's it! Hope it helps.
Alternative Method 1
After a few hours from the above content posted, I thought about another approach.
What about if I upload the files using www-data user as FTP user?
To do this, I set a password for www-data user by:
sudo passwd www-data
Then I uploaded some files as www-data using FTP client.
Nothing more needed! Thats all!
Thank God for giving us thinking brains! :)
Alternate Method 2: Anonymous Uploads Only
A few days later, I noticed /etc/vsftpd.conf file contains some options like:
chown_uploads=YES
chown_username=whoever
Comment for this options says:
If you want, you can arrange for uploaded anonymous files to be owned by # a different user.
It looks like this option applies only if the files uploaded by anonymous. Not for uploads by local user.
References: