Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
3.40/5 (2 votes)
See more:
Made a C++ webserver that I using for at work. Nothing much, just a socket listening and serving... But I want to extend it to run PHP and MySQL. Any pointers will be appreciated.
Posted

That's a tall order but here's an outline of how I would think if I had a similar task.

In regards to all things Web Server related it is generally considered a good thing to make like an Apache. Now I'm not going to go and read all the source code for Apache Web Server and tell you how it works ( you can do that if you've got the time and tenacity ) so I'm going to tell you how I think it could work.

A Web Server has essentially 2 tasks which most people get the wrong way round. Firstly sending out Web Pages to temporarily connected clients and Secondly routing incoming requests to something that generates Web Pages.

If you think of it like that then a Web Server is quite simple and shouldn't grow into a bloated and unstable monstrocity.
I guess you've already met requirement 1 and can send out pages. Now you need to meet requirement 2 which is to route requests for something other than a simple static page to something other than the Web Server itself to generate the page.

My understanding is that this is best done by breaking down the request as follows.

Which port did the request arrive on? A general port on which all sorts of junk comes in or a specialist port assigned to FTP for example. If a routing decision can be made on the basis of the port then the request can be sent off to the FTP handler for example and the Web Server's job is done until a reponse is ready to be sent.

Which protocol did the request arrive as? Could be FTP, HTTP or something wrapped inside HTTP like PHP. Again route if it can be identified as something with an available handler.

At the main Web Server level that's pretty much it. If no specific protocol is identified then it's junked in the direction of the error log and a nicely formatted 404 message is lined up to be sent to the client.

So you need a plugin or handler of some kind for PHP that only recieves the PHP parts of PHP requests. That's pure PHP from there on and I don't PHP so I'll leave that to you.

MySQL is a little more interesting because it doesn't have it's own HTTP packaged protocol as far as I know. So like everyone else you're going to need to make one up or borrow someone else's. Usually borrowing someone else's is easier at least to start with and I guess there are lots of MySQL specific articles and examples about to work from. The fun part in the case of MySQL will be taking the requests and turning them back into Web Pages and parts of web Pages, the rest is mostly plumbing.
 
Share this answer
 
Did a little more research on the web and found out that there are 3 ways the PHP part can be achieved
1. Communicating with php-cgi.exe as a CGI (while setting some global variables)
2. Using fastcgi (similar to No. 1 and faster, only that I don't have a clue what this is)
3. Building a PHP module into your webserver code (hardest to implement but fastest in terms of performers)

My question now is, how do I get the output from php-cgi.exe
 
Share this answer
 
Comments
markkuk 23-Mar-13 10:10am    
Create a pipe and connect it to the standard output of php-cgi.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900