Motivation
If you host a website you usually want to access it. To do this you need to open some ports. Here we’re using ufw for this purpose.
Opening ports
At first you need to login to your server via ssh for example with:
bash
ssh root@MY_IP
For this to work your ssh key has to be added to the authorized keys for the root user. As soon as you’re logged in you can call
bash
ufw status
Which might just tell you “Status: inactive”. This “ufw” thing is the firewall which we need to enable. 3 ports have to be able for us to use and maintain our application properly, namely 22 (ssh), 80 (http) and 443 (https). They all can differ depending on your preferences though. These are just the default ports. So we call the following to allow and enable them:
bash
ufw allow 22ufw allow 80ufw allow 443ufw enable
We can see what this give us:
bash
ufw statusStatus: activeTo Action From-- ------ ----22 ALLOW Anywhere80 ALLOW Anywhere443 ALLOW Anywhere22 (v6) ALLOW Anywhere (v6)80 (v6) ALLOW Anywhere (v6)443 (v6) ALLOW Anywhere (v6)
Wrapping Up
Now as mentioned we opened the ssh port. Thats the “door” we’ve gone through to get to our server. So before closing the existing connection, it’s highly recommended that you try to log in from another terminal. If it doesn’t work, then you have now an easier time to correct this. Otherwise thing might look grim. That’s all there is to it. Short and simple. But there is of course a lot more to it. You can allow ports only for specific IPs or a explicitly define a TCP or UDP allowance.