Skip to main content

Managing and deploying PHP workers

A "PHP worker" in this context is a PHP application that is not served by a webserver like Apache. Instead, it is a standalone PHP application that runs in the background and either

  1. performs tasks like processing messages from a queue, or handling background tasks.
  2. ships its own webserver to serve HTTP requests.

Starting a PHP worker

To start a PHP worker from the mStudio, follow these steps:

  1. Navigate to the project that you want to create the application in.
  2. Select the "Apps" menu item in the sidebar.
  3. Click the "Create App" button and select "PHP-Worker".
  4. In the installation wizard, select the entrypoint command that should be used to start your application and complete the wizard.

After the installation has completed, observe the installation directory in the UI.

Deploying your app

After you have created your PHP worker, you can deploy your code by moving it into the designated application directory. You may use any method that you prefer to deploy your code, such as a local Git clone, rsync or SFTP.

Have a look at our collection of deployment guides for more information on how to deploy your code.

Making your app accessible via HTTP

See our guide on Running Webservers to learn how to make your PHP worker accessible via HTTP.

Managing your process lifecycle

See our guide on Running Webservers to learn how to manage the process lifecycle of your PHP worker.

Example applications

ReactPHP

The following code snippets show a minimal example of a ReactPHP application that listens on the port specified in the PORT environment variable.

Start by creating a PHP worker app, for example using the CLI:

Local shell session
$ mw app create php-worker --wait --entrypoint "php server.php"

After the app has been installed, initialize a new Composer project in the app's root directory and install ReactPHP as dependency.

Alternatively, start with a local project and the deploy it into the application directory.

SSH shell session
$ composer init --require=react/http --require=react/socket

After that, create a server.php with the following contents:

server.php
<?php

require __DIR__ . '/../vendor/autoload.php';

$http = new React\Http\HttpServer(function (Psr\Http\Message\ServerRequestInterface $request) {
return React\Http\Message\Response::plaintext(
"Hello World!\n"
);
});

$PORT = getenv('PORT');
if($PORT === false) {
throw new Exception("PORT environment variable not set");
}

$socket = new React\Socket\SocketServer('0.0.0.0:' . $PORT);
$http->listen($socket);

echo "PHP Worker sample app is running" . PHP_EOL;

As soon as you connect a domain to your application, the ReactPHP server will be accessible via HTTP.

Laravel Reverb

To start a Laravel Reverb application, create a PHP worker with the following arguments:

Local shell session
$ mw app create php-worker --wait --entrypoint "bash -c 'php artisan serve --port $PORT'"

Symfony Messenger

To start a Symfony Messenger worker, create a PHP worker with the following arguments:

Local shell session
$ mw app create php-worker --wait --entrypoint "php bin/console messenger:consume async"

Other tutorials

Running commands from a different directory

Oftentimes, you may already have an existing application (running as a regular PHP app), and want to start a worker process in that same directory.

Currently, the worker process is always started from its own directory, which may not be the same as the directory of the main application. To work around this, you can create a small shell script that changes the directory before starting the worker process, and use that script as the entrypoint:

#!/bin/bash
cd /html/actual-installation-directory
exec php artisan serve --port $PORT

Running multiple worker instances

Sometimes, you may want to run multiple worker instances of the same application. While this can be achieved by creating multiple PHP worker apps, you can also run multiple worker instances within the same PHP worker app using a process manager like pm2.

To do this, add Node.js as a dependency to your PHP worker app:

Local shell session
$ mw app dependency update <app-id> --set node=~22.0

Then, install PM2 via NPM:

SSH shell session
$ npm install -g pm2

In your application directory, create an ecosystem configuration file ecosystem.config.js:

ecosystem.config.js
module.exports = {
apps: [
{
name: "test-app",
instances: 2,
script: "bin/console",
args: "messenger:consume async",
watch: ["src"],
interpreter: "php",
cwd: "/html/actual-installation-directory", // optional
},
],
};

Finally, assert that the pm2 command is used as the entrypoint for your PHP worker app:

Local shell session
$ mw app update <app-id> --entrypoint "pm2 start ecosystem.config.js --no-daemon"