Run PHP Web Applications Without External Server

Introduction

This tutorial will show you how to run PHP web applications without external server. So, you don’t need to use external server when you are developing or building your PHP based web applications in your development or local environment. I will show you here both how to run standalone PHP file and PHP web applications without using any external server, such as Apache HTTP Server.

I am assuming it is already known to you that you can run your PHP file using CLI (command line interface). So basically, what you do to run your PHP file is execute the php.exe followed by your php file: php.exe <php file name>. Make sure you have already set environment variable path for your PHP executable (exe) otherwise you must give full path of PHP exe file followed by your PHP file name.

When you want to run your PHP web applications without using any external server then you need to start your development server using the command php -S localhost:8000. So, your development server will listen to port 8000 at localhost. This command needs to be executed from the root folder of your PHP web application.

run php application without server

Remember you will get this facility of starting development server for your PHP web applications from PHP version 5.4 onward.

Prerequisites

PHP 7.4.8

Project Directory

It’s assumed that you have setup PHP and MySQL in your system.

Now we will create a project root directory called php-run-without-external-server anywhere in your system. I may not mention the project root directory in subsequent sections and I will assume that I am talking with respect to the project’s root directory.

MySQL Table

Create a table user in MySQL server under roytuts database.

CREATE TABLE `user` (
  `id` int NOT NULL AUTO_INCREMENT,
  `username` varchar(45) DEFAULT NULL,
  `password` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

Insert sample data to test the application right away:

insert into user(`username`, `password`) value(‘roy’, ‘roy’);

Database Configuration

Create db.php file for various database operations, such as, inserting data into MySQL database, selecting data from MySQL database, etc. Please do not forget to change the database credentials as per your database.

<?php
/**
* Author : https://roytuts.com
*/
	
$dbConn = mysqli_connect('localhost', 'root', 'root', 'roytuts') or die('MySQL connect failed. ' . mysqli_connect_error());

function dbQuery($sql) {
	global $dbConn;
	$result = mysqli_query($dbConn, $sql) or die(mysqli_error($dbConn));
	return $result;
}

function dbFetchAssoc($result) {
	return mysqli_fetch_assoc($result);
}

function dbNumRows($result) {
    return mysqli_num_rows($result);
}

function closeConn() {
	global $dbConn;
	mysqli_close($dbConn);
}

Displaying User Data

The following users.php file is created to fetch the data from MySQL database and display on the front-end.

<?php

/**
* Author : https://roytuts.com
*/

require_once 'db.php';


$sql = "SELECT * FROM user";
$results = dbQuery($sql);

$rows = array();

while($row = dbFetchAssoc($results)) {
	$rows[] = $row;
}

closeConn();

echo json_encode($rows);

Testing PHP Standalone Application

To run your PHP file using CLI, use the following command. Note php-run-without-external-server is the project root directory.

Command

\php-run-without-external-server>php users.php

Output

[{"id":"1","username":"roy","password":"roy"}]

So you have got the desired result from the server.

Start Development Server

Now to start the development server, navigate to the root folder of your project from the command line tool and execute the command: php -S localhost:8000.

Your development server will start and listen to port 8000.

Command and Output

\php-run-without-external-server>php -S localhost:8000
[Fri 14:01:08 ] PHP 7.4.8 Development Server (http://localhost:8000) started

Testing PHP Web Application

You can browse the URL in the browser to see the results: http://localhost:8000/users.php

run php web applications without external server

Notice in the above URL, I have not used the project’s root folder name because I have started the development server in the project’s root folder.

In PHP based framework CodeIgniter 4, you can easily start your development server for building the web application without needing any external server using the command php spark serve.

Source Code

Download

Leave a Reply

Your email address will not be published.