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 it to develop applications.
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
http-server module globally on your machine so that you can run it from any directory via command line:
$ npm install http-server -g
Once the installation is completed, you can run the
http-server command inside any directory to quickly 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.
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
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
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
👋 If you enjoy reading my articles and want to support me to continue creating free tutorials, ☕ Buy me a coffee (cost $5) .
Need help to launch a new product? I am available for contract work. Hire me to accomplish your business goals with engineering and design.