cd /var/www/
sudo chown -R www-data: html
This is necessary for folders to keep www-data as their group after they are regenerated from command line.
Without this step, the owner would become the sudo user triggering the command (like cache:clean or setup:static-content:deploy), and www-data couldn't write into these folders anymore.
This could probably be avoided seting umask, but that is a server-wide change, while this way it only affects current vhost.
cd /var/www/html
sudo chmod -R g+s pub/static pub/media var/cache var/generation
More details about this can be found here