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.
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
If you want to share files and dirs, cd into whichever directory you want to serve
Should 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 address127.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 https://docs.python.org/3/library/http.server.html
Here 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:
Ruby:
Node:
Php:
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 :
Then run
make
and you have your darkhttpd executable. (Place it in/usr/local/bin
to make it available to every user)Run
or
to 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 toindex.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 http://127.0.0.1 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:
You can install it on Debian/Ubuntu with
And use as
It will print an URL to put into a browser at the other end.
If 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.
One 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 usingcat filename
.For a more complicated example check out bashttpd.
Also note the differences between netcat-traditional and netcat-openbsd. Ubuntu provides both versions.