使用 Micro 打包 PHP 应用
- lwmbs 巨好用的打包工具,对我来说,Golang的生存空间又被挤掉了一部分。
- box-skeleton 用于方便构建PHP二进制文件的骨架包。
前言
接下来我们使用一个例子,来介绍一下这个东西,平常我更新 Submodule 的时候,都需要进入到对应的文件夹,然后 checkout master 再 pull,着实麻烦。
当然,以下的能力,其实也可以写个 shell 脚本代替,但这不是为了介绍一下 micro 打包工具么
创建应用
composer create limingxinleo/box-skeleton sgit dev-master
创建脚本
首先我们先创建一个脚本
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://hyperf.wiki
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Command;
use Hyperf\Command\Annotation\Command;
use Hyperf\Command\Command as HyperfCommand;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputOption;
#[Command]
class UpdateCommand extends HyperfCommand
{
public function __construct(protected ContainerInterface $container)
{
parent::__construct('update');
}
public function configure()
{
parent::configure();
$this->setDescription('更新子模块');
$this->addOption('branch', 'B', InputOption::VALUE_OPTIONAL, '需要被更新的分支', 'master');
}
public function handle()
{
$branch = $this->input->getOption('branch');
$root = getcwd();
if (! file_exists($path = $root . '/.gitmodules')) {
$this->output->writeln($path . ' not found.');
return;
}
$content = file_get_contents($path);
$preg = '/path = (.*)/';
$matched = null;
preg_match_all($preg, $content, $matched);
$command = 'cd %s && git checkout %s && git pull && cd %s';
if ($paths = $matched[1] ?? null) {
foreach ($paths as $path) {
shell_exec(sprintf($command, $root . '/' . $path, $branch, $root));
}
}
}
}
打包脚本
- 首先,我们先打包成 phar
php bin/hyperf.php phar:build
- 接下来,下载已经可用的 micro
这里我使用的是,提前下载好的,大家也可以到 GitHub 上重新下载 github.com/dixyes/lwmbs/actions/ru...
我这里是 x86_64 的 Mac OS,所以提前准备好了 micro_8.0_x86_64
版本
wget https://alpine-apk-repository.knowyourself.cc/micro/v0.0.1/micro.8.0.x86_64.sfx
- 打包
打包出来的二进制文件,仅有 41M
cat micro.8.0.x86_64.sfx sgit.phar > sgit
chmod u+x sgit
mv sgit /usr/local/bin/
测试
找一个项目,测试下看看
$ php sgit.phar update
切换到分支 'master'
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (6/6), done.
remote: Total 6 (delta 4), reused 0 (delta 0), pack-reused 0
展开对象中: 100% (6/6), 841 字节 | 168.00 KiB/s, 完成.
来自 xxxx:xxxx/xxxx
xxxxx..xxxxx master -> origin/master
完美运行!!!
本作品采用《CC 协议》,转载必须注明作者和本文链接
👍,虽然不知道用来干嘛的
可以打包成不依赖php环境的可执行文件吗
目前php应用多数是配合 nginx 和php-fpm 来调用的。 打包成二进制后就无法配合使用nginx了吧。
但是换一个说法,将单独的php服务使用swow来编写,替换掉nginx,完全由php来实现。
将这样的代码项目打包成一个可执行的二进制文件,运行起来后,通过读取环境变量来连接nacos,mysql,来实现微服务化管理体系。
一方面打包成二进制,可以快速进行部署,同时也可以解决compose依赖库的问题,升级也方便
另一方面,php在代码加密这方便一直不好,容易源码泄露,这点在企业内部系统上是很明显的缺点,打包成二进制,再配合 部署授权的 license,可以很好的避免这个情况 。
我认为这个技术是非常重要的。
文章最后测试使用的命令 php sgit.phar update 这明显还是使用的php 来执行的。 改成使用 二进制文件 sgit 来执行 我认为更能体现本文的用意