Wordpress documentation has the official recommendations here.
I wrote a script that I can run on demand to upgrade wordpress, plugins, and themes, and also sets permissions on my various WordPress installs. I'm not an expert and I make no claims on whether it's fit for purpose, but I can say it seems to mostly work for me, other than the side effect below.
The side effect of this script are you can't install WordPress plugins or themes using the web interface of WordPress, you have to use the WordPress cli to install plugins. That means the permissions the script sets aren't quite right, they're too secure, but I don't install plugins often so it's close enough and I haven't bothered spending the time to fix it. Someone else can probably figure it out. I did have a quick look and I put an idea in commented out that might work.
You need to have the wordpress cli installed and working for this script to work. You'll also have to create the folder /var/log/wordpress/ and make sure the user who runs the script has permissions. I run it as root.
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
echo
echo Wordpress Update and Permissions Script Starting
echo "$(date) Wordpress update and backup started" >> /var/log/wordpress/upgrades 2>&1
# Function to upgrade wordpress
function upgrade_wordpress() {
# set up folders in the formats needed
dir=$1
uploads=$1/wp-content/uploads
plugins=$1/wp-content/plugins
themes=$1/wp-content/themes
echo Upgrading Wordpress core, plugins, themes in ${dir}
sudo -H -u www-user bash -c "wp core update --path=$dir"
sudo -H -u www-user bash -c "wp plugin update --all --path=$dir"
sudo -H -u www-user bash -c "wp theme update --all --path=$dir"
echo Setting wordpress permissions to 755 files and 644 folders
find ${dir} -type d -exec chmod 755 {} \;
find ${dir} -type f -exec chmod 644 {} \;
chmod 440 ${dir}/wp-config.php
echo Making uploads folder ${uploads} writable by the web server
chown -R www-data:www-data ${uploads}
# This might make the WordPress web interface able to install plugins and themes. It might also break everything or make it insecure. Beware.
# This part is is completely untested
# chown -R www-data:www-data ${plugins}
# chown -R www-data:www-data ${themes}
}
echo Setting /var/www permissions to www-user:www-data
chown -R www-user:www-data /var/www/
# Run Wordpress update for each wordpress install
upgrade_wordpress /var/www/wordpress1
upgrade_wordpress /var/www/wordpress2
upgrade_wordpress /var/www/wordpress3
echo Wordpress Update and Permissions Script finished
echo "$(date) Wordpress update and backup finished" >> /var/log/wordpress/upgrades 2>&1