5 ways to create an instant lightweight web server with Linux command line
You can create a simple and lightweight web server very easily to test some code, serve files or whatever you like. No need of a full blown web server like apache or nginx, few lines of command will be fine to do this job.
Lets do it in 5 different ways !
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 folder, 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 .
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 simle 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 HTTP server
Webfs or webfsd is a very lightweight and simple HTTP web server for static pages and files. It is highly configurable, webfsd 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 folder in Debian.
4. netcat as a 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 basic 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 .
while true; do echo -e "HTTP/1.1 200 OKnn $(cat /var/www/index.html)" | nc -lp 1500 -q 1 sleep 1 done
You may want to chaneg 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 .
5. php inbuilt 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.
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.