In the PHP version
5.4.0
, CLI SAPI provides a build-in web server feature that allows us to create web servers from the command line (CLI) command .
This server is very useful for development , because we can find errors through the console server.
This default PHP server is not recommended for use in the production environment (prodcution) .
Make a Server from Command Line
Make sure PHP is installed, then create a server with the following command.
php -S localhost:8000
Notice the argument
-S
, the argument uses capital letters. Then the second argument is the server address and port number that will be used. The portnumber is free, provided that it does not conflict with other service portnumbers .
The picture above shows, if the server is ready to receive a request from the http: // localhost: 8000 address with the document root
/home/petanikode
.
We will get a 404 error , because there are no files
index.php
in the root of the server document. The following is the log that will appear in the terminal.petanikode@imajinasi ~ $ php -S localhost:8000
PHP 7.0.8-0ubuntu0.16.04.3 Development Server started at Sun Oct 9 14:43:04 2016
Listening on http://localhost:8000
Document root is /home/petanikode
Press Ctrl-C to quit.
[Sun Oct 9 14:49:15 2016] 127.0.0.1:40244 [404]: / - No such file or directory
[Sun Oct 9 14:49:15 2016] 127.0.0.1:40246 [404]: /favicon.ico - No such file or directory
[Sun Oct 9 14:49:15 2016] 127.0.0.1:40248 [404]: /favicon.ico - No such file or directory
To stop the server, press the
Ctrl
+ button c
.Root Document
A root document is the location of a project or web document.
For example, in the example above, the root document is on
/home/petanikode/
.
Because I run the command in the home directory. Let's try using another document root.
- Create a new directory, then enter the directory
mkdir webku cd webku
- Create a new file named
index.php
touch index.php
- Fill in the file with the following code
<?php echo "Hallo, ini webku. Selamat datang ya"; ?>
- Then run the server
php -S localhost:8000
The following are the outputs we will get.
We can also specify the location of the root document specifically with arugmen
-t
. For example,php -S localhost:8000 -t /var/www/html/proyek-web/
Using the Router Script
The router functions to set direction and destination. For example, a request (request) with an URI,
/profil
then point or open the page profil.php
.- Make a new file in the previous directory with the name
router.php
, then fill in the code with the following.<?php // router.php switch ($_SERVER["REQUEST_URI"]){ case "/profil": include("profil.php"); break; case "/home": echo "Ini halaman Home"; break; default: echo "404: Halaman tidak ditemukan"; } ?>
- Then make a file named
profil.php
with the contents as follows.<?php echo "Ini halaman profil"; ?>
- Run the server
php -S localhost:8000 router.php
The output we will get is like this picture.
Well, that's how to use the default PHP web server. More complete, can be learned in the PHP documentation. So many posts this time, hopefully useful.
0 Komentar untuk "How to Use Web Server from PHP"
Silahkan berkomentar sesuai artikel