Web App With C & FastCGI - Quick Start

Minimal app and Nginx configuration.

Updated December 7, 2022

Making Money with Django and AI: How to Build SaaS Services Using Python

Learn how to build AI-driven websites with the Django web framework and monetize your services.
Read More
You will receive the book in PDF and ePub formats.

Table of contents

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:


Leave a comment

You can use Markdown to format your comment.