How to run a temporary http server without installing Nginx / Apache in Ubuntu

Sometimes we only want to open HTTP ports temporarily, here is how:

If you have enabled ufw, add ufw rule to allow the port being used by HTTP server. For example. to open port 3000 for HTTP server:

sudo ufw allow from any to any port 3000

Now check all ufw rules:

sudo ufw status numbered

Go to your website folder. Create html files, e.g. index.html if you don’t have them.

Run the following command to start your web server by typing:

python3 -m http.server 3000

Now you can access that page in your browser.

After running it, delete the rule (the number can be found from above command, e.g. sudo ufw status numbered):

sudo ufw delete THE_RULE_NUMBER