To develop / deploy websites in 3 stages, i.e. ( 1 ) locally, ( 2 ) with an access controlled dedicated beta / test website in the www and ( 3 ) the production site, you can have ( 1 ) & ( 2 ) using the same domain name - port free - as follows:
- look up the IP address of the beta.web.site
and edit the hosts file to reflect:
- <IP> beta.web.site
- 127.0.0.1 beta.web.site
Start the built-in web server to work locally with:
- sudo php -S beta.web.site:80
and just hit http://beta.web.site as usual. Switching back and forth between ( 1 ) and ( 2 ) is as easy as telling the php engine not to fake a server any more :) Nice!
Happy PHP'ing.
Serveur web interne
Depuis PHP 5.4.0, le CLI SAPI fournit un serveur web interne.
Ce serveur web est prévu uniquement dans un but de développement, et ne doit pas être utilisé en production.
Les requêtes URI sont servies depuis le dossier de travail courant où PHP a été démarré, tant que l'option -t est utilisé pour spécifier explicitement un document racine.
Si une requête URI ne spécifie pas un fichier, alors soit index.php, soit index.html du dossier courant sera retourné. Si aucun de ces fichiers n'existe, alors un code réponse 404 est retourné.
Si un fichier PHP est fourni dans la ligne de commande lorsque le serveur web
est démarré, il sera traité comme un script "routeur" pour le serveur web.
Le script sera exécuté au début de chaque requête HTTP. Si ce script retourne
FALSE, alors la ressource demandée est retournée telle quelle.
Sinon, la sortie du script est retournée au navigateur.
Exemple #1 Démarrage du serveur web
$ cd ~/public_html $ php -S localhost:8000
Le terminal affichera :
PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011 Listening on localhost:8000 Document root is /home/me/public_html Press Ctrl-C to quit
Après des requêtes URI sur http://localhost:8000/ et http://localhost:8000/myscript.html, le terminal affichera quelques choses comme :
PHP 5.4.0 Development Server started at Thu Jul 21 10:43:28 2011 Listening on localhost:8000 Document root is /home/me/public_html Press Ctrl-C to quit. [Thu Jul 21 10:48:48 2011] ::1:39144 GET /favicon.ico - Request read [Thu Jul 21 10:48:50 2011] ::1:39146 GET / - Request read [Thu Jul 21 10:48:50 2011] ::1:39147 GET /favicon.ico - Request read [Thu Jul 21 10:48:52 2011] ::1:39148 GET /myscript.html - Request read [Thu Jul 21 10:48:52 2011] ::1:39149 GET /favicon.ico - Request read
Exemple #2 Démarrage avec un dossier racine spécifique
$ cd ~/public_html $ php -S localhost:8000 -t foo/
Le terminal affichera :
PHP 5.4.0 Development Server started at Thu Jul 21 10:50:26 2011 Listening on localhost:8000 Document root is /home/me/public_html/foo Press Ctrl-C to quit
Exemple #3 Utilisation d'un script routeur
Le fait de demander des images les affichera, mais les requêtes pour les fichiers HTML afficheront "Bienvenue chez PHP !"
<?php
// router.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"]))
return false; // retourne la requête telle quelle.
else {
echo "<p>Bienvenue chez PHP !</p>";
}
?>
$ php -S localhost:8000 router.php
Après plusieurs requêtes URI, le terminal affichera quelques choses comme :
PHP 5.4.0 Development Server started at Thu Jul 21 10:53:19 2011 Listening on localhost:8000 Document root is /home/me/public_html Press Ctrl-C to quit. [Thu Jul 21 10:53:45 2011] ::1:55801 GET /mylogo.jpg - Request read [Thu Jul 21 10:53:52 2011] ::1:55803 GET /abc.html - Request read [Thu Jul 21 10:53:52 2011] ::1:55804 GET /favicon.ico - Request read
