Skip to content

Local Server Tutorial

Daniel Shiffman edited this page Jan 6, 2015 · 7 revisions

Command Line

We'll be using the command line regularly in this class. Nothing too fancy; just the basics. Get familiar, at the very least, with the commands ls, cd, and mkdir. This will help.

Setting Up A Local Server (for Macs)

  1. Open up a Terminal Window. (If you've never used the Terminal before, hit command-space and start typing "Terminal". Click on Terminal and a Terminal window should open.)

  2. Navigate to the directory (i.e.folder) where your files are saved. An easy way to do this is to type cd and then a space and then drag and drop the folder (where your files live) on top of the terminal window. The path to this folder (directory) should appear. Then click enter.

  3. After clicking enter, you can type the command pwd to see exactly where you are in terms of your directory structure

  4. Next, check your version number of python (python comes preloaded on Macs) by typing the following command into the Terminal window and then hitting enter:

python --version

If you are running version 2 (i.e Python 2.x.x) type the following command and then hit enter:

python -m SimpleHTTPServer 8000

If you are running version 3 (i.e. Python 3.x.x) type the following command and then hit enter:

python -m http.server 8000
  1. You may (or may not) see this in the Terminal
Serving HTTP on 0.0.0.0 port 8000 ...

Either way, open up your web browser and navigate to the following url:

http://localhost:8000
  1. You should see links to all the files in your folder. Click on the html file and your application should load. (Note, if you name your html file index.html, the application will load when you navigate to http://localhost:8000

Some extra steps if you are on windows

Before you can run a local server on windows with python, you'll need follow a few steps.

  1. Download and install python.

  2. You'll need a console application that lets you interact with Git and your computer in much the same way as the Terminal application on Apple computers. There are several options.

  3. Install Git Bash

  • Download and install git.

  • use all the default settings.

  • When you get to the Adjusting your PATH environment screen, be sure to choose the option for Use Git Bash Only.

  • You should have now have a program in your Start Menu called Git Bash. If you hit the Windows key to pull up the start menu and type “git bash” you should see the program highlighted.

  • You will now need to make sure that Git Bash recognizes python. You can do this temporarily with this command in Git Bash: PATH=$PATH:/c/Python27/ (or whatever version of python you are running). However if you want this to be permanent you'll need to add c:\python27 to your system environment variable path. Here is how you access that..

  • An alternative to Git Bash is the windows built-in command prompt. This should work but you'll have to use DOS versions of the unix commands I'm showing in class.

  • An alternative to all of this is WAMPServer.

    • Download WampServer from http://www.wampserver.com/en/.
    • Install WampServer and follow instructions.
    • The “www” directory will be automatically created (usually c:\wamp\www).
    • Create a subdirectory in “www” and put your HTML/JS files inside.
    • Open your internet browser and go to the URL : http://localhost/yourfile.html.

References