# How to start a local web server from any directory

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Mar 27, 2020, updated Oct 1, 2022. Topics: Node.js.
Canonical URL: https://attacomsian.com/blog/local-web-server

> A quick guide to learn how to start a web server from any local directory.

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](https://attacomsian.com/blog/install-nodejs-npm-ubuntu) (literally takes a couple of minutes to install), the [http-server](https://www.npmjs.com/package/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:

```bash
$  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:

```bash
# 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:

```bash
# 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](https://docs.python.org/2/library/simplehttpserver.html) 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:

```bash
# 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:

```bash
$ 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:

```bash
# 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`.
