安装swooletw/laravel-swoole扩展,前提是php安装swoole 库
composer require swooletw/laravel-swoole
代码完整安装流程如下:
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ sudo composer require swooletw/laravel-swoole
./composer.json has been updated
Running composer update swooletw/laravel-swoole
Loading composer repositories with package information
Updating dependencies
Lock file operations: 2 installs, 0 updates, 0 removals
- Locking predis/predis (v1.1.10)
- Locking swooletw/laravel-swoole (v2.13.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 2 installs, 0 updates, 0 removals
- Downloading predis/predis (v1.1.10)
- Downloading swooletw/laravel-swoole (v2.13.0)
- Installing predis/predis (v1.1.10): Extracting archive
- Installing swooletw/laravel-swoole (v2.13.0): Extracting archive
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Package fruitcake/laravel-cors is abandoned, you should avoid using it. No replacement was suggested.
Package swiftmailer/swiftmailer is abandoned, you should avoid using it. Use symfony/mailer instead.
Package fzaninotto/faker is abandoned, you should avoid using it. No replacement was suggested.
Package phpunit/php-token-stream is abandoned, you should avoid using it. No replacement was suggested.
Generating optimized autoload files
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> @php artisan package:discover --ansi
Discovered Package: facade/ignition
Discovered Package: fideloper/proxy
Discovered Package: fruitcake/laravel-cors
Discovered Package: laravel/sanctum
Discovered Package: laravel/tinker
Discovered Package: nesbot/carbon
Discovered Package: nunomaduro/collision
Discovered Package: swooletw/laravel-swoole
Package manifest generated successfully.
69 packages you are using are looking for funding.
Use the `composer fund` command to find out more!
Found 5 security vulnerability advisories affecting 2 packages.
Run "composer audit" for a full list of advisories.
执行下面命令生成配置文件
sudo php artisan vendor:publish --provider="SwooleTW\Http\LaravelServiceProvider"
生成文件如下
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ sudo php artisan vendor:publish --provider="SwooleTW\Http\LaravelServiceProvider"
Copied File [/vendor/swooletw/laravel-swoole/config/swoole_http.php] To [/config/swoole_http.php]
Copied File [/vendor/swooletw/laravel-swoole/config/swoole_websocket.php] To [/config/swoole_websocket.php]
Copied File [/vendor/swooletw/laravel-swoole/routes/websocket.php] To [/routes/websocket.php]
Publishing complete.
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$
然后在config/app.php中的providers里面添加
SwooleTW\Http\LaravelServiceProvider::class
Swoole命令
在网站根目录执行下面命令
开启命令
php artisan swoole:http start #开启
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ php artisan swoole:http start
Starting swoole http server...
Swoole http server started: <http://127.0.0.1:1215>
php artisan swoole:http stop #关闭
php artisan swoole:http reload #平滑重启
php artisan swoole:http restart #重启
php artisan swoole:http infos #查看信息
默认开启了 http://127.0.0.1:1215 这个地址。在服务器上开启我们的端口 1215 ,由于我们服务器是nginx,所以这里面配置nginx代理 127.0.0.1:1215这个端口来执行操作
关闭命令
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ php artisan swoole:http stop
Stopping swoole http server...
> success
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$
平滑重启
重启
查看信息
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ php artisan swoole:http infos
+-----------------+-----------------------------------------------------------------------+
| Name | Value |
+-----------------+-----------------------------------------------------------------------+
| PHP Version | 7.3.31 |
| Swoole Version | 4.8.13 |
| Laravel Version | 7.28.4 |
| Listen IP | 127.0.0.1 |
| Listen Port | 1215 |
| Server Status | Online |
| Reactor Num | 2 |
| Worker Num | 2 |
| Task Worker Num | 0 |
| Websocket Mode | Off |
| Master PID | 2318431 |
| Manager PID | 2318436 |
| Log Path | /www/wwwroot/laravel7.yshuq.com/laravel7/storage/logs/swoole_http.log |
+-----------------+-----------------------------------------------------------
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$ sudo ps aux |grep swoole
ubuntu 2318431 0.0 1.2 256144 42304 pts/2 Sl+ 19:39 0:00 swoole_http_server: master process for Laravel
ubuntu 2318436 0.0 0.7 110720 26736 pts/2 S+ 19:39 0:00 swoole_http_server: manager process for Laravel
ubuntu 2318439 0.0 0.9 114952 31992 pts/2 S+ 19:39 0:00 swoole_http_server: worker process for Laravel
ubuntu 2318440 0.0 0.9 114952 31944 pts/2 S+ 19:39 0:00 swoole_http_server: worker process for Laravel
ubuntu 2319649 0.0 0.0 6300 656 pts/3 S+ 19:43 0:00 grep --color=auto swoole
ubuntu@VM-4-13-ubuntu:/www/wwwroot/laravel7.yshuq.com/laravel7$