Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to start, stop workerman from web browser? #883

Open
blackcatt opened this issue Feb 22, 2023 · 6 comments
Open

How to start, stop workerman from web browser? #883

blackcatt opened this issue Feb 22, 2023 · 6 comments

Comments

@blackcatt
Copy link

Hi, again )
How to realize full work cycle of workerman from web page (php/nginx) ?
php-fpm start with low privileged user wwwdata
If I understood correctly, workerman works correctly only from root. But I really want to press the beautiful start-stop buttons in the browser, and not typing in the console.
Thanks.

@fuzqing
Copy link
Contributor

fuzqing commented Feb 23, 2023

Set the user of the worker processes.
You can see #139 (comment)

@blackcatt
Copy link
Author

Bad luck. Can`t start any simple workerman in background from php-fpm/nginx script.
My task is to start and stop a large number of workers from the web interface that perform different tasks.
Now I see only one (bad?) way : Create one other (root) worker that will be start/stop other :) and manage it from asyncTcpConnecton
Any ideas?

@fuzqing
Copy link
Contributor

fuzqing commented Feb 26, 2023

  1. Create wk-manager.php file in your php-fpm project.
 <?php
// This is just a simple example, please note for security verification.
$action = $_GET['action'] ?? null;
if (!empty($action) && in_array($action,['start','stop','status'])) {
    if ($action === 'start') {
        $action = "$action -d";
    }
   // You can get the static-php-cli bin file from https://www.workerman.net/download
    $cmd = "/your/path/of/workerman-app/php /your/path/of/workerman-app/start.php $action";
  // Don't forget to enable exec function in php.ini
    exec($cmd,$output);
    echo "<pre>";
    print_r($output);
    echo "</pre>";
} else {
    header('HTTP/1.1 400 Bad Request');
    echo '400 Bad Request';
}
  1. An http server
<?php
// start.php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
use Workerman\Protocols\Http\Request;

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

$http_worker = new Worker("http://0.0.0.0:2345");
// Set the user of worker processes. 
$http_worker->user = 'www';  // www is my running user for php-fpm and nginx.

$http_worker->count = 4;

$http_worker->onMessage = function(TcpConnection $connection, Request $request)
{
    $connection->send('hello world');
};

Worker::runAll();
  1. Some results:
    start
    status
    stop

@fuzqing
Copy link
Contributor

fuzqing commented Feb 26, 2023

If you are worried about the security risks of opening the exec function, you can use workerman to start an http server (restapi) with local access only, and then just access the restapi to manage your workerman.

@blackcatt
Copy link
Author

Thanks, seems like it works )
Why i must use static cimpiled php-cli ?
I tested with my dynamic php8.1 (out of the box) ant it works too.

@fuzqing
Copy link
Contributor

fuzqing commented Feb 26, 2023

It's not required, it's a workerman out-of-the-box runtime environment, and with it you'll no longer need to install various php environments and event and such extensions on your server.
You can also package it with your workerman project and run it on a linux server that doesn't have php installed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants