I recently installed apache and
/var/www was owned by root and then I
had to chmod 777 to make stuff work.
This is insane.
Yeah, don't do that. Use:
chown -R www-data.www-data /var/www
Instead. Otherwise you are compromising the security of your webpages. The guides that suggest chmod things to 777 are generally writing the tutorial for shared hosting providers, because if they already have it set as 775, or 755 and chown as the www-data user there is no reason to ever chmod 777 anything unless you are sharing a file with another user on the system, but even then just cp it to /tmp
Sudo is meant to protect you from doing silly things as root that you have no business doing. If you need a temporary "root" shell, do sudo su -. Be be forwarned this should only be used a temporary. I'd suggest a TMOUT=600 in your root's .bashrc so that root is automatically kicked after 10min of idle.
I would suggest that you read Ubuntu Server Guide(direct link to pdf). Specifically chapter 8 on Security. Reading the whole book should give you great oversight on doing, maintaining and enhancing your services/quality/security.