dunglas / frankenphp

🧟 The modern PHP app server

Home Page:https://frankenphp.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

FrankenPHP do no build binary with embed php app

grandrr opened this issue · comments

What happened?

So i had try build binary with embed php app, build finish successfully but binary do not include embed php app, or i dont know how to build/use it.

When i had try use dockerfile as described here:
https://frankenphp.dev/docs/embed/#creating-a-linux-binary
I got error that this is not git folder, after some digging in issues described here i realise that i should add this line:
FRANKENPHP_VERSION=1.1.2
as result my Dockerfile starting looks like:

FROM --platform=linux/amd64 dunglas/frankenphp:static-builder

# Copy your app
WORKDIR /go/src/app/dist/app
COPY . .

# Build the static binary, be sure to select only the PHP extensions you want
WORKDIR /go/src/app/
RUN FRANKENPHP_VERSION=1.1.2 \
    EMBED=dist/app/ \
    PHP_EXTENSIONS=ctype,iconv,pdo_sqlite \
    ./build-static.sh

So I had try build container by command:
docker build -t static-app -f static-build.Dockerfile .

This time its build successfully.

After i had try run command for build binary its stuck for some reason. So i remove all section related to run command for build binary, Dockerfile start looks like:

FROM --platform=linux/amd64 dunglas/frankenphp:static-builder

# Copy your app
WORKDIR /go/src/app/dist/app
COPY . .

# Build the static binary, be sure to select only the PHP extensions you want
WORKDIR /go/src/app/

I had build one more time, go inside container and run command inside container.

Run container by command:
docker run -it -p 127.0.0.1:80:80 static-app

after i get inside container in to folder:
/go/src/app as described in Dockerfile

And in this folder i run command for build binary like this:
FRANKENPHP_VERSION=1.1.2 EMBED=dist/app/ PHP_EXTENSIONS=ctype,iconv,pdo_sqlite ./build-static.sh

As result i get binary file, so i had run it like this:
./dist/frankenphp-linux-x86_64 php-server -v

its executed fine, but after i had try hit in browser:
http://127.0.0.1/
I got no response, and logs like this:

2024/04/25 17:50:59.925	DEBUG	http.handlers.file_server	sanitized path join	{"site_root": ".", "request_path": "/", "result": "."}
2024/04/25 17:50:59.926	DEBUG	http.handlers.file_server	no index file in directory	{"path": ".", "index_filenames": ["index.html", "index.txt"]}
2024/04/25 17:50:59.926	DEBUG	http.log.error	{id=7fu9petbc} fileserver.(*FileServer).notFound (staticfiles.go:629): HTTP 404	{"request": {"remote_ip": "172.17.0.1", "remote_port": "60084", "client_ip": "172.17.0.1", "proto": "HTTP/1.1", "method": "GET", "host": "127.0.0.1", "uri": "/", "headers": {"Sec-Ch-Ua-Platform": ["\"macOS\""], "Sec-Fetch-Mode": ["navigate"], "Sec-Fetch-Dest": ["document"], "Accept-Encoding": ["gzip, deflate, br, zstd"], "Accept-Language": ["en-GB,en-US;q=0.9,en;q=0.8,uk;q=0.7"], "Connection": ["keep-alive"], "Sec-Ch-Ua-Mobile": ["?0"], "Accept": ["text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7"], "User-Agent": ["Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"], "Sec-Fetch-Site": ["none"], "Sec-Fetch-User": ["?1"], "Cache-Control": ["max-age=0"], "Sec-Ch-Ua": ["\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\""], "Upgrade-Insecure-Requests": ["1"]}}, "duration": 0.0004179, "status": 404, "err_id": "7fu9petbc", "err_trace": "fileserver.(*FileServer).notFound (staticfiles.go:629)"}

So i had try run this one:
http://127.0.0.1/index.php
and got response in browser:

Warning: Unknown: Failed to open stream: No such file or directory in Unknown on line 0

Fatal error: Failed opening required '/go/src/app/index.php' (include_path='.:') in Unknown on line 0

So this is mean its looking for files in same root as binary is located, so as there is index.php copied from my local machine by command in Dockerfile i had try run it by url:
http://127.0.0.1/dist/app/index.php

and got response from my php file.

and my project:
Screenshot 2024-04-25 at 19 57 16

Build Type

Docker (Debian Bookworm)

Worker Mode

Yes

Operating System

GNU/Linux

CPU Architecture

x86_64

PHP configuration

franken

Relevant log output

No response

Hello @grandrr! Instead of

RUN FRANKENPHP_VERSION=1.1.2 \
    EMBED=dist/app/ \
    PHP_EXTENSIONS=ctype,iconv,pdo_sqlite \
    ./build-static.sh

Can you instead run the EMBED command first, like this:

RUN EMBED=dist/app/ \
    FRANKENPHP_VERSION=1.1.2 \
    PHP_EXTENSIONS=... \
    ./build-static.sh

I'm working on a Dockerfile generator for Laravel apps( fly-apps/dockerfile-laravel ) And I've recently added support to generate a Dockerfile that uses FrankenPHP to build a standalone binary( and get it deployable at Fly.io )! In my journey I've also experienced a similar misconception about running EMBED incorrectly as I've mentioned in a post. See items under ( NOTE! ).

I think that running EMBED first before declaring FRANKENPHP_VERSION should proceed with proper binary generation. ( although it took some time for the build to finish ). And once the build finishes, and you run ./dist/frankenphp-linux-x86_64 php-server , you should be able to see a log like:

embedded PHP app 📦     {"path": "/tmp/frankenphp_a204c16a3cc7597030296b259d908317  app.tar"}

That should be your sign that the app has been embedded successfully!

Thanks KTanAug21, i will try!

Thanks one more time! its working! i got this log:
embedded PHP app 📦 {"path": "/tmp/frankenphp_d5058167b13bd154646861207bd8944c app.tar"}
and it give me response from index.php in public folder, next try will be with real project.