Using the built-in PHP web server
Aside from unit testing and running PHP directly from the command line, the obvious way to test your applications is to use a web server. For long-term projects, it would be beneficial to develop a virtual host definition for a web server that most closely mirrors the one used by your customer. Creating such definitions for the various web servers (that is, Apache, NGINX, and so on) is beyond the scope of this book. Another quick and easy-to-use alternative (which we have room to discuss here) is to use the built-in PHP 7 web server.
How to do it...
- To activate the PHP web server, first change to the directory that will serve as the base for your code.
- You then need to supply the hostname or IP address and, optionally, a port. Here is an example you can use to run the recipes supplied with this book:
cd /path/to/recipes php -S localhost:8080
You will see output on your screen that looks something like this:
- As the built-in web server continues to service requests, you will also see access information, HTTP status codes, and request information.
- If you need to set the web server document root to a directory other than the current one, you can use the
-t
flag. The flag must then be followed by a valid directory path. The built-in web server will treat this directory as if it were the web document root, which is useful for security reasons. For security reasons, some frameworks, such as Zend Framework, require that the web document root is different from where your actual source code resides.Here is an example using the
-t
flag:php -S localhost:8080 -t source/chapter01
Here is an example of the output: