I just tried this code:
<?php
set_time_limit(0);
$address = '176.9.117.136';
$port = 9000;
$sock = socket_create(AF_INET, SOCK_STREAM, 0);
socket_bind($sock, $address, $port) or die('Could not bind to address');
while(1)
{
socket_listen($sock);
$client = socket_accept($sock);
$input = socket_read($client, 1024);
echo $input;
$output = 'URL: http://ip-of-my-server:9000/
HTTP/1.1 200 OK
Date: Tue, 10 Jul 2012 16:58:23 GMT
Server: TestServer/1.0.0 (PHPServ)
Last-Modified: Fri, 06 Jul 2012 14:29:58 GMT
ETag: "13c008e-1b9-4c42a193de580"
Accept-Ranges: bytes
Content-Length: 441
Vary: Accept-Encoding
Content-Type: text/html
';
socket_write($client, $output);
socket_close($client);
}
socket_close($sock);
?>
But there is a problem. Instead of using the content of $output as the headers, Apache returns its own headers...
I don't know why because I execute the script via this command: php webserv.php
.
However it practically works, because when I load the page http://ip-of-my-server:9000/
from my browser, it shows me the headers sent by the client on the server, and returns the content of $output
to the client (my browser).
I want to create my own webserver only in PHP if it's possible. I just want to know how to run it without Apache, so I can manage my own HTTP headers.
Is there a reason to implement an HTTP server in PHP (of all things)? There's no threading, etc. It would be a pain… (unless this is some sort of academic thing…)
PHP 5.4 ships with a built-in webserver. Maybe that is what you're looking for…
Update:
While I understand your motivation to learn this sort of stuff, I believe you're on the wrong track trying this sort of stuff with PHP. PHP is not really designed for long running processes (like a server would be), it is not equipped for parallel processing (threads). Even multi-processing would require PCNTL, specifically pcntl_fork() and limit your educational walkabout to a Unix based system (which may not be a problem, though).
If your goal is to understand how servers deal with concurrency, I suggest playing with a language designed for that (Erlang, Go, Scala, …). Or play with a language that at least sort of emulates parallel processing (Python, Ruby, … [sort of, because of their GILs]).
If your goal is to understand HTTP (and let me tell you HTTP is a beast, if you're going past HTTP/1.0 and want to do it right), fiddling with PHP may be fine, if it's the only language you're firm in. If so, have a look at the example code (chat server) in this (sadly German) article on Socket Servers in PHP to get the basic socket stuff running an concentrate on the actual HTTP.
Update 2:
To answer your question regarding the headers…
I have no clue how apache would fit into the scenario described in your question. But I see you're using line-breaks to delimit headers and a double line-break to delimit headers from body. Unless you've saved your php file using \r\n
as the default line-break (windows-style), you're header-part is malformed and would thus be recognized as the body. Depending on the http client (user agent, may it be your browser, or curl, or whatever) this may be treated with "insert some default headers". Replace your line-breaks with \r\n
and try again.
If your server is reachable from the internet, try some header test tools to verify your HTTP is sound. If it is localhost-only, see what curl -I http://ip-of-my-server:9000
spits out.