How do I set up the simplest HTTP local server? [duplicate]
I use the R Studio IDE to do many things, one of which is to serve local websites utilizing the blogdown package which is a fork of hugo. I write the code in R Studio and preview the site utilizing the blogdown::serve_site() command. This is the easiest way I know of previewing static HTML sites. Utilizing hugo R Studio coerces my browser to go to the 127.0.0.1:XXXX location and my local site is previewed before my eyes.
Can I serve local sites natively with Ubuntu 18.04? How? I imagine it should be very simple. Searching online I can't find any simple way to do it though. If I simply open the static HTML files directly in my browser they showup wonky. All image links are immediately broken. Formatting of headings, hyperlinks, etc is not the same as when I serve the page locally with hugo.
EDIT - I did not really define my term 'simplicity'. There are two different approaches (so far) in the answers, one that is simplest to the end user, and one that is simple with regard to operations being performed by my computer. I like both approaches and will welcome answers utilizing any approach as I test them. Thank you.
77 Answers
Ubuntu ships using python3 as its default, and they have gone to great lengths to make this extremely easy for us :D
To start the http server on port port simply type
python -m http.server portIf you want to share files and dirs, cd into whichever directory you want to serve
cd /my/html/files
python -m http.server 8080Should you want to use an address other than the default 0.0.0.0 you can use --bind
Ex: python -m http.server 8080 --bind 127.0.0.1 will serve them at the address 127.0.0.1:8080 :)
Edit: Whether or not it truly was great lengths, I'll leave that to the reader
Also for your convenience here is a link to the docs
8Here is a list of HTTP server in one line. I'm sure there is one that will fit your purposes/existing tooling.
Hereafter is a subset of the link, that contains in my opinion the most convenient ones.
Python:
python -m http.server 8000Ruby:
ruby -run -ehttpd . -p8000Node:
npm install -g http-server
http-server -p 8000Php:
php -S 127.0.0.1:8000 4 One simple way to setup a static http site is to use darkhttpd
There is no package in ubuntu for that but the software is just one single source file that you can download with a tarball on the site or with git :
git clone
cd darkhttpdThen run make and you have your darkhttpd executable. (Place it in /usr/local/bin to make it available to every user)
Run
./darkhttpd /path/to/wwwrootor
./darkhttpd --helpto get help about the command
One can specify directory or port to use and many other options.
sudo apt install apache2 will install the apache2 webserver. By default it provides access to index.html in the /var/www/html folder; replacing this file with whatever you want to host is the easiest way to do things, then you can navigate to on your local machine, or to your machine's IP address on your network and it will serve the pages.
If you do not want to remember the python command's arguments, use woof:
Description: share files through HTTP protocol Woof (Web Offer One File) is a tool to copy files among hosts. It can serve a specified file on HTTP, just for a given number of times, and then exits.
Features include: * it can share stuff "one shot" and exit just after he served that file. * it can share things among different operating system or different devices (e.g.: a smartphone), and allows one to upload files easily. * it can also show a simple html form in order to upload file (useful if the client hasn't a way to serve the file).You can install it on Debian/Ubuntu with
apt install woofAnd use as
woof kittens.pngIt will print an URL to put into a browser at the other end.
2If you are a Google Chrome user, it can be as easy as using the Web Server for Chrome. Simply install it, launch it, click Choose Folder to select the directory that holds your static files.
2One of the simplest (and the most limited) solutions would be using netcat as described in this article:
while true; do { echo -e "HTTP/1.1 200 OK\r\n$(date)\r\n\r\n<h1>hello world from $(hostname) on $(date)</h1>" | nc -vl 8080; } done
This example is serving on port 8080, serving on first 1024 ports will require you to use sudo. You can also serve a file this way simply by using cat filename.
For a more complicated example check out bashttpd.
Also note the differences between netcat-traditional and netcat-openbsd. Ubuntu provides both versions.
More in general
"Zoraya ter Beek, age 29, just died by assisted suicide in the Netherlands. She was physically healthy, but psychologically depressed. It's an abomination that an entire society would actively facilitate, even encourage, someone ending their own life because they had no hope. Th…"