How to start a local web server from any directory

In this article 👇

Sometimes you want to start a web server from any directory in your local file system. It doesn't necessarily mean that you are looking to serve web pages on the Internet, but just a way to load the static files (HTML, JavaScript, styles, etc.) in a browser.

There are multiple ways to start a web server from a local directory. It depends on the programming language you have already configured and used to develop applications.

Node.js

If you use Node.js and have already installed it along with NPM (literally takes a couple of minutes to install), the http-server module can be a good choice for this purpose.

The http-server is a simple, zero-configuration command-line HTTP server that is powerful enough to be used for testing, local development, and learning.

Run the following command to install the http-server module globally on your machine so that you can run it from any directory via the command line:

$  npm install http-server -g

Once the installation is completed, you can run the http-server command inside any directory to launch a web server. For example, if you want to start a web server from /opt/sites/blog, use the following commands:

# Navigate to the directory
$ cd /opt/sites/blog

# Launch HTTP server
$ http-server

That's it. Now you can visit http://localhost:8080 to access your files.

By default, http-server uses port 8080 to launch the server. However, you can use the -p flag to specify a port of your choice:

# Launch HTTP server on port 4000
$ http-server -p 4000

Python

If you use Python and have it installed already, you can use the SimpleHTTPServer module to start a web server from any local directory.

Just navigate to the folder from where you want to serve static files, and then execute the following command in your terminal:

# Navigate to the directory
$ cd /opt/sites/blog

# Launch HTTP server
$ python -m SimpleHTTPServer 8080

The above command will start a web server on port 8080. If you're using Python 3.x or higher, you'd use:

$ python -m http.server 8080

PHP

If you are already using a modern PHP version (5.4.x and higher), you can use the following commands to launch a development server from any directory of your choice:

# Navigate to the directory
$ cd /opt/sites/blog

# Launch HTTP server
$ php -S localhost:8080

The above example will start a web server on port 8080.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.