version=pmwiki-2.2.130 ordered=1 urlencoded=1 agent=Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.60 Safari/537.36 author=gtlsgamr charset=UTF-8 csum= ctime=1649171332 host=103.247.6.189 name=Openhttpd.CGI rev=8 targets= text=CGI (Common Gateway Interface). It is a way to communicate between browser and server. Simple scripts used to do simple tasks.%0a%0a%0a!! Docs and References%0aYou'll want to consult the [[https://man.openbsd.org/httpd|httpd]] and [[https://man.openbsd.org/httpd.conf|httpd.conf]] man pages to know about httpd config blocks.%0a!! Writing the script%0aWe will use C language to create the script.%0aCreate the script at your home directory @@/home/username/hello.c@@%0a%0a%0a[@%0a#include %3cstdio.h>%0a %0aint main(void)%0a{%0a puts("Status: 200 OK\r");%0a puts("Content-Type: text/html\r");%0a puts("\r");%0a puts("Hello, world!\n");%0a return 0;%0a}%0a@]%0a%0aYour script is ready, but you still need to compile it.%0a%0a[@%0a$ cd /home/username%0a$ cc hello.c -o hello.cgi%0a$ doas mv hello.cgi /var/www/htdocs/yoursite/cgi-bin/%0a@]%0a%0aMake the script executable%0a[@%0a$ doas chmod +x /var/www/htdocs/yoursite/cgi-bin/hello.cgi%0a@]%0a%0a!! Configuring httpd.conf%0aAdd the cgi block to your /etc/httpd.conf block for your website. It should look like this:%0a%0a[@%0aserver "example.com" {%0a listen on * port 80%0a root "/htdocs/yoursite"%0a connection max request body 104857600%0a location "/cgi-bin/*"{%0a root "/htdocs/yoursite/"%0a fastcgi%0a }%0a location "/.well-known/acme-challenge/*" {%0a root "/acme"%0a request strip 2%0a }%0a}%0a@]%0a%0a%0a'''NOTE''': You must replace example.com with your own domain%0a%0aNow any scripts that you add in [@ /var/www/htdocs/yoursite/cgi-bin @] directory will be accessible by [@ http://example.com/cgi-bin/scriptname @]%0a%0aMake sure you edit the block accordingly if you are using [[https://wiki.ircnow.org/index.php?n=Openhttpd.Tls |TLS]].%0a%0aenable the server to run the script, we will use [[https://man.openbsd.org/slowcgi.8| Slowcgi ]].%0a%0aEnable and start slowcgi%0a[@%0a$ doas rcctl enable slowcgi%0a$ doas rcctl start slowcgi%0a@] %0a%0aEnable and start httpd if you haven't already%0a[@%0a$ doas rcctl enable httpd%0a$ doas rcctl start httpd%0a@]%0a%0aGo to @@http://example.com/cgi-bin/hello.cgi@@ and you will see "Hello world" printed on your screen!%0a%0a time=1649175728 author:1649175728=gtlsgamr diff:1649175728:1649175673:=0a1,2%0a> '''NOTE: THIS GUIDE IS A WORK IN PROGRESS. DO NOT FOLLOW IT FOR NOW'''%0a> %0a host:1649175728=103.247.6.189 author:1649175673=gtlsgamr diff:1649175673:1649175649:minor=63c63%0a%3c Make sure you edit the block accordingly if you are using [[https://wiki.ircnow.org/index.php?n=Openhttpd.Tls |TLS]].%0a---%0a> Make sure you edit the block accordingly if you are using [[https://wiki.ircnow.org/index.php?n=Openhttpd.Tls | TLS]].%0a host:1649175673=103.247.6.189 author:1649175649=gtlsgamr diff:1649175649:1649175542:=63c63%0a%3c Make sure you edit the block accordingly if you are using [[https://wiki.ircnow.org/index.php?n=Openhttpd.Tls | TLS]].%0a---%0a> Make sure you edit the block accordingly if you are using TLS.%0a host:1649175649=103.247.6.189 author:1649175542=gtlsgamr diff:1649175542:1649171556:=10,12c10,12%0a%3c Create the script at your home directory @@/home/username/hello.c@@%0a%3c %0a%3c %0a---%0a> Create the script at @@/var/www/htdocs/yoursite/cgi-bin/hello.c@@%0a> %0a> %0a14c14%0a%3c #include %3cstdio.h>%0a---%0a> #include %3cstdio.h>%0a16,23c16,23%0a%3c int main(void)%0a%3c {%0a%3c puts("Status: 200 OK\r");%0a%3c puts("Content-Type: text/html\r");%0a%3c puts("\r");%0a%3c puts("Hello, world!\n");%0a%3c return 0;%0a%3c }%0a---%0a> int main(void)%0a> {%0a> puts("Status: 200 OK\r");%0a> puts("Content-Type: text/html\r");%0a> puts("\r");%0a> puts("Hello, world!\n");%0a> return 0;%0a> }%0a29,31c29,30%0a%3c $ cd /home/username%0a%3c $ cc hello.c -o hello.cgi%0a%3c $ doas mv hello.cgi /var/www/htdocs/yoursite/cgi-bin/%0a---%0a> $ cd /var/www/htdocs/yoursite/cgi-bin%0a> $ doas cc hello.c -o hello.cgi%0a36c35%0a%3c $ doas chmod +x /var/www/htdocs/yoursite/cgi-bin/hello.cgi%0a---%0a> $ doas chmod +x hello.cgi%0a host:1649175542=103.247.6.189 author:1649171556=gtlsgamr diff:1649171556:1649171525:minor=35c35%0a%3c $ doas chmod +x hello.cgi%0a---%0a> doas chmod +x hello.cgi%0a68,69c68,69%0a%3c $ doas rcctl enable slowcgi%0a%3c $ doas rcctl start slowcgi%0a---%0a> doas rcctl enable slowcgi%0a> doas rcctl start slowcgi%0a74,75c74,75%0a%3c $ doas rcctl enable httpd%0a%3c $ doas rcctl start httpd%0a---%0a> doas rcctl enable httpd%0a> doas rcctl start httpd%0a host:1649171556=103.247.6.189 author:1649171525=gtlsgamr diff:1649171525:1649171492:minor=29d28%0a%3c $ cd /var/www/htdocs/yoursite/cgi-bin%0a host:1649171525=103.247.6.189 author:1649171492=gtlsgamr diff:1649171492:1649171332:=14c14%0a%3c #include %3cstdio.h>%0a---%0a> #include %3cstdio.h> /* puts(3) */%0a host:1649171492=103.247.6.189 author:1649171332=gtlsgamr diff:1649171332:1649171332:=1,79d0%0a%3c '''NOTE: THIS GUIDE IS A WORK IN PROGRESS. DO NOT FOLLOW IT FOR NOW'''%0a%3c %0a%3c CGI (Common Gateway Interface). It is a way to communicate between browser and server. Simple scripts used to do simple tasks.%0a%3c %0a%3c %0a%3c !! Docs and References%0a%3c You'll want to consult the [[https://man.openbsd.org/httpd|httpd]] and [[https://man.openbsd.org/httpd.conf|httpd.conf]] man pages to know about httpd config blocks.%0a%3c !! Writing the script%0a%3c We will use C language to create the script.%0a%3c Create the script at @@/var/www/htdocs/yoursite/cgi-bin/hello.c@@%0a%3c %0a%3c %0a%3c [@%0a%3c #include %3cstdio.h> /* puts(3) */%0a%3c %0a%3c int main(void)%0a%3c {%0a%3c puts("Status: 200 OK\r");%0a%3c puts("Content-Type: text/html\r");%0a%3c puts("\r");%0a%3c puts("Hello, world!\n");%0a%3c return 0;%0a%3c }%0a%3c @]%0a%3c %0a%3c Your script is ready, but you still need to compile it.%0a%3c %0a%3c [@%0a%3c $ doas cc hello.c -o hello.cgi%0a%3c @]%0a%3c %0a%3c Make the script executable%0a%3c [@%0a%3c doas chmod +x hello.cgi%0a%3c @]%0a%3c %0a%3c !! Configuring httpd.conf%0a%3c Add the cgi block to your /etc/httpd.conf block for your website. It should look like this:%0a%3c %0a%3c [@%0a%3c server "example.com" {%0a%3c listen on * port 80%0a%3c root "/htdocs/yoursite"%0a%3c connection max request body 104857600%0a%3c location "/cgi-bin/*"{%0a%3c root "/htdocs/yoursite/"%0a%3c fastcgi%0a%3c }%0a%3c location "/.well-known/acme-challenge/*" {%0a%3c root "/acme"%0a%3c request strip 2%0a%3c }%0a%3c }%0a%3c @]%0a%3c %0a%3c %0a%3c '''NOTE''': You must replace example.com with your own domain%0a%3c %0a%3c Now any scripts that you add in [@ /var/www/htdocs/yoursite/cgi-bin @] directory will be accessible by [@ http://example.com/cgi-bin/scriptname @]%0a%3c %0a%3c Make sure you edit the block accordingly if you are using TLS.%0a%3c %0a%3c enable the server to run the script, we will use [[https://man.openbsd.org/slowcgi.8| Slowcgi ]].%0a%3c %0a%3c Enable and start slowcgi%0a%3c [@%0a%3c doas rcctl enable slowcgi%0a%3c doas rcctl start slowcgi%0a%3c @] %0a%3c %0a%3c Enable and start httpd if you haven't already%0a%3c [@%0a%3c doas rcctl enable httpd%0a%3c doas rcctl start httpd%0a%3c @]%0a%3c %0a%3c Go to @@http://example.com/cgi-bin/hello.cgi@@ and you will see "Hello world" printed on your screen!%0a%3c %0a%3c %0a\ No newline at end of file%0a host:1649171332=103.247.6.189