65

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.

7
  • 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, 2018 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. Dec 18, 2018 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. Dec 18, 2018 at 12:12
  • 1
    @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. Dec 18, 2018 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). Dec 18, 2018 at 15:16

7 Answers 7

123

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

9
  • 1
    Is the port the XXXX in 127.0.0.1:XXXX or is it the entire string 127.0.0.1:XXXX? Dec 17, 2018 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, 2018 at 18:24
  • 4
    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, 2018 at 21:30
  • 1
    @KFleischer Enter Ctrl-C, or use the kill command
    – db-inf
    Dec 20, 2019 at 10:51
  • 1
    This is very useful
    – Kinjal
    Oct 2, 2020 at 4:13
64

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
4
  • 3
    Nice answer. It provides information about many different options.
    – cezar
    Dec 19, 2018 at 6:49
  • 3
    In python 2.7 the command is: python -m SimpleHTTPServer 8000
    – greuze
    Jul 19, 2019 at 7:33
  • 2
    I strongly recommend the Node option (http-server). It is the most feature-rich one, supporting partial downloads and more, which is great if you are streaming videos for instance. Sep 8, 2020 at 12:58
  • 1
    @BoraM.Alper if you need specific features, maybe you shouldn't look for a simple http server, but rather configure one to fit your needs.
    – Richard
    Sep 9, 2020 at 13:05
9

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.

5
  • 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. Dec 17, 2018 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, 2018 at 17:47
  • 7
    apache2 is way overkill for just a simple static site.
    – solsTiCe
    Dec 17, 2018 at 17:50
  • 5
    I get the impression that simplicity and speed are the goal more than the weight of the program.
    – Minty
    Dec 17, 2018 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. :) Dec 20, 2018 at 0:02
4

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.

3
  • 1
    This is a super underrated way of doing it
    – J Lewis
    Dec 18, 2018 at 16:35
  • is there anything like this for firefox?
    – creanion
    Aug 17, 2021 at 8:19
  • Meanwhile, there is also a native app. May 17, 2023 at 3:11
4

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.

2
  • wow so easy, upload and download
    – Pieter
    Jul 23, 2019 at 2:52
  • 2
    Unfortunately, woof doesn't seem to be included in Ubuntu 20.04
    – ws6079
    Oct 6, 2020 at 22:03
1

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 .