Here's how you can create a lightweight web server on Linux easily to test some code, serve static HTML files or whatever you like.
There's no need of a full blown web server like Apache or NGINX, just few lines of basic command will be fine to do this job. Lets do it in 5 different ways !
Contents
Instant python web server
Python is pre-installed in almost every UNIX or GNU/Linux distributions, packs many feature reach modules inside it. Python commands could be used as an web server very easily.
For r python 2.7.x versions use this command.
python -m SimpleHTTPServer port_number
Replace the port_number with port number of your choice.
python -m SimpleHTTPServer 8080
To start a python web server on port 8080, by default it binds port 8000 with all available network interfaces.
Note: Binding any port number bellow 1024 requires root privilege, in such case use sudo
before the command . If there is a index.html file at that directory, it will be served by default, else contents of that directory will be served.
For python 3.x version use this command.
python3 -m http.server
test: Open up your favorite web browser, type 127.0.01:8000 in the URL bar and hit enter.
You can use the --bind or -b option to run the web server on a specific network interface, or even use it to bind with IPv6 address. as an example-
python3 -m http.server --bind ::
That's an example of using python's internel http.server module to serve few web pages, here :: denotes the IPv6 localhost. You can also define a specific IPv4 or IPv6 address and port, example below.
python3 -m http.server -b 2603:4900:3bd0::786:bbad:1e5d 8080
Here 2603:4900:3bd0::786:bbad:1e5d is an example IPv6 address and 8080 is the desired port.
Busybox httpd web server
Busybox, The Swiss Army knife of Embedded Linux, packs many stripped-down UNIX tools inside a single static executable binary. It runs on most UNIX/Linux systems and pre installed in all Debian based distributions.
Busybox have an awesome feature rich HTTP server applet, busybox httpd , very simple and lightweight but also configurable enough. Download latest busybox and read more about here.
busybox httpd --help # show available options busybox httpd -p 127.0.0.1:8080 -h /var/www/ # start start httpd pkill busybox # to stop busybo httpd
note: by default busybox httpd binds port 80 with all available network interfaces and reads /etc/httpd.conf configuration file .
Node.js simple web server
If you're already familiar with NodeJs or love to fiddle with javascript, the easiest way to server static files is to use the http-server
node module.
Just install the module globally, and start the server just by typing the command http-server
.
sudo npm install http-server -g
http-server
Obviously you need super user privileges to install node modules globally. By default the hhe server binds to port 8080, to bind a port below 1024, you also need superuser privilege.
Read more about the http-server here.
Webfsd lightweight web server
Webfs or webfsd is a very lightweight, linux simple web server for static pages and files. It is highly configurable, configurations are stored at the /etc/webfsd.conf
file, which could be very easily edited.
sudo apt-get install webfs # install webfs in Debian/Ubuntu sudo service webfs start # start the webfs server
note: By default webfsd binds port 8000 with all available network interfaces with /var/www/html as document root directory in Debian.
Netcat as a linux simple web server
Netcat or nc
, the swiss army knife for TCP/IP , is a very popular network troubleshooting tool.
It could be used as port scanner, port listener, file transfer agent, network connected file backup tool, as a backdoor even as a vary lightweight web server, amazing !
Netcat based web server is mainly used for serving text only files over HTTP, useful in monitoring purpose. Assuming you have basic shell scripting knowledge, lets start a simple HTTP server with netcat. Save the script bellow with whatever name you want , modify it according to your needs, make it executable with chmod +x
and run it .
#!/bin/sh while true; do echo -e "HTTP/1.1 200 OK\r\n $(cat /var/www/index.html)" | nc -lp 1500 -q 1 sleep 1 done
You may want to change the line cat /var/www/index.html
with whatever you want to do, like /sbin/ifconfig eth0
etc. etc. If the sleep 1 line is removed, it will be difficult to stop the script with CTRL+C
.
PHP inbuilt lightweight web server
If you have installed php 5.4 or above, then you could easily use the php CLI to create a simple and lightweight web server with.
It also have the capability of serving simple php based dynamic pages. simply use the command bellow .
php -S 127.0.0.1:8080
If there is a index.html or index.php file , it will be server by this simple php server , now test it with your favorite web browser.
Conclusion
If you need to create a simple HTTP server with IPv6 capability in python, it's here, python IPv6 web server.
I think now your problem is solved, if you need any further assistance just drop a comment, we’d be happy to assist you. Feel free to share this tutorial with your friends.
Yavor Ivanov says
Hi!
First of all, thanks for the great site!
I have a question : i was started web server and with python3, and with Node.js (no difference for me), but everything is visible, the whole pc... I want to be visible, to can entered just directories or files, which i want to show (share) with somebody.
Can u help me pls, how to do that?
Matthew says
Good effort. These are simple. Not sure if I would call node lightweight, especially since people looking here are likely to not have a lot of overhead.
Patrick Prucha says
Hello there. Thank you very much for the information. Im trying to find out whether i really need a web server. I go through the same process all the time. why do i need one? what advantages? Im not ready to setup a web site on my system so im still trying to figure out whether i need one.
Back to the reason for this comment. I really liked the idea of using python as a web server, since it serves me up the contents of the directory i chose. I can use this strategy to set up a web site for pictures and all. Only one thing! How do i remotely get access to the web page described in your "python" method?? i tried to get on via my windows computer. went locally and remotely using both types of ip addresses and no luck. Im sure i need to add some port forwarding, but i am just checking in with you to see if this was the purpose of using the light weight web servers. Please get back to me at patrickprucha@yahoo.com. Many thanks
Patrick
Arnab Satapathi says
I use those servers to serve files on a local network, or rarely, to run a live demo of web development projects.
Sent you a mail.
miguipda says
Hi,
The menu About - Contact us answer that you are not reachable telling that I must use another way to contact you. Then I used this concerned form.
============
Big challenge - Personal Cozy cloud on OpenWRT
by considering the fact that OpenWRT must not be compile with needed packages could you please consider and make a tutorial of how do install Cozy cloud on OpenWRT ?
You must know it needs node.js but it is better to use Light Cozy that allow to just add what it is needed :
Light Cozy : https://cozy-labs.github.io/cozy-light/
Full cozy : https://cozy.io/en/
Have a nice day,
Miguipda 😉
Arnab says
Good to see you again, a long time. What a coincidence, I'm thinking about writing a tutorial about running Node.js on OpenWrt Routers. Surely I'll write how to setup Light Cozy on OpenWrt as soon as possible. Node.js packages are currently not available on OpenWrt repo so we'll have to build it.
Doing such experiments in constrained environment is damn tough, laptop with 40GB HDD!(after 1TB crashed) and 1.5GB monthly download+upload bandwidth quota, seriously !
Oppps,, sorry about the contact form, that's some PHP related issue while sending mail from the hosting server and I'm not a PHP expert to solve this issue. Mail me to as.work0(at)gmail.com .
Btw, I'hd sent a mail to your mail address(which you filled in the comment form) to wish you christmas.
sayantan says
Nice web site dude ........carry on