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 !
ContentsShow / Hide
1. 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.
python -m http.server
test: Open up your favorite web browser, type 127.0.01:8000 in the URL bar and hit enter .
Enjoy free shipping and One-Day delivery, cancel any time.
2. 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 .
3. 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.
4. netcat as a linux simple web server
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
5. 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.
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.