How to fix Drupal 7 file and directory permissions

I had some problems recently with a Drupal 7 website, specifically related to file and directory permission problems, first after moving the site from one server to another, and then again after updating the site to the latest Drupal 7 release. To fix these problems I issued these commands from my Drupal installation directory:

$ sudo chown -R alvin:www-data .
$ sudo find . -type d -exec chmod u=rwx,g=rx,o= {} \;
$ sudo find . -type f -exec chmod u=rw,g=r,o= {} \;

That first command assumes the files are owned by the user alvin, and the group "www-data", which is a normal Apache group.

After that, I had to fix some file permission problems in the sites/default directory:

$ cd sites/default
$ sudo find files -type d -exec chmod u=rwx,g=rwx,o= {} \;
$ sudo find files -type f -exec chmod u=rw,g=rw,o= {} \;