First of all, I know to create web servers which deal with static html pages.
I want to create a web server which supports PHP pages.
I have coded a server like below,
-> main()
function just create a socket, accept a request and calls connection(fd) function.
int main(int argc, char *argv[]) {
int sockfd, newsockfd, portno, pid;
socklen_t clilen;
struct sockaddr_in serv_addr, cli_addr;
if (argc < 2) {
fprintf(stderr, "ERROR, no port provided
");
exit(1);
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
error("ERROR opening socket");
bzero((char *) &serv_addr, sizeof(serv_addr));
portno = atoi(argv[1]);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(portno);
if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
error("ERROR on binding");
listen(sockfd, 5);
clilen = sizeof(cli_addr);
/*
Server runs forever, forking off a separate
process for each connection.
*/
while (1) {
newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
if (newsockfd < 0)
error("ERROR on accept");
pid = fork();
if (pid < 0)
error("ERROR on fork");
if (pid == 0) {
close(sockfd);
connection(newsockfd);
exit(0);
} else
close(newsockfd);
} /* end of while */
close(sockfd);
return 0; /* we never get here */
}
-> connection()
function sets document root, and explicitly sets a PHP page(echo.php) and calls php-cgi() function. php-cgi() does the further process.
Actually i dont know what php5-cgi does. When i run the server and request a page from web browser, it shows "Am a PHP file" for 1 second, and hides, displays the message,
The connection was reset
1) What is the proper way to handle php pages in web server
2) Can anyone suggest me some books or anything to develop a web server in C which handle PHP pages?
3) Do i wanna try any other language? which is the most preferable language to create web server and proxies?
4) how to deal with query strings and POST method requests?
when i did telnet to the server it works,
GET /echo.php HTTP/1.1
HTTP/1.1 200 OK
X-Powered-By: PHP/5.5.9-1ubuntu4.6
Content-type: text/html
<html>
<body>
Am a PHP file
</body>
</html>