How to setup a local test server?

This article will show you how to install a simple local test server on your computer and its basic usage.


premise: You need to know how the internet works and what a web server is
Target: You will learn how to configure a local test server.



Local files and remote files

In most of the examples we told you to just open your example directly in the browser, there are many ways to do this, you can do this by double-clicking or dragging the HTML file into the browser window, or in the browser Select the HTML file, etc. in the selection . 文件 > 打开... 


If you open the local example, you can see in the address bar that the address starts with, followed by the path to the example file on the local hard drive. file:// 


By contrast, if you're looking at our example hosted on GitHub (or another example on a remote server), the web address will start with or , indicating that the file was transferred over HTTP. http://  https:// 


Test for problems

Some examples will not run if you open it as a local file. This can be due to various reasons, most likely:

They have asynchronous requests.  Some browsers (including Chrome) will not run asynchronous requests if you just run the example from a local file . This is because of security restrictions . They have server-side code .  Server-side languages ​​such as PHP or Python require a special server to interpret the code and provide results.


Running a simple local HTTP server

To solve the problem with asynchronous requests, we need to test the examples by running them on a local web server. For our purposes, one of the easiest ways is to use Python's SimpleHTTPServermodules.

we need to:

1. Install Python.

If you are using Linux or Mac OS X, it should already be available on your system.

If you are a Windows user, you can get the installer from the Python homepage and follow the instructions:


Go to python.org Under the "Downloads" section, click the link for Python "3.xxx". At the bottom of the page, select the Windows x86 executable installer and download it.


When it has downloaded, run it. On the first installer page, make sure the "Add Python 3.xxx to PATH" checkbox is checked. Click Install , and then click Close when the installation is complete .


Open your Command Prompt (Windows)/Terminal (OS X/Linux).

To check if Python is installed, enter the following command: python -V

​The version number you have installed should be given below, use the cdcommand to navigate to the directory where your example is located.

# Enter the directory you want to enter

Example cd Desktop

# Use two dots to indicate the directory cd .. enter the command to start the server in that directory: # If the Python version returned above is 3 .X python -m http.server

# If the Python version returned above is 2.X python -m SimpleHTTPServer This will run the contents of the directory on port 8000 on the local web server by default.

You can localhost:8000access this server by going to the URL in your web browser. Here you will see the contents of the directory listed - click on the HTML file you want to run.

Note:  If you already have something running on port 8000, you can choose another port by running the server command, then choosing another port number (eg python -m http.server 7800 (Python 3.x) or python -m SimpleHTTPServer 7800 (Python 2.x)). Then you can access your content localhost:7800.


Run server-side languages

Python's SimpleHTTPServermodules are useful, but it doesn't know how to run code written in languages ​​like PHP or Python. To handle this, you need a few more things - exactly what you need depends on the server-side language you're running. Here are a few examples:


  • To run Python server-side code, you need to use the Python web framework. You can learn how to use the Django framework by reading Django Web Framework (Python).
  • Flask is also a good option (slightly lighter). To run Flask, you need to install Python/PIP first, then pip3 install flask install Flask using . At this point, you should be able to run the Python Flask example python3 python-example.pyand open it to view in your browser . localhost:5000 
  • To run Node.js (JavaScript) server-side code, you can use Node directly or choose a framework built on it. Express is a good choice - see Express Web Framework (Node.js/JavaScript).
  • To run PHP server-side code, you need a server setup that can interpret PHP. Good options for local PHP testing are MAMP (Mac and Windows), AMPPS​ ( Mac, Windows, Linux) and LAMP (Linux, Apache, MySQL and PHP/Python/Perl). These are complete packages that create local setups that allow you to run an Apache server, PHP and MySQL database.