Quick Note: Setting up a localhost on a Mac
Many developers don’t even worry about setting up a localhost on their computers as automated task runners often will create one for you.
But at times I’ve found myself needing to run a localhost with no actual need to get a Gulp, Grunt or webpack setup going.
Sure, those times are far and few between these days, but the following commands can help you (or me for my own future reference) get a PHP or Python server up and locally running on your machine.
Setting up a local server
To successfully setup either a PHP or Python local server, you’ll need to use the command line to cd
into the directory where you want to start up the server.
PHP
Once in the desired directory, run:
php -S localhost:2222
Now you can go to “localhost:2222” in your browser, and if you have an index.html or .php file in your root directory, you’re in business.
Python
As with before, once in the desired directory, run:
python -m SimpleHTTPServer
Or specify a specific port number with the following:
python -m SimpleHTTPServer 8888
http-server
Step one is to npm install -g http-server
so you don’t have to do it again.
Then navigate to the desired directory and run: http-server
.
Go to the localhost addresses displayed in your terminal and go.
- Hat tip: Zell Liew
I was also looking at serve on NPM, but have not given that a try yet.