Skip to content

[Daemon] web server

HouzuoGuo edited this page Mar 21, 2022 · 7 revisions

Introduction

The web server hosts a static personal website that consists of:

  • A home page in an HTML file.
  • Media files and other assets in directories.

The web server also hosts specialised web services such as telephone/SMS web hook, desktop-on-a-page, check out Components - rich web services to explore and enable then.

Configuration

Construct the following JSON object and place it under JSON key HTTPDaemon in configuration file:

Property Type Meaning Default value
Address string The address network to listen on. "0.0.0.0" - listen on all network interfaces.
Port integer Port number to listen on. It is usually 443 for TLS-enabled server, or 80 for non-TLS server. 443 (if TLS is enabled) or 80 (if TLS is not enabled)
PerIPLimit integer Maximum number of visits a visitor (identified by IP) may make in a second.
The number acts as a multiplier in initialising rate limit of file, directory, and web service access.
12 - resonable for a personal website
ServeDirectories {"/the/url/location": "/path/to/directory"...} Serve the directories at the specified URL location. The prefix slash in URL location string is mandatory. (Not used by default)
TLSCertPath string Absolute or relative path to PEM-encoded TLS certificate file.
The file may contain a certificate chain with server certificate on top and CA authority toward bottom.
(Not enabled by default)
TLSKeyPath string Absolute or relative path to PEM-encoded TLS certificate key. (Not enabled by default)

Host an index page using an HTML file

To host an index (home) page, place the following things under JSON key HTTPHandlers in configuration file:

  • String array IndexEndpoints - URL locations that will serve home page; in most cases it should be ["/", "/index.html"]. The prefix slash is mandatory.
  • Object IndexEndpointConfig that comes with the following mandatory attributes:
Property Type Meaning
HTMLFilePath string Absolute or relative path to the home page file written in HTML.

Host an index page using environment variable

Instead of composing an HTML file and use it as the index (home) page, you may also write the entire index page HTML content in the environment variable LAITOS_INDEX_PAGE:

sudo env 'LAITOS_INDEX_PAGE=<h1>hi from laitos</h1>' ./laitos -daemons ...,insecurehttpd,...

The HTTP server will serve the index page at /, /index.html, and /index.html.

Example

Here is an example setup that hosts a home page and media files:

{
    ...

    "HTTPDaemon": {
        "TLSCertPath": "howard-dot-net.crt",
        "TLSKeyPath": "howard-dot-net.key",
        "ServeDirectories": {
            "/media/videos": "/home/howard/CoolVideos",
            "/site/img": "/home/howard/WebsiteImages",
            "/site/css": "/home/howard/WebsiteCSS",
            "/site/js": "/home/howard/WebsiteJavascript"
        }
    },
    "HTTPHandlers": {
        ...

        "IndexEndpointConfig": {
            "HTMLFilePath": "index.html"
        },
        "IndexEndpoints": ["/", "/index.html"],

        ...
    },

    ...
}

Run

Tell laitos to run HTTPS web server in the command line:

sudo ./laitos -config <CONFIG FILE> -daemons ...,httpd,...

Tell laitos to run plain HTTP web server in the command line:

sudo ./laitos -config <CONFIG FILE> -daemons ...,insecurehttpd,...

Both HTTPS and HTTP web servers can run simultaneously, they share the same configuration, pages, and API endpoints:

sudo ./laitos -config <CONFIG FILE> -daemons ...,httpd,insecurehttpd,...

Deployment

In order for an Internet user to browse your website hosted via laitos:

  1. Your domain names must be covered by a DNS hosting service. If the concept sounds unfamiliar, check out this article from Amazon Web Service: What is DNS.
  2. DNS hosting providers usually charge ~ 1 USD per domain per month. If you are looking for a provider, check out:
  3. Check at your Domain Registrar that the domain name servers are pointing to DNS hosting providers.
  4. If you are making changes to domain name servers, it may take up to 24 hours to propagate through the Internet.

Now, create or modify a DNS "A" entry for your domain name. The entry must look like:

  • DNS name: my-domain-name.net
  • Record type: A
  • Time to live (TTL): leave at default or 5 minutes
  • Value (preference and mail server): the public IP address of laitos server

Here is an example involving two domain names and one sub-domain, assuming that laitos server is on 58.169.236.112:

DNS name Record type Time to live (TTL) Value Remark
howard-homepage.net A 5 minutes 58.169.236.112 First example
howard-blog.org A 5 minutes 58.169.236.112 Second example
cool.howard-blog.org A 5 minutes 58.169.236.112 A sub-domain of second example

Wait up to an hour for new DNS records to propagate through the Internet.

Test

Use a web browser to visit laitos web server on your domain name, pay special attention to these items:

  • If TLS is enabled, then navigate to the web server using https:// prefix, the web browser should recognise its TLS certificate as valid.
  • Visit home page locations and ensure that they work as intended.
  • Visit file directories and inspect the directory file listing.
  • If plain HTTP daemon (without TLS) is also running, then check home page and file directories using http:// address prefix as well.

Tips

  • The home page HTML is slightly processed in this way:

    • #LAITOS_CLIENTADDR is substituted to visitor's IP address.
    • #LAITOS_3339TIME is substituted to current system date and time.

    For example, the following HTML snippet:

     <p>Welcome, visitor! Your IP is #LAITOS_CLIENTADDR and the time is now #LAITOS_3339TIME.</p>
    

    Will be rendered as (IP and time are examples):

     <p>Welcome, visitor! Your IP is 41.156.72.9 and the time is now 2017-08-22T15:04:05Z07:00</p>
    
  • If you wish to take a closer look at the web server's performance over time and plot the graphs on a dashboard, check out the specialised web service prometheus metrics exporter which exports metrics many of laitos' components (including this web server daemon) to the popular open-source monitoring software prometheus.

Clone this wiki locally