Quickly Run a Web Server To Serve a Local Directory Path

Sometimes we just want to serve off static contents (files and folders) from a local directory so that we could do quick local development or some form of testing in the browser (localhost:8080). Instead of spending a while installing and configuring a proper solution (oh and reading their documentations) like Apache or Nginx, depending on the language you’re comfortable with or the interpreter installed on your system, we’ve a few quick and easy options.

Node

Fire up a server with the http-server npm package.

$ npx http-server
Starting up http-server, serving ./

http-server version: 14.1.0

http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none

Available on:
  <http://127.0.0.1:8080>
  <http://192.168.189.92:8080>
Hit CTRL-C to stop the server

PHP

Fire up a web server with the in-built PHP web server.

$ php -S 0.0.0.0:8080
PHP 7.2.24-0ubuntu0.18.04.10 Development Server started at Sat Feb  5 09:54:51 2022
Listening on http://0.0.0.0:8080
Document root is /home/rish # docroot
Press Ctrl-C to quit.

Python

Fire up an HTTP web server with Python’s http.server module.

$ python3 -m http.server
Serving HTTP on 0.0.0.0 port 8000 (http://0.0.0.0:8000/) ...

Do note that:

  • All these options are not for production use-cases but only for local development and quick testing.
  • The Node and Python options are plain simple static asset web server. They won’t execute any code (Ruby, PHP, Python, JavaScript, etc.) in any files. The PHP option will only execute any PHP script (apart from serving static assets) and return the result to the client (browser).
  • We could also use some docker image with Apache or Nginx to setup a more sophisticated and powerful web server but that process won’t be as quick as the ones listed above. You should totally do this though when actually doing long-term development.

Leave a Reply

Your email address will not be published.