Updated Dec 7, 2022

Web App With C & FastCGI - Quick Start

Minimal app and Nginx configuration.

Quick Start

Install libfcgi-dev and nginx:

sudo apt-get update
sudo apt-get install libfcgi-dev nginx

Create main.c and add these lines to it:

#include <fcgiapp.h>
#include <stdio.h>

int main() {

	int Socket = FCGX_OpenSocket("", 1024);
	FCGX_Request Request;
	FCGX_InitRequest(&Request, Socket, 0);

	while(FCGX_Accept_r(&Request) >= 0) {
		printf("Request Accepted\n");
			"Content-type: text/html\r\n\r\n"
	return 0;

Replace /etc/nginx/sites-enabled/default contents with these lines:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    server_name _;

    location / {
        include fastcgi_params;

Run these commands:

sudo service nginx restart
cc main.c -o app.fcgi -lfcgi

Open a new terminal and make a request to port 80:

curl localhost

You should see the response body:

Full project in GitHub