Automating Long-Running PHP Application Restarts with Nodemon

Developing long-running PHP applications like HTTP servers requires manual restarts after each code change, interrupting the development workflow. The Node.js tool nodemon can be configured to monitor PHP files and automatically restart these services, significantly improving developer productivity.

Consider a basic HTTP server built with ReactPHP. The server runs continuously, handling incoming requests.

<?php

use React\Http\Server;
use React\Http\Response;
use React\EventLoop\Factory;
use Psr\Http\Message\ServerRequestInterface;

// Initialize the event loop
$eventLoop = Factory::create();

// Configure the server response handler
$httpServer = new Server(function (ServerRequestInterface $incomingRequest) {
    return new Response(
        200,
        ['Content-Type' => 'text/plain'],
        "Service is running"
    );
});

// Bind the server to a network socket
$networkSocket = new \React\Socket\Server('127.0.0.1:8080', $eventLoop);
$httpServer->listen($networkSocket);

echo 'Server active at ' . str_replace('tcp:', 'http:', $networkSocket->getAddress()) . PHP_EOL;

// Begin the event loop to handle requests
$eventLoop->run();

Traditionally, any modification to this script necessitates stopping the process and restarting it with php server.php. To automate this, install nodemon globally via npm:

npm install -g nodemon

Create a configuration file named nodemon.json to tailor nodemon for PHP development:

{
    "verbose": false,
    "ignore": [
        ".git",
        ".idea",
        "vendor/*"
    ],
    "execMap": {
        "php": "php"
    },
    "restartable": "rs",
    "ext": "php"
}

This configuration disables verbose output, ignores common directories, maps the .php extension to the php executable, sets a restart command, and specifies that only .php file extensions should be monitored.

To launch the server with automatic restart capability, execute:

nodemon server.php

The terminal will display startup messages, confirming the server is running and nodemon is actively watching for file changes.

When you edit the source code, for insatnce, changing the response format, nodemon detects the file modification and restarts the application proccess automatically.

// Updated server response handler
$httpServer = new Server(function (ServerRequestInterface $incomingRequest) {
    return new Response(
        200,
        ['Content-Type' => 'application/json'],
        json_encode(["status" => "success", "data" => "Updated response"])
    );
});

Upon saving the file, the console will show a restart log, and the updated server begins runing immediately without manual intervention. This process works recursively for all PHP files within the project directory, providing seamless live reload functionality for frameworks like Webman or Hyperf that operate as persistent services.

Tags: PHP Nodemon automation Development Tools ReactPHP

Posted on Wed, 22 Jul 2026 17:10:17 +0000 by shams