I have a script which generates a daily report which I want to serve to the so called general public. The problem is I don’t want to add to my headaches maintance of a HTTP server (e.g. Apache) with all the configurations and security implications.
Is there a dead simple solution for serving one small HTML page without the effort of configuring a full blown HTTP server?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Try SimpleHTTPServer:
python -m SimpleHTTPServer # or the Python 3 equivalent python3 -m http.server
It should will serve whatever’s in the CWD (e.g. index.html) at http://0.0.0.0:8000.
Method 2
There is a Big list of http static server one-liners:
To get on this list, a solution must:
- serve static files using your current directory (or a specified directory) as the server root
- be able to be run with a single, one line command (dependencies are fine if they’re a one-time thing)
- serve basic file types (html, css, js, images) with proper mime types,
require no configuration (from files or otherwise) beyond the command itself (no framework-specific servers, etc)- must run, or have a mode where it can run, in the foreground (i.e. no daemons)
For example:
-
Twisted (Python)
twistd -n web -p 8000 --path .
-
Erlang:
erl -s inets -eval 'inets:start(httpd,[{server_name,"NAME"},{document_root, "."},{server_root, "."},{port, 8000},{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"}]}]).' -
Plack (Perl)
cpan Plack plackup -MPlack::App::Directory -e 'Plack::App::Directory->new(root=>".");' -p 8000
-
webfs
webfsd -F -p 8000
-
Ruby 1.9.2+
ruby -run -ehttpd . -p8000
Method 3
Use node.js , fast and lightweight.
Or
just use simple nc netcat command to start a quick webserver on a port and serve the content of a file including the server response headers.
Reference from Wikipedia:
{ echo -ne "HTTP/1.0 200 OKrnrn"; cat some.file; } | nc -l -p 8080
{ echo -ne "HTTP/1.0 200 OKrnContent-Length: $(wc -c <some.file)rnrn"; cat some.file; } | nc -l -p 8080
Method 4
Yes, nweb.
Can be found here: nweb.c
(previously at ibm.com)
To compile nweb.c:
gcc -O -DLINUX nweb.c -o nweb
Method 5
Since version 5.4.0 PHP also has a built-in web server:
php -S localhost:8000
You can Specify the web server’s documents directory with -t, for example:
php -S localhost:8000 -t /var/lib/www
If you want to be able to access the server over the network then:
php -S 0.0.0.0:8000 -t /var/lib/www
Method 6
Node has a simple, fast, light HTTP server module. To install:
sudo npm install http-server -g
(Assuming you have node and npm already installed.)
To run it, using the current directory as the website root:
http-server
This creates a server on http://0.0.0.0:8080/.
Method 7
Simple Ruby one liner to serve a directory:
ruby -run -e httpd . -p 8080
Method 8
Try using SimpleHTTPServer in Python.
mkdir ~/public_html
command_to_generate_output > ~/public_html/output.txt
(cd ~/public_html; python -c 'import SimpleHTTPServer,BaseHTTPServer; BaseHTTPServer.HTTPServer(("", 8080), SimpleHTTPServer.SimpleHTTPRequestHandler).serve_forever()')
The first two lines are setup for the web server. The last line creates a simple web server, opened on port 8080, which only serves files from ~/public_html. If only one file is in that directory, then only that is exposed: http://localhost:8080/output.txt.
Method 9
A simple fix/enhancement to a slightly unfairly (imho) down voted answer might also work. Let’s set up the html file first …
echo '<html><head><title>My Test File</title></head><body><h1>OK!</h1></body></html>' > my_file.html
(Thx to Steve Folly for catching my typo in the HTML above. fixed.)
Now you can serve it up with this one-liner:
while true; do echo -e "HTTP/1.1 200 OKrnContent-Type: text/htmlrnrn" | cat - my_file.html | nc -l -p 8080; done
This basic idea lends itself to other tricks that might work for you via other cat or subshell ideas such as:
while true; do echo -e "HTTP/1.1 200 OKrnContent-Type: text/htmlrnrnI think the date is $(date), Have a good day!" | nc -l -p 8080; done
Method 10
Simple netcat example to put in bash script:
while true ; do nc -l 80 <index.html ; done
Method 11
./devd -o -a -P devd:devd .
- -o opens url in browser
- -a for all interfaces
- -P auth user/pass
- . serve files in same directory
https://github.com/cortesi/devd/releases
Method 12
try caddy
curl https://getcaddy.com | bash
serve content from /var/www
caddy -root /var/www "browse"
now you find the server at http://localhost:2015
Method 13
Oldschool Ruby WEBrick HTTP server:
#!/usr/bin/env ruby
require 'webrick'
server = WEBrick::HTTPServer.new(:Port => 8000,
:DocumentRoot => '~/webrickroot')
# stop server with Ctrl-C
trap('INT') { server.stop }
server.start
Make sure to modify the DocumentRoot for your setup. See also this.
Method 14
Another option would be to install lighttpd. Following are suggested steps to install lighttpd on a Unbuntu 12.04 LTS.
apt-get update apt-get upgrade --show-upgraded apt-get install lighttpd ifconfig http://[your-ip-address]:80 /etc/lighttpd/lighttpd.conf (Edit to add server.port) server.port = "8080"
Note: Documentroot is where all web accessible files will be places. The location is /var/wwww
The above step will install a basic lighttpd web server. For more information refer the following references
References:
- lighttpd Web Server on Ubuntu 12.04 (Precise)
- Lighttpd Description
- Lighttpd starts but doesn’t work. lighttpd.pid Permission Denied
- Getting Started – Linode
- Web Server for testing on Linux
- Tech Tip: Really Simple HTTP Server with Python
- Which Light Weight, Open Source Web Server is Right for You?
- 6 Excellent Linux/Open Source Web Servers
- Lighttpd configuration
Method 15
You can piggy back on xinetd. Put the following config file into /etc/xinetd.d/ and service xinetd reload:
service http
{
flags = REUSE IPv4
protocol = tcp
socket_type = stream
port = 80
wait = no
user = nobody
server = /bin/echo
server_args = -e HTTP/1.0 301 Moved PermanentlynContent-Length: 0nLocation: https://goo.gl/nn
disable = no
}
Works for my redirecting purposes:
# wget 127.0.0.1 --2016-04-04 22:56:20-- http://127.0.0.1/ Connecting to 127.0.0.1:80... connected. HTTP request sent, awaiting response... 301 Moved Permanently Location: https://goo.gl/ [following] ...
Method 16
I’ve improved the nc solution a bit so it:
- Adds the
filename=hint, - Runs in a loop until Ctrl + C,
- Saves a PID to
/tmp/serveFile-$PORTso you can kill it later easily.
.
#!/bin/bash
FILE=$1;
if [ "$FILE" == "" ] ; then echo "Usage: $0 <file-to-serve> [<port:7979>]"; exit; fi
PORT=${2:-7979}
echo Serving $FILE at $PORT, PID: $$
echo $$ > /tmp/serveFilePID-$PORT
while true; do
{ echo -ne "HTTP/1.0 200 OKrnContent-Length: $(wc -c <$FILE)rnContent-Disposition: inline; filename="$(basename $FILE)"rnrn"; cat $FILE; } | nc -l $PORT
CODE=$?
#echo "Code: $CODE";
if [ $CODE -gt 128 ] ; then break; fi;
done;
rm /tmp/serveFilePID-$PORT
One could also use nc -k -l ... but this way you can
- do custom actions between files served,
- make several files alternate.
Method 17
SFK worth mentioning here
http://stahlworks.com/dev/swiss-file-knife.html
an excellent multipurpose tool with no dependencies
available in both deb and rpm flavours
sfk httpserv -port 1234
will serve current directory
sfk httpserv -port 1234 -rw
will also allow file uploading
Method 18
I used these instructions to install a web server on my CentOS machine without having to use sudo or touch any system files:
First install node:
$ cd ~ $ wget https://nodejs.org/download/release/latest/node-v8.6.0-linux-x64.tar.gz $ tar node-v8.6.0-linux-x64.tar.gz
then install http-server:
$ export PATH=~/node-v8.6.0-linux-x64/bin:$PATH $ npm install http-server
then run http-server on port 12321:
$ ~/node-v8.6.0-linux-x64/bin/node_modules/http-server/bin/http-server -p 12321
Method 19
My five cents, written in shell: https://gitlab.com/mezantrop/supermatic
Supermatic can run on FreeBSD and any other OS which has Bourne Shell
and inetd-like daemon. It serves static HTML and TXT files along with
GIF, JPEG and PNG image formats. What’s about any other standards and
features? No, never heard of them.
Method 20
A docker based one liner so you don’t have to have any particular language installed:
docker run --rm -i -p 8000:8000 -v $(pwd):/app -w /app ruby:alpine ruby -run -ehttpd . -p8000
Method 21
Pure bash: A web server in a shell script.
Also, you’ll need xinetd (I believe available in any distro) to listen port and run the script when needed, so you don’t have to code tcp stack etc in bash.
Method 22
To nweb.c I have added directory listing functionality. https://github.com/jbabuc/nweb
nweb.c, simple http server with directory listing.
nwebs, statically compiled binary for linux.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0