crazywhalecc / static-php-cli

Build standalone PHP binary on Linux, macOS, FreeBSD, Windows, with PHP project together, with popular extensions included.
https://static-php.dev
MIT License
1.33k stars 233 forks source link

Error when trying create binary build of file a .phar from hyperf application #477

Open HallexCosta opened 5 months ago

HallexCosta commented 5 months ago

Hi @crazywhalecc! I have a new problem with build

New phpmicro update resolved the initial error from issue #468, but now I'm getting a new error. I followed the instructions to remove the buildroot and source folders and run bin/spc del-download micro && bin/spc download micro However, when I try to run the binary with ./my-binary it shows the hyperf commands normally Captura de tela de 2024-06-13 19-46-10

Now, if I actually try to run the server with the ./my-binary start command, it shows error logs Captura de tela de 2024-06-13 19-35-54

crazywhalecc commented 5 months ago

Can you find this file in your packaged phar? (not micro binary, just phar) https://github.com/hyperf/hyperf/blob/master/src/framework/src/Event/BeforeServerStart.php

And it would be better if you could provide phar build method.

HallexCosta commented 5 months ago

Can you find this file in your packaged phar? (not micro binary, just phar) https://github.com/hyperf/hyperf/blob/master/src/framework/src/Event/BeforeServerStart.php

And it would be better if you could provide phar build method.

Yes, when I run the command directly in the phar file, it recognizes the BeforeServerStart file

The library used to build phar was hyperf/phar this is guide how to usage Command used for build phar: swoole-cli -d "phar.readonly=0" bin/hyperf.php phar:build --name=hyperf.phar

❯ swoole-cli -d "phar.readonly=0" bin/hyperf.php phar:build --name=hyperf.phar
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[INFO] Creating phar hyperf.phar
[INFO] Adding main package "hyperf/hyperf-skeleton"

I followed all the specifications in the hyperf docs, even the part about moving the runtime folder to /tmp, then start the server

hallexcosta in github.com/hallexcosta/hyperf-binary via 🐘 v8.3.8 took 3s
❯ sudo swoole-cli hyperf.phar start
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#1 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#2 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#3 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#4 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#5 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#6 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#7 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
crazywhalecc commented 5 months ago

I forgot to ask earlier, how did you compile micro.sfx and php-cli? (Commands and operating system environment used)

HallexCosta commented 5 months ago

I forgot to ask earlier, how did you compile micro.sfx and php-cli? (Commands and operating system environment used)

Operation System: Ubuntu 22.04 PHP Version: 8.1.28

PHP CLI in my local machie: https://github.com/swoole/swoole-cli/releases/tag/v5.1.2.0

Command build: set EXTENSIONS "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium" && ./bin/spc build --build-micro --with-micro-fake-cli "$EXTENSIONS" Command combine: ./bin/spc micro:combine ../../hallexcosta/hyperf-binary/hyperf.phar -O ./my-hyperf-app

crazywhalecc commented 5 months ago

Try this? https://hyperf.wiki/3.1/#/en/phar?id=precautions

I briefly tried hyperf-skeleton locally and seemed to encounter the same error, but it turned out to be a problem with the pid file path, which should be a problem with hyperf itself.

HallexCosta commented 4 months ago

Try this? https://hyperf.wiki/3.1/#/en/phar?id=precautions

I briefly tried hyperf-skeleton locally and seemed to encounter the same error, but it turned out to be a problem with the pid file path, which should be a problem with hyperf itself.

Yes, I moved the runtime folder into /tmp on my Ubuntu machine

To get to this problem, I needed to adjust the server.php, logger.php and config.php files to identify if php is running in the phar environment, if so, use the /tmp directory as reference to BASE_PATH, otherwise continue using BASE_PATH

$basePath = str_starts_with(BASE_PATH, 'phar') ? '/tmp' : BASE_PATH;

Example: logger.php

return [
    'default' => [
        'handler' => [
            'class' => Monolog\Handler\StreamHandler::class,
            'constructor' => [
                'stream' => $basePath . '/runtime/logs/hyperf.log',
                'level' => Monolog\Logger::DEBUG,
            ],
        ],
        'formatter' => [
          ....
        ],
    ],
];

1) I tried running without moving the runtime folder to tmp, it says hyperf.pid was not found

[2024-06-24 12:38:49 @563350.0] WARNING file_put_contents(): open(/tmp/runtime/hyperf.pid) failed, Error: No such file or directory[2]
[INFO] Worker#0 started.

logs

❯ swoole-cli hyperf.phar start
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[2024-06-24 12:38:49 @563350.0] WARNING file_put_contents(): open(/tmp/runtime/hyperf.pid) failed, Error: No such file or directory[2]
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#1 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#2 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#3 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#4 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#5 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#6 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#7 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
^C⏎

2) Now when execute set EXTENSIONS "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium" && ./bin/spc download --for-extensions "$EXTENSIONS" --with-php=8.1 && ./bin/spc build --build-micro --with-micro-fake-cli "$EXTENSIONS" After build: spc micro:combine ../../hallexcosta/hyperf-binary/hyperf.phar -O ./my-hyperf-app

3) I run the file with swoole-cli hyperf.phar start and it works fine, now if I try to build the binary and run ./my-hyperf-app start then it gives me the initial error

HallexCosta commented 4 months ago

Try this? https://hyperf.wiki/3.1/#/en/phar?id=precautions

I briefly tried hyperf-skeleton locally and seemed to encounter the same error, but it turned out to be a problem with the pid file path, which should be a problem with hyperf itself.

Yes, I moved the runtime folder to /tmp on my Ubuntu machine

I modify the files server.php, logger.php and config.php to adjust the path

$basePath = str_starts_with(BASE_PATH, 'phar') ? '/tmp' : BASE_PATH;

The flow I followed to get to this problem

1) I tried running without moving the runtime folder to /tmp, it says hyperf.pid was not found

❯ swoole-cli hyperf.phar start
[DEBUG] [command] Commands registered by Hyperf\Command\Listener\RegisterCommandListener
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Command\Listener\RegisterCommandListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\Config\Listener\RegisterPropertyHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\DbConnection\Listener\RegisterConnectionResolverListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ExceptionHandlerListener listener.
[DEBUG] Event Hyperf\Framework\Event\BootApplication handled by Hyperf\ExceptionHandler\Listener\ErrorExceptionHandler listener.
[DEBUG] Event Hyperf\Framework\Event\BeforeMainServerStart handled by Hyperf\Process\Listener\BootProcessListener listener.
[2024-06-24 12:38:49 @563350.0] WARNING file_put_contents(): open(/tmp/runtime/hyperf.pid) failed, Error: No such file or directory[2]
[INFO] Worker#0 started.
[INFO] HTTP Server listening at 0.0.0.0:9501
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#1 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#2 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#3 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#4 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#5 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#6 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnManagerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[INFO] Worker#7 started.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\AfterWorkerStartListener listener.
[DEBUG] Event Hyperf\Framework\Event\OnStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
[DEBUG] Event Hyperf\Framework\Event\AfterWorkerStart handled by Hyperf\Server\Listener\InitProcessTitleListener listener.
^C⏎

3) Now when execute set EXTENSIONS "bcmath,calendar,ctype,curl,dba,dom,exif,filter,fileinfo,iconv,mbstring,mbregex,openssl,pcntl,pdo,swoole,phar,posix,readline,simplexml,sockets,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib,sodium" && ./bin/spc download --for-extensions "$EXTENSIONS" --with-php=8.1 && ./bin/spc build --build-micro --with-micro-fake-cli "$EXTENSIONS" After build: spc micro:combine ../../hallexcosta/hyperf-binary/hyperf.phar -O ./my-hyperf-app

3) I run the file with swoole-cli hyperf.phar start and it works fine, now if I try to build the binary and run my-hyperf-app start then it gives me the initial error

HallexCosta commented 4 months ago

I reset my settings and cloned the project, downloaded and built it again and now I'm getting an error even though I set the memory_limit as an argument when phar is built

Build command: spc build --build-micro "bcmath,calendar,ctype,curl,dba,dom,exif,fileinfo,filter,iconv,mbregex,mbstring,mysqlnd,openssl,pcntl,pdo,pdo_mysql,pdo_sqlite,phar,posix,readline,simplexml,sockets,sodium,sqlite3,tokenizer,xml,xmlreader,xmlwriter,zip,zlib" -I "memory_limit=8G"

hallexcosta in static-php-cli on  main [?] via 🐘 v8.2.13 took 9s
❯ spc micro:combine ../../hallexcosta/hyperf-binary/hyperf.phar -O ./my-hyperf-app
     _        _   _                 _
 ___| |_ __ _| |_(_) ___      _ __ | |__  _ __
/ __| __/ _` | __| |/ __|____| '_ \| '_ \| '_ \
\__ \ || (_| | |_| | (_|_____| |_) | | | | |_) |
|___/\__\__,_|\__|_|\___|    | .__/|_| |_| .__/   v2.2.4
                             |_|         |_|

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 68918384 bytes) in /home/hallexcosta/local/github.com/crazywhalecc/static-php-cli/src/SPC/command/MicroCombineCommand.php on line 89
crazywhalecc commented 4 months ago

The memory limit issue you mentioned is spc bug (the micro and phar file is too large), and I will fix it later. And I'm searching and testing hyperf packaging command now.

crazywhalecc commented 4 months ago

I tried hyperf locally, checked the source code, and also tried to package a pure swoole application. Only the hyperf package had this problem. Maybe the problem is caused by hyperf itself. We need to hand it over to the hyperf author to check the problem.