Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
a simple guide for getting a local web server set up

Do I have a web server running?


having a web server turned on doesn't necessarily mean you are serving pages on the world wide web. its what allows you to load your own static files (.html, .js etc.) in a browser via http://.

if you're not sure whether or not you have a web server running, no problem! its easy to confirm.

what happens when you visit http://localhost/?

if you're looking at a webpage, great!
you're done.

What if i don't?

Windows

the most popular web server software for microsoft computers is IIS. if its not already running, you can follow the instructions below to get things set up.

https://msdn.microsoft.com/en-us/library/ms181052(v=vs.80).aspx

iis screenshot

afterward, save a .html file in C:/inetpub/wwwroot and try to access it via http://localhost/[myfile].html. if the page is served up, you're ready to roll.

Mac

Apache comes preinstalled on Apple computers. If its not running, you can follow the instructions below to get it turned on.

http://osxdaily.com/2012/09/02/start-apache-web-server-mac-os-x/

afterward, save a .html file in ~/Users/[yourlogin]/Sites/ and try to access it via http://localhost/~[yourlogin]/myfile.html. if you can view the content, all is well.


🎵 interlude 🎵


What if I'd rather use something else?

you don't need a why, but here are a couple:

  • you don't have admin priviledges on the computer
  • you think Apache .conf files are scary, and editing text in VIM is even more frightening
  • you want something lightweight

SimpleHTTPServer (a Pythonic approach)

Python comes preinstalled on Macs (and is installed on Windows with ArcGIS Software) so it's SimpleHTTPServer module is an excellent choice.

  1. navigate into the folder where you plan on saving your .html files (using terminal/cmd) and execute the following command:

    python -m SimpleHTTPServer 1337

    if you're using Python 3.x or higher, you'd use

    python -m http.server 1337
  2. now you should be able to access your own files via http://localhost:1337/myfile.html in Chrome, Firefox or any other web browser.

<html>
  <body>
    <h1>i'm web serving!</h1>
  </body>
</html>

hello world

http-server (for Node)

Node.js gets more popular by the day, so if you already have it installed (or don't mind taking two minutes to do it now) the npm module http-server is a really good choice too.

using http-server

  1. if you haven't already installed Node.js, visit the site below and lay it down

https://nodejs.org/en/download/

  1. next, open terminal or cmd and install the http-server module globally on your machine
npm install http-server -g
  1. run it using CLI (specifying the folder you'd like to serve files from)
http-server ./[yourfolder] -p 1337
  1. now you should be able to access your files (via something like http://localhost:1337/myfile.html) in a web browser.

thats it!

@jgravois

This comment has been minimized.

Copy link
Owner Author

commented Sep 15, 2015

maybe this as a good alternative tutorial for yosemite sam:
http://coolestguidesontheplanet.com/get-apache-mysql-php-phpmyadmin-working-osx-10-10-yosemite/

we can just tell them to ignore the PHP and MySQL stuff

cc/ @nixta

@bdalal

This comment has been minimized.

Copy link

commented Aug 31, 2017

The URL provided in step 2 for SimpleHTTPServer for python is incorrect. Port should be after localhost, not filename.

Great article otherwise!

@CarmenScholte

This comment has been minimized.

Copy link

commented Oct 6, 2017

Thanks for the article! Node.js worked like a charm

@tyViroth

This comment has been minimized.

Copy link

commented Nov 13, 2017

It's does't work for me

@TroySijuwade

This comment has been minimized.

Copy link

commented Dec 9, 2017

Good article.

@Zer0xF

This comment has been minimized.

Copy link

commented Dec 29, 2017

with SimpleHTTPServer works, but I have a contact form in the site when I enter the data gives me the following error:
Error response
Error code: 501

Message: Unsupported method ('POST').

Error code explanation: HTTPStatus.NOT_IMPLEMENTED - Server does not support this operation.
How can I do?
sorry for the bad english.

@IAfanasov

This comment has been minimized.

Copy link

commented Jan 25, 2018

just want to say thank you!

@MessierObject111

This comment has been minimized.

Copy link

commented Feb 12, 2018

I'm using Mac but I have to say it doesn't work for me. I don't see any ‘Sites’ folder in username and nor setting a html file make my localhost show anything

@kellyhutchins

This comment has been minimized.

Copy link

commented Feb 14, 2018

Great article!

@SarahPS

This comment has been minimized.

Copy link

commented Feb 15, 2018

This is awesome ! Just a note to add that for the last instruction you should make sure your terminal is in the directory just above the folder with the index.html, not in that folder. Thank you

@romart007

This comment has been minimized.

Copy link

commented Feb 20, 2018

Does this allows to access on the link on mobile and tablet?

@akshatpv

This comment has been minimized.

Copy link

commented Mar 14, 2018

Thanks for the article!
For anyone testing if you have a currently running web server, using "http://localhost/", you might need to append a port number such as http://localhost/4200 (or whichever port your server might be running on)

@andraskatai

This comment has been minimized.

Copy link

commented Mar 14, 2018

Thanks for the article!

@duttaditya18

This comment has been minimized.

Copy link

commented May 3, 2018

Thanks! Really cool!

@vaibhavjindal

This comment has been minimized.

Copy link

commented May 7, 2018

Thanks!
Just what i needed.

@tarekahf

This comment has been minimized.

Copy link

commented May 10, 2018

That was fantastically easy .... thank you so much, I use NPM Server and it is way better than the complicated Eclipse and Tomcat setup.

@AllieCR

This comment has been minimized.

Copy link

commented May 22, 2018

Thank you! Exactly what I needed for testing out ES2015 import/export

@octoxalis

This comment has been minimized.

Copy link

commented Aug 9, 2018

Well, nice simple guide deserving congrats!
I just tested both the npm and python approaches: so cool to quickly be "online" to test something.

@JamesSwift

This comment has been minimized.

Copy link

commented Sep 13, 2018

A very useful tutorial. If you want to easily set up a web server on ubuntu, you might find my scripts helpful: https://github.com/JamesSwift/SharedServerTools

@byteknacker

This comment has been minimized.

Copy link

commented Oct 23, 2018

This article is amazing! I can now start a web server without writing any additional code. Thank you so much!

@saurav-bhagat

This comment has been minimized.

Copy link

commented Nov 30, 2018

What is the exact need of setting up a local server for serving static files? Like why this way is preferable?

@iarickvigasi

This comment has been minimized.

Copy link

commented Feb 1, 2019

Hey, I've created simple and beautifully looked solution for those who are not familiar with the console.

Check out https://github.com/iarickvigasi/NUIS

@arneper

This comment has been minimized.

Copy link

commented Apr 14, 2019

super simple and easy to follow. Thanks.

@proffessionalshivam4444

This comment has been minimized.

Copy link

commented Apr 20, 2019

superb explanation thnx

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.