-
Notifications
You must be signed in to change notification settings - Fork 40
/
tutorial2.xml
125 lines (121 loc) · 5.06 KB
/
tutorial2.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<article data-sblg-article="1" data-sblg-tags="tutorial" itemscope="itemscope" itemtype="http://schema.org/BlogPosting">
<header>
<h2 itemprop="name">
FastCGI Deployments
</h2>
<address itemprop="author"><a href="https://kristaps.bsd.lv">Kristaps Dzonsons</a></address>
<time itemprop="datePublished" datetime="2015-08-05">5 August, 2015</time>
</header>
<p>
<aside itemprop="about">
FastCGI allows for much higher throughput by running web applications as daemons.
In this tutorial, I'll describe how to deploy a simple FastCGI application using <a href="kfcgi.8.html">kfcgi(8)</a>.
</aside>
</p>
<p>
The target system will be OpenBSD.
Our FastCGI script will be deployed using the socket interface, which is the default on OpenBSD and supported on other web
servers, such as Apache, as well.
In this method, the FastCGI application creates a socket that listens for connections.
The web server maps a web page to a socket, then connects to the socket and transmits requests.
</p>
<h3>
Source Code
</h3>
<p>
Let's start with the server itself.
This will be brutally simple: it will respond to all requests and simply print <q>Hello, World</q> and nothing more.
The difference between this and the usual CGI method is that all processing occurs in a loop instead of as the entire body of
the program.
</p>
<figure class="sample">
<pre class="prettyprint linenums">#include <sys/types.h> /* size_t, ssize_t */
#include <stdarg.h> /* va_list */
#include <stddef.h> /* NULL */
#include <stdint.h> /* int64_t */
#include <kcgi.h>
int
main(void)
{
struct kreq req;
struct kfcgi *fcgi;
if (khttp_fcgi_init
(&fcgi, NULL, 0, NULL, 0, 0) != KCGI_OK)
return 0;
while (khttp_fcgi_parse(fcgi, &req) == KCGI_OK) {
khttp_head(&req, kresps[KRESP_STATUS],
"%s", khttps[KHTTP_200]);
khttp_head(&req, kresps[KRESP_CONTENT_TYPE],
"%s", kmimetypes[KMIME_TEXT_PLAIN]);
khttp_body(&req);
khttp_puts(&req, "Hello, world!\n");
khttp_free(&req);
}
khttp_fcgi_free(fcgi);
return 0;
}</pre>
</figure>
<p>
This example doesn't do any real error checking or content validation, so it's pretty easy to understand.
Consult the <a href="khttp_fcgi_init.3.html">khttp_fcgi_init(3)</a>, <a href="khttp_fcgi_parse.3.html">khttp_fcgi_parse(3)</a>,
and <a href="khttp_fcgi_free.3.html">khttp_fcgi_free(3)</a> manpages to see how these work properly.
Next comes the more tricky part: deploying as a FastCGI application.
</p>
<h3>
Compile and Link
</h3>
<p>
Compiling and linking follow the same logic as in the <a href="tutorial0.html">Getting Started with CGI in C</a>.
Since we're going to install our application in a file-system jail, we'll statically compile it.
If your operating system doesn't support static linking, you won't be able to run your application with a file-system jail
unless you copy over all libraries into the jail.
(This is not covered by this tutorial.)
</p>
<figure class="sample">
<pre class="prettyprint lang-sh linenums">% cc `pkg-config --cflags kcgi` -c -o tutorial2.o tutorial2.c
% cc -static -o tutorial2 tutorial2.o `pkg-config --libs kcgi`</pre>
</figure>
<h3>
Install
</h3>
<p>
Our installation process will look a great deal like that of <a href="tutorial0.html">Getting Started with CGI in C</a> except
that we'll change our paths around a bit.
First, let's configure the web server.
I'll assume this is OpenBSD's <a href="https://man.openbsd.org/httpd.8">httpd(8)</a>.
The file-format documentation is <a href="https://man.openbsd.org/httpd.conf.5">httpd.conf(5)</a>.
</p>
<figure class="sample">
<pre class="prettyprint lang-sh linenums">server "me.local" {
listen on * port 80
location "/fcgi-bin/*" {
fastcgi socket "/run/httpd.sock"
root "/"
}
}</pre>
</figure>
<p>
This will cause CGI requests to <span class="file">/fcgi-bin</span> to be routed into the socket at <span
class="file">/run/httpd.sock</span>, which is relative to the server root <span class="file">/var/www</span>.
Let's first install our web application within the server root.
</p>
<figure class="sample">
<pre class="prettyprint lang-sh linenums">% doas mkdir -p /var/www/fcgi-bin
% doas install -m 0555 tutorial2 /var/www/fcgi-bin</pre>
</figure>
<p>
We must now set up the socket by executing our server.
We do this by using the <a href="kfcgi.8.html">kfcgi(8)</a> utility, which will securely create the FastCGI socket, enact the
file-system jail, then execute a pool of applications.
</p>
<figure class="sample">
<pre class="prettyprint lang-sh linenums">% doas kfcgi -U www -u www -- /fcgi-bin/tutorial2</pre>
</figure>
<p>
That's it!
At this point, the web application (running as user <q>www</q>) is executing under the <a href="kfcgi.8.html">kfcgi(8)</a>
daemon within the file-system jail at <span class="file">/var/www</span>.
It has created the socket in <span class="file">/var/www/run/httpd.sock</span> as user <q>www</q>.
The web server will now be able to connect to this socket for its requests.
</p>
</article>