PHP二進(jìn)制 Swoole-Cli 了解一下
簡(jiǎn)介
Swoole-Cli 是一個(gè) PHP 的二進(jìn)制發(fā)行版,集成了 swoole、php 內(nèi)核、php-cli、php-fpm 以及多個(gè)常用擴(kuò)展。Swoole-Cli 是全部靜態(tài)編譯打包的,不依賴任何操作系統(tǒng)的 so 動(dòng)態(tài)鏈接庫(kù),具備非常好的移植性,可以在任意 Linux 系統(tǒng)之間復(fù)制,下載即可使用。
作為 PHP 開發(fā)者都應(yīng)該知道 PHP 有兩種運(yùn)行模式:php-fpm和php-cli。那么在 Swoole 5.0 中將迎來(lái)一種新的運(yùn)行模式:swoole-cli。
Swoole將像node.js這樣作為獨(dú)立程序提供給用戶,而不是作為PHP的一個(gè)擴(kuò)展。
除此之外swoole-cli會(huì)盡可能地對(duì)php-src進(jìn)行裁剪,移除一些不用的機(jī)制、模塊、擴(kuò)展、函數(shù)、類型、常量、代碼,使得整個(gè)程序可以在幾分鐘之內(nèi)編譯完成。
操作系統(tǒng)支持
Swoole-Cli 會(huì)提供 Linux、macOS、Windows(Cygwin) 3種操作系統(tǒng)的二進(jìn)制包的支持。
安裝
下載
Linux 系統(tǒng)
wget https://github.com/swoole/swoole-cli/releases/download/v5.1.3.0/swoole-cli-v5.1.3-linux-x64.tar.xz
Windows 系統(tǒng)
https://github.com/swoole/swoole-cli/releases/download/v5.1.3.0/swoole-cli-v5.1.3-cygwin-x64.zip
配置環(huán)境
tar -xf swoole-cli-v5.1.3-linux-x64.tar.xz
chmod u+x swoole-cli
sudo mv swoole-cli /usr/bin/swoole-cli
查看版本號(hào)swoole-cli -v
swoole-cli -v
Swoole 5.1.3 (cli) (built: Jun 7 2024 07:19:27) (NTS)
查看已安裝擴(kuò)展swoole-cli -m
swoole-cli -m
[PHP Modules]
apcu
bcmath
bz2
Core
ctype
curl
date
dom
ds
exif
fileinfo
filter
gd
gmp
hash
iconv
imagick
inotify
intl
json
libxml
mbstring
mongodb
mysqli
mysqlnd
opcache
openssl
pcntl
pcre
PDO
pdo_mysql
Phar
posix
readline
redis
Reflection
session
SimpleXML
soap
sockets
sodium
SPL
sqlite3
ssh2
standard
swoole
tokenizer
xlswriter
xml
xmlreader
xmlwriter
xsl
yaml
Zend OPcache
zip
zlib
[Zend Modules]
Zend OPcache
配置文件
swoole-cli 默認(rèn)不加載任何 php.ini 配置文件。可通過(guò) -d 參數(shù)來(lái)設(shè)置 PHP 選項(xiàng)或使用 -c 參數(shù)指定加載的php.ini配置文件。
swoole-cli -d swoole.use_shortname=off bin/hyperf.php start
swoole-cli -c /tmp/php.ini -v
啟動(dòng) PHP-FPM
swoole-cli 集成了 PHP-FPM ,可使用 -P 命令來(lái)啟動(dòng) fpm 。
# 查看幫助文件
swoole-cli -P -h
# 運(yùn)行 FPM
swoole-cli -P --fpm-config /opt/php-8.1/etc/php-fpm.conf -p /opt/php-8.1/var
# 關(guān)閉守護(hù)進(jìn)程
swoole-cli -P --fpm-config /opt/php-8.1/etc/php-fpm.conf -p /opt/php-8.1/var -F
# 使用 root 賬戶啟動(dòng)
swoole-cli -P --fpm-config /opt/php-8.1/etc/php-fpm.conf -p /opt/php-8.1/var -F -R
啟動(dòng) Swoole Server
server.php文件:
<?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 開源技術(shù)小棧!');
});
$http->start();
通過(guò)swoole-cli啟動(dòng):
swoole-cli server.php
Swoole http server is started at http://127.0.0.1:9501
通過(guò)curl訪問(wèn)
curl http://127.0.0.1:9501
Hello 開源技術(shù)小棧!
啟動(dòng) webman
cd webman.tinywan.com
swoole-cli start.php start
Workerman[start.php] start in DEBUG mode
-------------------------------------------- WORKERMAN --------------------------------------------
Workerman version:4.1.15 PHP version:8.1.27 Event-Loop:\Workerman\Events\Select
--------------------------------------------- WORKERS ---------------------------------------------
proto user worker listen processes status
tcp www webman http://0.0.0.0:8787 8 [OK]
tcp www monitor none 1 [OK]
---------------------------------------------------------------------------------------------------
Press Ctrl+C to stop. Start success.