Every once in a while I need to serve a web site locally on my MacBook. This is one of those tasks that happens just infrequently enough that I don’t always remember the quick one line command right off the bat. If you’re a developer you probably have situations like this too.
For me looking up what process is using a specific network port falls into this category too – but we’ll save that for another post.
So anyway, this time I’m writing down exactly how to serve a web site from the command line with a one liner and sharing so that in case you have to do this too, now you know how. These instructions should work on any modern MacOS, Linux or Windows machine when used from the command line.
Serve a Static Website with 1 Line of Python
If you have Python 3+ installed, and you probably do, you can use this one liner:
python3 -m http.server --cgi 8888
where 8888 is the port to use. Once you run this command, open http://localhost:8888 in your browser and you should be good to go. Need to use a different port? Change the 8888 for the port number of your choice.
When you’re done, just hit CTRL-C and the process will end.
nom i -g serve
then run the serve command in the folder where you’re web site files are located:
that’s it! If you want to specify the port use this syntax:
serve -p 8888
as before the port is 8888 in this example, swap as you see fit. Once you run this command, open http://localhost:8888 in a browser and you can surf your local site. Once done, again CTRL-C will end the web server.
I hope this is helpful!