CGI

CGI (Common Gateway Interface). It is a way to communicate between browser and server. Simple scripts used to do simple tasks.

Docs and References

You'll want to consult the httpd and httpd.conf man pages to know about httpd config blocks.

Writing the script

We will use C language to create the script. Create the script at your home directory /home/username/hello.c

#include <stdio.h>

int main(void)
{
   puts("Status: 200 OK\r");
   puts("Content-Type: text/html\r");
   puts("\r");
   puts("Hello, world!\n");
   return 0;
}

Your script is ready, but you still need to compile it.

$ cd /home/username
$ cc hello.c -o hello.cgi
$ doas mv hello.cgi /var/www/htdocs/yoursite/cgi-bin/

Make the script executable

$ doas chmod +x /var/www/htdocs/yoursite/cgi-bin/hello.cgi

Configuring httpd.conf

Add the cgi block to your /etc/httpd.conf block for your website. It should look like this:

server "example.com" {
        listen on * port 80
        root "/htdocs/yoursite"
        connection max request body 104857600
        location "/cgi-bin/*"{
                root "/htdocs/yoursite/"
                fastcgi
        }
        location "/.well-known/acme-challenge/*" {
                root "/acme"
                request strip 2
        }
}

NOTE: You must replace example.com with your own domain

Now any scripts that you add in /var/www/htdocs/yoursite/cgi-bin directory will be accessible by http://example.com/cgi-bin/scriptname

Make sure you edit the block accordingly if you are using TLS.

enable the server to run the script, we will use Slowcgi.

Enable and start slowcgi

$ doas rcctl enable slowcgi
$ doas rcctl start slowcgi

Enable and start httpd if you haven't already

$ doas rcctl enable httpd
$ doas rcctl start httpd

Go to http://example.com/cgi-bin/hello.cgi and you will see "Hello world" printed on your screen!