the first time you want to access (either port 22 or port 80) it always fails. After pinging the public IP of the VM, then the server can be accessed (both port 22 and port 80)
This is /etc/network/interfaces on VM
root@ubuntu:~# cat /etc/network/interfaces
# This file describes the network...