If you have to run with things set to 777, it means you have some ownership problems.
Generally speaking there are only three groups of users you have to worry about:
- the actual users who are setting the web application up;
- the uid that the web server (and probably PHP) runs under; and
- everyone else.
Usually you should be able to set the ownerships of the files and directories to be the actual user doing the setup, and the group membership to be the group membership of the web server. If you do that, then you can set the permissions on most things to be rwx for the owner, and rx for the apache group; your application notes should be very specific as to what directories the application requires write-access. (If it isn't, it's probably written loosely in other ways too and may be a security vulnerability.)
If you do it correctly, then "everyone else" (which is "everyone as users on the server who are not members of the web server's group") can have their permissions set to 0 (ie no-read, no-write, no-ex).
For example, my WordPress installation is installed by me, but run by the webserver, so everything is generally permission 750, ownership dave:apache. The directory where wordpress stashes incoming uploads is set for permission 770, ownership dave:apache.