$ sudo ufw default deny incoming
  $ sudo ufw default allow outgoing
  $ sudo ufw allow ssh
  $ sudo ufw allow 80/tcp
  $ sudo ufw allow 443/tcp
  $ sudo ufw enable
As a user of iptables this order makes me anxious. I used to cut myself out from the server many times because first blocking then adding exceptions. I can see that this is different here as the last command commits the rules...

I had this one too: I first denied all incoming requests and was about to allow SSH, but my SSH connection dropped :) Fortunately, I was able to restore the VM with the provider's VM console.

I have a funny story, when I did it in the startup script, then I ran it. I lost my ssh, and moreover this server was in another country, France. And moreover I set up the internal keyboard layout there to be US as I am writing without looking at the keyboard. In the result, the Polish-French guy there, who was at site, was unable to enter the password correctly unless I translated it to the french keyboard for him.