MixPHP V3 development process experience introduction to various operation modes of swoole, workman, FPM and cli server

Posted by shahab03 on Thu, 30 Dec 2021 17:02:28 +0100

MixPHP After V3 is released, users may not be able to start because it supports too many execution modes. Here is a general introduction:

  • CLI server: suitable for native development, zero expansion dependency, Windows/MacOS and other full platform support
  • PHP-FPM: suitable for shared development environment deployment and admin and other management background projects
  • Swoole, workerman: it is suitable for online deployment. Select one as needed

Multiple modes of Swoole:

  • Swoole multi process synchronization: it is suitable for projects that need to use third-party libraries that are not supported by the collaboration process. It is consistent with Workerman
  • Swoole multi process collaboration: suitable for projects that focus on mysql + redis and need ultra-high io performance
  • Swoole single process collaboration: single process collaboration is v2 The golang style collaborative process of version 2 is suitable for developing websocket

Almost all popular execution modes of PHP are supported, and the above execution mode code is seamlessly switched, so that efficiency and performance coexist.

Please help Star:

First create a skeleton

Let's take the development of an API project as an example to open MixPHP Development documentation There are the development tutorials of cli api web websocket grpc project. The README under the V3 start warehouse is the development document. If you don't understand, you can add ours Official QQ group Participate in the discussion.

  • First create a skeleton

If you are prompted that there is no extension support such as redis, you can use -- ignore platform reqs to temporarily ignore the dependency check

composer create-project --prefer-dist --ignore-platform-reqs mix/api-skeleton api

The directory structure after installation is as follows:

  • The bin directory is all entry files, and different files correspond to different drive modes
  • routes is the routing profile
  • public/index.php is the entry file of FPM and cli server modes
  • shell/server.sh is the deployment management process start|stop|restart
├── README.md
├── bin
│   ├── cli.php
│   ├── swoole.php
│   ├── swooleco.php
│   └── workerman.php
├── composer.json
├── composer.lock
├── conf
│   └── config.json
├── public
│   └── index.php
├── routes
│   └── index.php
├── runtime
├── shell
│   └── server.sh
├── src
│   ├── Command
│   ├── Container
│   ├── Controller
│   ├── Error.php
│   ├── Middleware
│   ├── Vega.php
│   └── functions.php
└── vendor

Native development using cli server zero expansion dependency mode

First, let's take a look at composer JSON, different from other frameworks, we recommend using composer run script startup program in the native development stage, which can perfectly cooperate with the debugging function of PhpStorm.

  • The command entry file for each execution mode is defined here
  • Composer run script -- timeout = 0 cliserver: start to start the command
  "scripts": {
    "cliserver:start": "php -S localhost:8000 public/index.php",
    "swoole:start": "php bin/swoole.php",
    "swooleco:start": "php bin/swooleco.php",
    "workerman:start": "php bin/workerman.php start",
    "cli:clearcache": "php bin/cli.php clearcache"
  }

Since it is now native development, we use CLI-Server Mode startup, zero extension dependency, no need for pcntl, event, swoole extensions, with hot update.

% composer run-script --timeout=0 cliserver:start
> php -S localhost:8000 public/index.php
PHP 7.3.24-(to be removed in future macOS) Development Server started at Tue Aug 10 17:00:55 2021
Listening on http://localhost:8000
Document root is /Users/***/mix/examples/api-skeleton
Press Ctrl-C to quit.

Test the default route

% curl http://127.0.0.1:8000/hello
hello, world!

Next, according to the document:

Deploying a shared development environment using PHP-FPM

Hot update is a rigid requirement, so we directly use PHP-FPM to deploy the shared development environment, which is completely consistent with the deployment methods of Laravel and ThinkPHP PHP can be rewritten by configuring rewrite in nginx.

server {
    server_name www.domain.com;
    listen 80;
    root /data/project/public;
    index index.html index.php;

    location / {
        if (!-e $request_filename) {
            rewrite ^/(.*)$ /index.php/$1 last;
        }
    }

    location ~ ^(.+\.php)(.*)$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_split_path_info ^(.+\.php)(.*)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

Online deployment using spoole multiprocess collaboration mode

You can choose between Swoole and workman at will. Here we take Swoole as an example.

  • Install first Swoole extend
  • Modify shell / server Absolute path and parameters in SH script

Here we choose the multi process collaboration mode of Swoole, so the entry file is bin / Swoole PHP. For other modes, refer to composer json

php=/usr/local/bin/php
file=/data/project/bin/swoole.php
cmd=start
numprocs=1

Start management

sh /data/project/shell/server.sh start
sh /data/project/shell/server.sh stop
sh /data/project/shell/server.sh restart

Next, add the startup command to crontab to prevent abnormal interruption of the program

*/1 * * * * sh /data/project/shell/server.sh start > /tmp/server.sh.log 2>&1 &

When modifying the code, use restart to make the code take effect

sh /data/project/shell/server.sh restart

Topics: PHP