Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making a barebones, PHP-enabled web server in C?

I want to make the most lightweight possible HTTP server in C that supports PHP and possibly FastCGI if it will make a huge difference.

I'm not sure how to implement PHP support. Does it just call PHP.exe with the path to a .php file and read the output? What about things like header () in PHP? How are those handled by the server?

And another question, is it ideal to use separate threads for each request? I don't expect a very heavy load, but I'm not 100% sure on the design aspect of this...

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

like image 950
guitar- Avatar asked Jun 19 '10 08:06

guitar-


People also ask

Do I need a web server to run PHP?

You can make a PHP script to run it without any server or browser. You only need the PHP parser to use it this way. This type of usage is ideal for scripts regularly executed using cron (on *nix or Linux) or Task Scheduler (on Windows). These scripts can also be used for simple text processing tasks.

How can I practice PHP without server?

For windows system you should be able to run php by following below steps: Download php version you want to use and put it in c:\php. append ;c:\php to your system path using cmd or gui. call $ php -S localhost:8000 command in a folder which you want to serve the pages from.

Is PHP a web server?

Last but not least is PHP. PHP is a server scripting language, developed to make dynamic web pages.

Why does PHP need Apache?

PHP isn't a web server, it's a scripting language. Since you do need a web server, as you say, Apache is rather necessary, yes. (Caveat, asterisk: PHP ships with a development web server, and you can write a web server in PHP, but both are bad ideas in production.)


2 Answers

Firstly let me say that if the goal is a lightweight HTTP server that serves PHP pages, this has already been done. Have a look at nginx.

As for a learning experience, you've chosen something that's actually fairly tough.

Multithreaded is hard at the best of times. On C/C++ (anything with manual memory allocation really) it's an order of magnitude harder.

Added to this is network communication. There are quirks to deal with, different versions of HTTP (mostly a non-issue now), all sorts of HTTP headers to deal with and so on.

The most intuitive solution to this problem is to have a process that listens to a port. When it receives a request, it spawns a process, which may exec to a PHP process if required.

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

Go beyond that and you're looking at async socket handling, which is fairly low level.

All of these however are fairly big projects.

Is there any particular reason you're doing this in C/C++? Or any particular reason you're learning one or both of those languages? These languages certainly have their place but they're increasingly becoming niche languages. Managed (garbage collected) languages/platforms have almost completely taken over. Joel argues that garbage collection is about the only huge productivity increase in programming in about the last 20 years and I tend to agree.

like image 189
cletus Avatar answered Sep 30 '22 05:09

cletus


For a learning experience regarding HTTP code written in C you may also take a look at:

http://hping.org/wbox/

like image 28
chad Avatar answered Sep 30 '22 06:09

chad