I want to run a (compiled) C program every time Nginx receives a request for a URL, say https://myserver.com/programs/my-C-file.

More specifically, when a PR in GitHub is merged, I want a hook to initiate a custom build and release process written in C (I want to use C because the build process is complex. There are many steps and and it's easier to parse stdout in C than Bash or PHP.)

But is there any reason I should use FastCGI to trigger the C binary directly instead of a PHP file and exec('my C program')?

I'm obviously not getting more than a few of these every few days so I'm not worried about scaling up, and it seems like FastCGI is a lot of extra configuration whereas PHP I can use right out of the box. Just create a PHP file run exec() and create a block in Nginx.

Or maybe I'm thinking about this whole process wrong and there's an easier way to do this.


Yes, it is possible to use nginx and FastCGI to run your C binary. Here's a rough overview of the necessary steps:

  • Install nginx
  • Install spawn-fcgi
  • Install fastcgi database (optional)
  • Create nginx vhost config with a server that listens on your IP/domain and a location that sends a FCGI upstream to localhost/port.
  • Compile your C program (your program should include while (FCGI_Accept() >= 0) { })
  • Start spawn-fcgi process to listen on localhost/port as wrapper for your program.

More to read:

Running a C FastCGI script using NGINX

[introduction article] Nginx + FastCGI program (C/C++) Demo and deployed to buil

  • Yes, but my question is not, "is it possible" but is it better? Why or why not? All those steps seem like a lot of extra work when I can seemingly do the same via a PHP file and exec() – OrdinaryHuman Feb 22 '18 at 18:10
  • Sorry. I misunderstood your question. Of course you can also use PHP and exec(). The downside of this is that by using PHP you add one more layer of complexity. For example: I think this way your C programm will be executed under the PHP user. You will have PHP child processes using some RAM. You will need to take care of PHP security updates from time to time. But I now understand that you want to know if there is a reason NOT to use PHP, as this would be much easier for you to use PHP. But that might be no real problems for your simple build (with no high traffic). – Bob Feb 22 '18 at 18:28

Two possible solutions:

  1. Use Python's CGIHTTPServer. No configuration, just put your compiled program in a cgi-bin directory and start python -m CGIHTTPServer in the parent directory. You can call your program on path /cgi-bin/yourprogramname. This is already a working HTTP server, if you still want Nginx in front of it configure it for a simple reverse proxy (proxy_pass should be enough).

  2. Use Apache's native CGI support. I'm going to refer you to Apache's documentation for configuration, directory structure is equivalent to the first option.

You could also reduce the amount of moving pieces by not using C but a language like Go because you can write an HTTP server in Go in like 10 lines which can call a function to execute the actual build process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.