PHP Web Development Without Web Server

Web Development Using PHP’s Built-in Web Server

Web developers can utilize the PHP’s built-in web server. The web server is intended for development purposes only. PHP’s built-in web server requires a minimum of version 5.4.

The CLI SAPI provided the built-in web server which is started on the command line. URI requests are served from the current working folder where PHP command was executed.

This tutorial uses PHP as the HTTP Server.

    Tools are required:

  • Text editor.
  • Folder for web server.
  • PHP with CLI SAPI.
  • Browser to view output.

Optional Download and install Geany

Geany is required in order to follow this tutorial. For more information about Geany read Ojambo.com Lightweight Programming Editors.

Create PHP Web Server Folder

mkdir php_dev
cd php_dev

The folder will be used as the root for the web server. In this tutorial, a linux terminal command will be used to create a folder. The command is “mkdir php_dev”. In order to enter the created folder, the “cd” command is used.

Command Line Start PHP Web Server

php -S localhost:8000

In this tutorial, the document root of the web server will be inside the folder “php_dev”. However if a specific folder (such as foo) was required inside “php_dev” then the command could be “php -S localhost:8000 -t foo/”. The web server is now available in your browser at http://localhost:8000.

The web server is only available on the local machine using port 8000. To allow remote access, “php -S 0.0.0.0:8000” can be used. Another option is to use the IP address of the local machine.

Index.php File

<?php
/*
 * index.php
 * 
 * Copyright 2013 edward <http://ojambo.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>Ojambo.com PHP Web Server</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<body>
	<h1>Ojambo.com PHP Web Server Tutorial</h1>
	<h2><?php echo "PHP Web Server works"; ?></h2>
</body>

</html>

The PHP tags are wrapped in “H2” HTML tags. The PHP will only execute on a web server.

How to Use:

    Open Terminal

  • Create web development folder.
  • Change to web development folder.
  • Execute PHP web server command.

    Open Browser

  • Enter path (might be IP) of web development folder.
  • Check the output of PHP code.

Demonstration:

Ojambo.com PHP Web Development Without Web Server Tutorial

Image Missing
Ojambo.com PHP Start Web Server

Image Missing
Ojambo.com Web Development Without Web Server

Conclusion:

The CLI SAPI allows PHP to have a built-in web server. The server can be used for web development without installing a separate web server. Web developers can use a specific PHP option to start and stop the web server.

PHP command to start the we server must be started from the command line. If PHP is not started in the intended root, a specific folder can be added to the command line. The PHP command can also be tweaked to allow for remote access to the development web server.

    Recommendations:

  1. Place your PHP code in a specific folder.
  2. Start the PHP web development command from the folder where your PHP code resides.