We support #BlackLivesMatters !
If you support us then follow us by clicking here and following us!

Do you know PHP has its own built in server? Know how to run it.

For all PHP related discussion, one of the most popular programming languages on the web.
Post Reply
admin
Site Admin
Posts: 44

Do you know PHP has its own built in server? Know how to run it.

Post by admin » Sun Nov 03, 2019 5:18 pm

PHP 5.4+ comes with a built-in development server. It can be used to run applications without having to install a
production HTTP server such as nginx or Apache. The built-in server is only designed to be used for development
and testing purposes. It can be started by using the -S flag:

Code: Select all

php -S <host/ip>:<port>

Example usage

1. Create an index.php file containing:

Code: Select all

<?php
echo "Hello World from built-in PHP server";
2. Run the command php -S localhost:8080 from the command line. Do not include
http://
. This will start a web server listening on port 8080 using the current directory that you are in as the
document root.
3. Open the browser and navigate to http://localhost:8080. You should see your "Hello World" page.

Configuration
To override the default document root (i.e. the current directory), use the -t flag:

Code: Select all

php -S <host/ip>:<port> -t <directory>
E.g. if you have a public/ directory in your project you can serve your project from that directory using php -S
localhost:8080 -t public/
.

Logs
Every time a request is made from the development server, a log entry like the one below is written to the
command line.
[Mon Aug 18 18:20:19 2019] ::1:52455 [200]: /

Post Reply