How to Host an HTTP Server in One Line – All Programming Languages

Sometimes, we need a quick HTTP server, either for testing purposes or transferring files. Setting up an Apache server might prove to be too much work for something trivial. Thus it is really handy to have a bunch of one liners which can quickly spawn a HTTP server for us. In this module we have listed several such commands using various programs to help us host quick HTTP servers from the terminal itself to suit our needs!

Setup a One-line HTTP Server using Different Languages

Let’s check the code that we need to run to setup a one-line HTTP server using some of the major programming languages. We’ll be using the built-in HTTP server code that is available in all these languages.

1. Host an HTTP Server on Python

Using Python 2.x:

$ sudo python2 -m SimpleHTTPServer 80

Using Python 3.x:

$ sudo python -m http.server 80

Using Twisted:

We’re using the Twisted module over here. You can install the same using the below commands:

$ sudo pip3 install Twisted
$ sudo twistd -n web -p 80 --path .

Or:

$ sudo pip3 install Twisted
$ sudo python -c 'from twisted.web.server import Site; from twisted.web.static import File; from twisted.internet import reactor; reactor.listenTCP(80, Site(File("."))); reactor.run()'

2. Host a Webserver using NodeJS

Using http-server:

$ sudo npm install -g http-server
$ sudo http-server -p 80

Using node-static:

$ sudo npm install -g node-static
$ sudo static -p 80

3. Using PHP to Host a Local Server

$ sudo php -S 127.0.0.1:80

4. Running a Webserver on Rust

$ cargo install miniserve
$ sudo ~/.cargo/miniserve -p 80 .

This is one of my favorites because of the very pretty UI

Miniserve
Miniserve

5. Hosting an HTTP server in one line of Ruby

$ sudo ruby -rwebrick -e'WEBrick::HTTPServer.new(:Port => 80, :DocumentRoot => Dir.pwd).start'

For Ruby 1.9.2+

$ sudo ruby -run -ehttpd . -p80

Using adsf:

$ sudo gem install adsf
$ sudo adsf -p 80

Using sinatra:

$ sudo gem install sinatra
$ sudo ruby -rsinatra -e'set :public_folder, "."; set :port, 80'

6. One-line HTTP server on Perl

Using HTTP::Server:

$ sudo cpan HTTP::Server::Brick
$ sudo perl -MHTTP::Server::Brick -e '$s=HTTP::Server::Brick->new(port=>80); $s->mount("/"=>{path=>"."}); $s->start'

Using Plack:

$ sudo cpan Plack
$ sudo plackup -MPlack::App::Directory -e 'Plack::App::Directory->new(root=>".");' -p 80

Using Mojolicious:

$ sudo cpan Mojolicious::Lite 
$ sudo perl -MMojolicious::Lite -MCwd -e 'app->static->paths->[0]=getcwd; app->start' daemon -l http://*:80

7. Hosting a webserver on Erlang

$ sudo erl -s inets -eval 'inets:start(httpd,[{server_name,"NAME"},{document_root, "."},{server_root, "."},{port, 80},{mime_types,[{"html","text/html"},{"htm","text/html"},{"js","text/javascript"},{"css","text/css"},{"gif","image/gif"},{"jpg","image/jpeg"},{"jpeg","image/jpeg"},{"png","image/png"}]}]).'

8. Busybox HTTP Server

$ sudo busybox httpd -f -p 80

9. HTTP Server using Webfs

$ sudo webfsd -F -p 80

Conclusion

Thus we saw how to spawn a a quick HTTP server. Note that to run the HTTP server on a lower port (like port 80) might require sudo privileges. To avoid running the server with elevated privileges, use a higher port(like port 8080)

Also, once you have hosted your server, you can also port forward it using tools like ngrok to port forward it !