Swoole-Cli 介绍
Swoole-Cli 是一个 PHP 的二进制发行版,集成了 swoole、php 内核、php-cli、php-fpm以及多个常用扩展。Swoole-Cli 是全部静态编译打包的,不依赖任何操作系统的so动态链接库,具备非常好的移植性,可以在任意 Linux 系统之间复制,下载即可使用。
GitHub: https://github.com/swoole/swoole-cli
操作系统
Swoole-Cli 会提供Linux、macOS、Windows(Cygwin) 3种操作系统的二进制包的支持。
Windows环境推荐使用WSL
主要更新
PHP同步到8.1.12版本- 增加了
mongodb扩展 - 集成了
cli http server和php-fpm
下载安装
下载地址:
直接下载后放置到 /usr/bin 目录,并且设置为可运行,chmod +x /usr/bin/swoole-cli 即可。在终端中执行:
swoole-cli -v
swoole-cli -m
替代 PHP
将 Swoole-Cli 软链接到 php,替代 PHP
ln -s /usr/bin/swoole-cli /usr/bin/php
php -v
# 安装 composer
curl -sS https://getcomposer.org/installer | php
配置文件
swoole-cli 默认不加载任何 php.ini 配置文件。
可通过 -d 参数来设置 PHP 选项或使用 -c 参数指定加载的php.ini配置文件。
swoole-cli -d swoole.use_shortname=off bin/hyperf.php start
swoole-cli -c /tmp/php.ini -v
启动 PHP-FPM
swoole-cli 集成了 PHP-FPM ,可使用 -P 命令来启动 fpm 。
# 查看帮助文件
swoole-cli -P -h
# 运行 FPM
swoole-cli -P --fpm-config /opt/php-8.1/etc/php-fpm.conf -p /opt/php-8.1/var
# 关闭守护进程
swoole-cli -P --fpm-config /opt/php-8.1/etc/php-fpm.conf -p /opt/php-8.1/var -F
# 使用 root 账户启动
swoole-cli -P --fpm-config /opt/php-8.1/etc/php-fpm.conf -p /opt/php-8.1/var -F -R
启动 CLI Server
# 使用 Laravel Artisan 工具
swoole-cli artisan serve
# 启动 CLI Server
swoole-cli -S 127.0.0.1:9001
启动 Swoole Server
server.php
$http = new Swoole/Http/Server('127.0.0.1', 9501);
$http->on('start', function ($server) {
echo "Swoole http server is started at http://127.0.0.1:9501/n";
});
$http->on('request', function ($request, $response) {
$response->header('Content-Type', 'text/plain');
$response->end('Hello World');
});
$http->start();
执行
swoole-cli server.php
扩展列表
[email protected]:~/workspace/cli-swoole$ ./bin/swoole-cli -m
[PHP Modules]
bcmath
bz2
Core
ctype
curl
date
dom
exif
fileinfo
filter
gd
gmp
hash
iconv
imagick
intl
json
libxml
mbstring
mongodb
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
redis
Reflection
session
SimpleXML
soap
sockets
sodium
SPL
sqlite3
standard
swoole
tokenizer
xml
xmlreader
xmlwriter
xsl
yaml
zip
zlib
[Zend Modules]
IT极限技术分享汇