Blog for Learning

| lesson material | material summary | questions and answers | definitions | types and examples | other information | materi pelajaran | ringkasan materi | pertanyaan dan jawaban | definisi | jenis-jenis dan contoh-contoh | informasi lainnya |

Powered by Blogger.


How to Use Web Server from PHP

In the PHP version 5.4.0CLI 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 .
Run PHP Server via Terminal
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.
Experiment with the PHP server
We will get a 404 error , because there are no files index.phpin 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] [404]: / - No such file or directory
[Sun Oct  9 14:49:15 2016] [404]: /favicon.ico - No such file or directory
[Sun Oct  9 14:49:15 2016] [404]: /favicon.ico - No such file or directory
To stop the server, press the Ctrlbutton 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.
  1. Create a new directory, then enter the directory
      mkdir webku
      cd webku
  2. Create a new file named index.php
      touch index.php
  3. Fill in the file with the following code
      echo "Hallo, ini webku. Selamat datang ya";
  4. Then run the server
      php -S localhost:8000
The following are the outputs we will get.
Output Server results with different root directories
We can also specify the location of the root document specifically with arugmen -tFor 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.
  1. 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":
      case "/home":
        echo "Ini halaman Home";
        echo "404: Halaman tidak ditemukan";
  2. Then make a file named profil.phpwith the contents as follows.
      echo "Ini halaman profil";
  3. Run the server
      php -S localhost:8000 router.php
The output we will get is like this picture.
Output Results
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

Template By Kunci Dunia
Back To Top