I have been successfully using the method described here: Maintained by a Single User and it has been working well for me. This is my script for all my websites:
sudo chown -R WebAdmin /var/www/example.com/
sudo chgrp -R www-data /var/www/example.com/
sudo chmod -R 750 /var/www/example.com/
sudo chmod g+s /var/www/example.com/
sudo chown www-data:www-data /var/www/example.com/Uploads/
sudo chmod -R 755 /var/www/example.com/Uploads/
I maintain all the sites on that server and WebAdmin is the general user I use for FTP and also SSH.
However, I recently moved a few Wordpress sites from a hosting company to my server and the above doesn't work. Updating Wordpress or its themes/plugins gave me the error:
To perform the requested action, WordPress needs to access your web
server. Please enter your FTP credentials to proceed. If you do not
remember your credentials, you should contact your web host.
There are several "fixes" such as this one which are more like workarounds and are probably insecure. So I used the safest (AFAIK) method and made the www-data the owner of the WordPress sites, while keeping non-WordPress sites as before. In other words, I simply ran
sudo chown -R www-data /var/www/wp_example.com/
on the WordPress sites. This of course worked, but it becomes a pain as far as maintaining the website manually goes, because I have to login as root to do that. I would also not be able to assign the website(s) to someone else to maintain in the future. Note that some of my sites are hybrid (partly WordPress and part custom pages).
So I am wondering if there is a better solution that is more elegant and more general i.e. it will work for both WordPress and non-WordPress sites.
One possibility is that I add the user WebAdmin to the www-data group. Or perhaps www-data to the WebAdmin user group? Which makes more sense and which is more secure?