Armbian Webserver - monkeymia/orangepizero GitHub Wiki

Webserver

Installation

A web-server can be installed with:

sudo apt install lighttpd

This package installs:

  • application (daemon): /usr/sbin/lighttpd
  • software to control (graceful stop/restart/...) the daemon /usr/sbin/lighttpd-angel
  • configuration * /etc/lighttpd/lighttpd.conf * /etc/lighttpd/conf-available/ * /etc/lighttpd/conf-enabled/
  • new user "www-data"
  • new group "www-data"
  • placeholder page :
    • You should replace this page with your own web pages as soon as possible.
    • Unless you changed its configuration, your new server is configured as follows:
    • Configuration files can be found in /etc/lighttpd. Please read /etc/lighttpd/conf-available/README file.
    • The DocumentRoot, which is the directory under which all your HTML files should exist, is set to /var/www/html.
    • CGI scripts are looked for in /usr/www/cgi-bin, which is where Ubuntu packages will place their scripts. You can enable cgi module by using command "lighty-enable-mod cgi".
    • Log files are placed in /var/log/lighttpd, and will be rotated weekly. The frequency of rotation can be easily changed by editing /etc/logrotate.d/lighttpd.
    • The default directory index is index.html, meaning that requests for a directory /foo/bar/ will give the contents of the file /var/www/foo/bar/index.html if it exists (assuming that /var/www is your DocumentRoot). You can enable user directories by using command "lighty-enable-mod userdir"

After installation the webserver is already running.

Enable Common Gateway Interface (CGI)

The web-server calls a sub-process to process the request. The sub-process does the dynamic creation of the content to be returned.

Steps :

  1. Enable cgi module within web-server :
demonstrator@orangepizero:~$ sudo lighty-enable-mod cgi
[sudo] password for demonstrator:
Enabling cgi: ok
Run "service lighttpd force-reload" to enable changes
demonstrator@orangepizero:~$ sudo service lighttpd force-reload
demonstrator@orangepizero:~$

This creates /etc/lighttpd/conf-enabled/10-cgi.conf

  1. Configure files with extension ".py" and ".cgi" non-static files :

    sudo nano /etc/lighttpd/lighttpd.conf
    
    static-file.exclude-extensions = ( ".php", ".pl", ".fcgi" )
    static-file.exclude-extensions = ( ".php", ".pl", ".fcgi", ".cgi", ".py" )
    
  2. map file extension and application

    sudo nano /etc/lighttpd/lighttpd.conf
    
    $HTTP["url"] =~ "^/cgi-bin/" {
       cgi.assign = ( "" => "" )
    }
    
    =>
    
    alias.url += ( "/cgi-bin" => "/var/www/cgi-bin" )
    $HTTP["url"] =~ "^/cgi-bin/" {
     cgi.assign = ( ".py"  => "/usr/bin/python3",
                    ".cgi" => "/bin/sh",
     )
    }
    
  3. write a cgi script in

    sudo touch /var/www/cgi-bin/welcome.cgi
    sudo nano  /var/www/cgi-bin/welcome.cgi
    
    echo "Content-type: text/html"
    echo ""
    echo '<HTML>'
    echo '<BODY>'
    echo '<H1>This is my first CGI script</H1>'
    echo 'Hello, world!'
    echo '<h2>Environment Variables:</h2>'
    echo '<pre>'
    /usr/bin/env
    echo '</pre>'
    echo '</BODY>'
    echo '</HTML>'
    exit 0
    
  4. Add correct file permission; scripts must be executable:

    sudo chmod +x welcome.cgi
    sudo chown -R www-data
    sudo chgrp -R www-data
    
  5. restart webserver

    sudo service lighttpd force-reload
    
  6. write a python based cgi script and test it :

    sudo touch /var/www/cgi-bin/welcome.py
    sudo nano  /var/www/cgi-bin/welcome.py
    
    print "Content-Type: text/html"
    print ""
    print "<body>"
    print "TEST"
    print "</body>"
    
    sudo service lighttpd force-reload
    
  7. open web-browser and surf

    http://192.168.2.240/cgi-bin/welcome.cgi
    

    returns something like

    Environment Variables:
    GATEWAY_INTERFACE=CGI/1.1
    REMOTE_ADDR=192.168.2.42
    QUERY_STRING=
    DOCUMENT_ROOT=/var/www/cgi-bin
    REMOTE_PORT=53415
    HTTP_USER_AGENT=Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36
    HTTP_UPGRADE_INSECURE_REQUESTS=1
    HTTP_ACCEPT=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    CONTENT_LENGTH=0
    SCRIPT_FILENAME=/var/www/cgi-bin/welcome.cgi
    HTTP_HOST=192.168.2.240
    REQUEST_URI=/cgi-bin/welcome.cgi
    SERVER_SOFTWARE=lighttpd/1.4.45
    HTTP_CONNECTION=keep-alive
    HTTP_ACCEPT_LANGUAGE=en-US,en;q=0.9
    SERVER_PROTOCOL=HTTP/1.1
    HTTP_ACCEPT_ENCODING=gzip, deflate
    REDIRECT_STATUS=200
    REQUEST_METHOD=GET
    SERVER_ADDR=192.168.2.240
    PWD=/var/www/cgi-bin
    SERVER_PORT=80
    SCRIPT_NAME=/cgi-bin/welcome.cgi
    SERVER_NAME=192.168.2.240
    
  8. open web-browser and surf

    http://192.168.2.240/cgi-bin/welcome.py
    

    returns something like

    TEST
    

Troubleshooting

  • Check file permission (executable)
  • check linux line endings
  • check shebang line
  • check the empty line after Content-type
  • call script on target
  • double-check spaces and correct syntax in configuration

Enable Fast Common Gateway Interface (FastCGI)

The FastCGI Interface is defined by http://www.fastcgi.com/ and is a platform-independent and server-independent interface between a web-application and a webserver.

The web-server keeps the sub-process running. This removes time to fork/stop/restart.

Steps :

  1. Enable fast cgi module within web-server :

    demonstrator@orangepizero:/etc/lighttpd$ sudo lighty-enable-mod fastcgi
    Enabling fastcgi: ok
    Run "service lighttpd force-reload" to enable changes
    demonstrator@orangepizero:/etc/lighttpd$ service lighttpd force-reload
    ==== AUTHENTICATING FOR org.freedesktop.systemd1.manage-units ===
    Authentication is required to restart 'lighttpd.service'.
    Authenticating as: ,,, (demonstrator)
    Password:
    ==== AUTHENTICATION COMPLETE ===
    

This creates /etc/lighttpd/conf-enabled/10-fastcgi.conf

  1. Install python fast cgi server (flup)

    sudo apt install python3-pip
    sudo pip3 install flup-py3 -t /usr/local/lib/python3.6/dist-packages/
    

    Note: If target is not specified, then pip installs into $HOME

  2. put example from https://docs.python.org/3.1/howto/webservers.html

    sudo mkdir -p /var/www/fcgi-bin/
    sudo touch /var/www/fcgi-bin/welcome.fcgi
    sudo nano  /var/www/fcgi-bin/welcome.fcgi
    
    #!/usr/bin/env python3
    # -*- coding: UTF-8 -*-
    import html
    import flup.server.fcgi
    import os
    import sys
    def app (environ, start_response) :
        start_response ('200 OK', [('Content-Type', 'text/html')])
        yield '<h1>FastCGI Environment</h1>'
        yield '<table>'
        for k, v in sorted(environ.items()):
             yield '<tr><th>{0}</th><td>{1}</td></tr>'.format(
                 html.escape(str (k)), html.escape(str (v)))
        yield '</table>'
    #end def
    flup.server.fcgi.WSGIServer(app).run()
    
  3. edit /etc/lighttpd/conf-enabled/10-fastcgi.conf

    server.modules += ( "mod_fastcgi" )
    fastcgi.server = ( ".fcgi" =>
        (
            "python-fcgi" =>
            (
                "socket" => "/tmp/fastcgi.python.socket",
                "bin-path" => "/usr/bin/python3 /var/www/fcgi-bin/welcome.fcgi",
                "check-local" => "disable",
                "max-procs" => 1,
                "bin-copy-environment" => (
                    "PATH", "SHELL", "USER", "PYTHONPATH"
                ),
            )
        )
    )
    
  1. test in web-browser

    http://192.168.2.240/foo.fcgi
    

    returns something like

    CONTENT_LENGTH      0
    DOCUMENT_ROOT       /var/www/html
    GATEWAY_INTERFACE   CGI/1.1
    HTTP_ACCEPT text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
    HTTP_ACCEPT_ENCODING        gzip, deflate
    HTTP_ACCEPT_LANGUAGE        en-US,en;q=0.9
    HTTP_CONNECTION     keep-alive
    HTTP_HOST   192.168.2.240
    HTTP_UPGRADE_INSECURE_REQUESTS      1
    HTTP_USER_AGENT     Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.62 Safari/537.36
    PATH_INFO
    QUERY_STRING
    REDIRECT_STATUS     200
    REMOTE_ADDR 192.168.2.42
    REMOTE_PORT 58236
    REQUEST_METHOD      GET
    REQUEST_URI /foo.fcgi
    SCRIPT_FILENAME     /var/www/html/foo.fcgi
    SCRIPT_NAME /foo.fcgi
    SERVER_ADDR 192.168.2.240
    SERVER_NAME 192.168.2.240
    SERVER_PORT 80
    SERVER_PROTOCOL     HTTP/1.1
    SERVER_SOFTWARE     lighttpd/1.4.45
    wsgi.errors <flup.server.fcgi_base.OutputStream object at 0xb6ab43d0>
    wsgi.input  <flup.server.fcgi_base.InputStream object at 0xb6ab4390>
    wsgi.multiprocess   False
    wsgi.multithread    True
    wsgi.run_once       False
    wsgi.url_scheme     http
    wsgi.version        (1, 0)
    

Development

⚠️ **GitHub.com Fallback** ⚠️