27

This question already has an answer here:

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.

marked as duplicate by muru, Eric Carvalho, Charles Green, George Udosen, David Z Dec 20 '18 at 3:18

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 5
    Not sure what you mean by 'wonky'. When I open static HTML from a file, it shows up the way I expect. – user535733 Dec 17 '18 at 17:43
  • @GeorgeUdosen To be fair, neither of those are a good match for the "simplest HTTP local server". There are plenty of far more lightweight / simple options available. – Jon Bentley Dec 18 '18 at 0:19
  • 1
    I support @user535733. You should investigate why the webpages are not displaying properly without a server, because those problems will inform what solution you need. Perhaps the pages contain URLs ("http://") to local files? In that case you need a server, or you change the URLs to relative paths and make sure the files exist relative to the viewed document. This implies that you need the whole web site file tree locally. – Peter A. Schneider Dec 18 '18 at 12:12
  • @Peter A. Schneider I drag my index.html file onto Chrome and this is the url file:///C:/Users/jason/hugoweb/public/index.html in my address bar. All image links are immediately broken. And formatting of headings, hyperlinks, etc is not the same as when I serve the page locally with hugo. Why, I have no idea?!? I'm open to answers. – Jason Hunter Dec 18 '18 at 14:06
  • 1
    if you have npm globally, install http-server globaly, then you can do in any directory in your terminal http-server to serve the files in localhost. (Default port 8080 but use -p to configure the port). – Mathijs Segers Dec 18 '18 at 15:16
57

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 port

If you want to share files and dirs, cd into whichever directory you want to serve

cd /my/html/files
python -m http.server 8080

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 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 https://docs.python.org/3/library/http.server.html

  • 1
    Is the port the XXXX in 127.0.0.1:XXXX or is it the entire string 127.0.0.1:XXXX? – Jason Hunter Dec 17 '18 at 18:22
  • 2
    It will just be the XXXX but by default it will serve at 0.0.0.0:port I will update my answer to reflect how to bind to a different address – j-money Dec 17 '18 at 18:24
  • 3
    If you are using a very old ubuntu release: in python2 the module is called SimpleHTTPServer. note however that it always binds to 0.0.0.0 you can only choose the port, thus use it exclusively if you are in a secure network. – Bakuriu Dec 17 '18 at 21:30
  • Note you need a wrapper in case it crashes. – Thorbjørn Ravn Andersen Dec 19 '18 at 14:51
29

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:

python -m http.server 8000

Ruby:

ruby -run -ehttpd . -p8000

Node:

npm install -g http-server
http-server -p 8000

Php:

php -S 127.0.0.1:8000
  • Nice answer. It provides information about many different options. – cezar Dec 19 '18 at 6:49
5

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 https://unix4lyfe.org/git/darkhttpd
cd darkhttpd

Then 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/wwwroot

or

./darkhttpd --help

to get help about the command

One can specify directory or port to use and many other options.

4

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 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.

  • after I install apache2 do I need to initiate the program with an apache2 command from the command line? Or does it run continuously in the background? I'd prefer not to run it continuously in the background. – Jason Hunter Dec 17 '18 at 17:46
  • 1
    you can turn it on/off/enable at startup by running (respectively) sudo systemctl start apache2 sudo systemctl stop apache2 sudo systemctl enable apache2. by default it does not run at startup, but does run after installation. If you change index.html while it is running, it will just serve the new version without needing a restart. – Minty Dec 17 '18 at 17:47
  • 4
    apache2 is way overkill for just a simple static site. – solsTiCe Dec 17 '18 at 17:50
  • 4
    I get the impression that simplicity and speed are the goal more than the weight of the program. – Minty Dec 17 '18 at 17:51
  • 1
    @Minty yes and OP has also to deal with permissions and stuff with Apache. The Python HTTP server solution is much simpler. Yours would be needed if the site was PHP based. :) – Andrea Lazzarotto Dec 20 '18 at 0:02
2

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.

  • This is a super underrated way of doing it – J Lewis Dec 18 '18 at 16:35
1

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 woof

And use as

woof kittens.png

It will print an URL to put into a browser at the other end.

0

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 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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.