Simple Command Line HTTP Server
Sometimes it’s useful to run a simple HTTP server from the command line to serve files from your current directory (or any directory for that matter). This guide lists several of the most commonly available tools for serving files over HTTP using tools you probably already have installed without the need for Apache, nginx or any other full HTTP server software.
HTTP Server One-Liners
Python 2’s SimpleHTTPServer module will allow you to quickly serve your current working directory over HTTP at
python -m SimpleHTTPServer
To specify a port other than 8000, simply pass it as the next argument to the command. For example, to serve content on port
1337 you could use the following.
python -m SimpleHTTPServer 1337
The SimpleHTTPServer module has been merged into http.server in Python 3. As above, the following example will serve the contents of your current working directory over HTTP at
python3 -m http.server
Like the Python 2 variant of this command, it’s possible to pass the port for the server to listen on.
python3 -m http.server 1337
It is also possible to specify the IP address this simple python server will bind to by using the
--bind command line argument.
python3 -m http.server --bind 127.0.0.1 8000
PHP has had an inbuilt web server since version 5.4.0. This can be extremely useful for testing one-off PHP files, or simply allowing you to open a directory in your web browser.
php -S localhost:8000
You can specify the document root directory using the
-t command line argument.
php -S localhost:8000 -t /home/user/web_dir
It’s also simple to use Ruby 1.9.2+ to serve files from the current directory. It does a great job of displaying nice file listing pages as well as serving static content. Use the
-p flag to specify port.
ruby -run -ehttpd . -p8000
Hopefully at least one of these suggestions proves useful for you! Feel free to leave a note in the comment section if you feel like there are any other common ways to serve files from the command line that should be added to this list.