Click here to Skip to main content
13,044,584 members (59,839 online)
Rate this:
Please Sign up or sign in to vote.
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 22-Mar-13 5:44am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

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
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 23 Mar 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100