Creating barebone, a PHP-enabled web server in C?

I want to make the lightest HTTP server in C that supports PHP, and possibly FastCGI, if that makes a huge difference.

I am not sure how to implement PHP support. Does it just call PHP.exe from the path to the .php file and read the output? What about things like header () in PHP? How is the server handled?

And one more question: is it ideal to use separate threads for each request? I do not expect a very large load, but I'm not 100% sure about the design of this ...

I'm still pretty new to C and C ++ and this is a learning experience.

+6
c php webserver fastcgi
source share
4 answers

First, let me say that if the goal is a lightweight HTTP server serving PHP pages, this is already done. Take a look at nginx .

Regarding the learning experience, you have chosen something that is actually quite difficult.

Multithreading is difficult in the best of times. In C / C ++ (something with manual memory allocation really) this is an order of magnitude heavier.

Added to this is network connectivity. There are quirks to deal with, different versions of HTTP (mostly no problem), all kinds of HTTP headers to work with, etc.

The most intuitive solution to this problem is the process that listens on the port. When it receives a request, it starts a process that can be executed when executing a PHP process, if necessary.

This, however, does not scale. The first (obvious) optimization is to use threads instead of processes and some form of interthread communication. Although this helps, it will only scale so far.

Go beyond that and you are looking at handling an asynchronous socket, which is a pretty low level.

All these, however, are quite large projects.

Is there any specific reason you do this in C / C ++? Or any special reason why you are learning one or both of these languages? These languages โ€‹โ€‹certainly have their place, but they are increasingly becoming niche languages. Collected (garbage collector) languages โ€‹โ€‹/ platforms are almost completely captured. Joel claims that garbage collection is the only huge increase in programming productivity in the last 20 years, and I tend to agree.

+9
source share

To learn from experience with HTTP code written in C, you can also take a look at:

http://hping.org/wbox/

+2
source share

To create your own HTTP server, I recommend getting inspiration from other people's code. The ry programmer, known for his node.js framework, wrote simple, elegant code on this.

Look at his libebb library, it has a parser generated using Raegel using a simple but powerful PEG (it is based on the Song Shaw mongrel parcel). Also check out the usage example . This is really clean and useful code.

libebb is a lightweight HTTP server library for C. It lays the foundation for writing a web server by providing the socket juggling and request parsing. By implementing the HTTP/1.1 grammar provided in RFC2612, libebb understands most most valid HTTP/1.1 connections (persistent, pipelined, and chunked requests included) and rejects invalid or malicious requests. libebb supports SSL over HTTP. 

Regarding PHP-Server communication, the easiest way is CGI , but if you think that adventure dig into the php source code in SAPI (Server API) to see how to do it.

+2
source share

Similar to libebb, see http://www.gnu.org/software/libmicrohttpd/ . It also uses GnuTLS for additional SSL.

0
source share

All Articles