Skip to content

Commit

Permalink
feat: improve Dockerfile and add some docs (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas authored Oct 13, 2022
1 parent b971c07 commit acc4883
Show file tree
Hide file tree
Showing 15 changed files with 1,139 additions and 107 deletions.
2 changes: 2 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Dockerfile
Dockerfile.dev
12 changes: 12 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,15 @@ Run the test app:

cd ../../testdata/
../internal/testserver/testserver

## Misc Dev Resources

* [PHP embedding in uWSGI](https://github.com/unbit/uwsgi/blob/master/plugins/php/php_plugin.c)
* [PHP embedding in NGINX Unit](https://github.com/nginx/unit/blob/master/src/nxt_php_sapi.c)
* [PHP embedding in Go (go-php)](https://github.com/deuill/go-php)
* [PHP embedding in Go (GoEmPHP)](https://github.com/mikespook/goemphp)
* [PHP embedding in C++](https://gist.github.com/paresy/3cbd4c6a469511ac7479aa0e7c42fea7)
* [Extending and Embedding PHP by Sara Golemon](https://books.google.fr/books?id=zMbGvK17_tYC&pg=PA254&lpg=PA254#v=onepage&q&f=false)
* [What the heck is TSRMLS_CC, anyway?](http://blog.golemon.com/2006/06/what-heck-is-tsrmlscc-anyway.html)
* [PHP embedding on Mac](https://gist.github.com/jonnywang/61427ffc0e8dde74fff40f479d147db4)
* [SDL bindings](https://pkg.go.dev/github.com/veandco/[email protected]/sdl#Main)
41 changes: 28 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,33 +25,48 @@ RUN apt-get update && \
libxml2-dev \
zlib1g-dev \
bison \
# Dev tools \
git \
gdb \
valgrind \
neovim && \
zsh && \
echo 'set auto-load safe-path /' > /root/.gdbinit && \
echo '* soft core unlimited' >> /etc/security/limits.conf \
&& \
apt-get clean

RUN git clone https://github.com/dunglas/php-src.git && \
RUN git clone --depth=1 --single-branch --branch=frankenphp-8.2 https://github.com/dunglas/php-src.git && \
cd php-src && \
git checkout frankenphp-8.2 && \
#export CFLAGS="-DNO_SIGPROF" && \
# --enable-embed is only necessary to generate libphp.so, we don't use this SAPI directly
./buildconf && \
./configure --enable-embed=static --enable-zts --disable-zend-signals --enable-debug && \
./configure \
--enable-embed=static \
--enable-zts \
--disable-zend-signals && \
make -j6 && \
make install && \
#rm -Rf php-src/ && \
rm -Rf php-src/ && \
ldconfig && \
php --version

RUN echo "zend_extension=opcache.so\nopcache.enable=1" > /usr/local/lib/php.ini

WORKDIR /go/src/app

COPY go.mod go.sum ./
RUN go get -v ./...

RUN mkdir caddy && cd caddy
COPY go.mod go.sum ./

RUN go get -v ./... && \
cd ..

COPY . .

RUN go get -d -v ./...
RUN cd caddy/frankenphp && \
go build && \
cp frankenphp /usr/local/bin && \
cp /go/src/app/caddy/frankenphp/Caddyfile /etc/Caddyfile && \
rm -Rf /go

WORKDIR /app

RUN mkdir -p /app/public
RUN echo '<?php phpinfo();' > /app/public/index.php

CMD [ "frankenphp", "run", "--config", "/etc/Caddyfile" ]
61 changes: 61 additions & 0 deletions Dockerfile.dev
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
FROM golang

ARG LIBICONV_VERSION=1.17
ENV PHPIZE_DEPS \
autoconf \
dpkg-dev \
file \
g++ \
gcc \
libc-dev \
make \
pkg-config \
re2c

RUN apt-get update && \
apt-get -y --no-install-recommends install \
$PHPIZE_DEPS \
libargon2-dev \
libcurl4-openssl-dev \
libonig-dev \
libreadline-dev \
libsodium-dev \
libsqlite3-dev \
libssl-dev \
libxml2-dev \
zlib1g-dev \
bison \
# Dev tools \
git \
gdb \
valgrind \
neovim \
zsh && \
echo 'set auto-load safe-path /' > /root/.gdbinit && \
echo '* soft core unlimited' >> /etc/security/limits.conf \
&& \
apt-get clean

RUN git clone --depth=1 --single-branch --branch=frankenphp-8.2 https://github.com/dunglas/php-src.git && \
cd php-src && \
git checkout frankenphp-8.2 && \
export CFLAGS="-DNO_SIGPROF" && \
# --enable-embed is only necessary to generate libphp.so, we don't use this SAPI directly
./buildconf && \
./configure \
--enable-embed=static \
--enable-zts \
--disable-zend-signals \
--enable-debug && \
make -j6 && \
make install && \
ldconfig && \
php --version

RUN echo "zend_extension=opcache.so\nopcache.enable=1" > /usr/local/lib/php.ini

WORKDIR /go/src/app

COPY . .

RUN go get -d -v ./...
96 changes: 10 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,93 +1,17 @@
# Caddy PHP
# FrankenPHP: Modern App Server for PHP


## Install

### Docker

The easiest way to get started is to use our Docker image:
## Getting Started

```
docker build -t frankenphp .
```

### Compile From Sources

#### Install PHP

To use FrankenPHP, you currently need to compile a fork of PHP.
Patches have been contributed upstream, and some have already
been merged. It will be possible to use the vanilla version of PHP
starting with version 8.3.

First, get our PHP fork and prepare it:

docker run -v $PWD:/app/public \
-p 80:80 -p 443:443 \
dunglas/frankenphp
```
git clone https://github.com/dunglas/php-src.git
cd php-src
git checkout frankenphp-8.2
./buildconf
```

Then, configure PHP for your platform:

**Linux**:

```
./configure \
--enable-embed \
--enable-zts \
--disable-zend-signals
```

**Mac**:

Use the [Homebrew](https://brew.sh/) package manager to install
`libiconv` and `bison`:

```
brew install libiconv bison
echo 'export PATH="/opt/homebrew/opt/bison/bin:$PATH"' >> ~/.zshrc
```

Then run the configure script:

```
./configure \
--enable-embed=static \
--enable-zts \
--disable-zend-signals \
--disable-opcache-jit \
--with-iconv=/opt/homebrew/opt/libiconv/
```

These flags are required, but you can add other flags (extra extensions...)
if needed.

Finally, compile PHP:

```
make -j6
make install
```

#### Compile the Go App

You can now use the Go lib and compile our Caddy build:

```
cd caddy/frankenphp
go build
```
Your app is served at https://localhost!

## Misc Dev Resources
## Docs

* [PHP embedding in uWSGI](https://github.com/unbit/uwsgi/blob/master/plugins/php/php_plugin.c)
* [PHP embedding in NGINX Unit](https://github.com/nginx/unit/blob/master/src/nxt_php_sapi.c)
* [PHP embedding in Go (go-php)](https://github.com/deuill/go-php)
* [PHP embedding in Go (GoEmPHP)](https://github.com/mikespook/goemphp)
* [PHP embedding in C++](https://gist.github.com/paresy/3cbd4c6a469511ac7479aa0e7c42fea7)
* [Extending and Embedding PHP by Sara Golemon](https://books.google.fr/books?id=zMbGvK17_tYC&pg=PA254&lpg=PA254#v=onepage&q&f=false)
* [What the heck is TSRMLS_CC, anyway?](http://blog.golemon.com/2006/06/what-heck-is-tsrmlscc-anyway.html)
* [PHP embedding on Mac](https://gist.github.com/jonnywang/61427ffc0e8dde74fff40f479d147db4)
* [SDL bindings](https://pkg.go.dev/github.com/veandco/[email protected]/sdl#Main)
* [worker mode](docs/worker.md)
* [Early Hints support (103 HTTP status code)](docs/early-hints.md)
* [compile from sources](docs/compile.md)
37 changes: 37 additions & 0 deletions caddy/frankenphp/Caddyfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
# Debug
{$DEBUG}

frankenphp {
#worker /path/to/your/worker.php
{$FRANKENPHP_CONFIG}
}
}

{$SERVER_NAME:localhost}

log
route {
root * public/
# Add trailing slash for directory requests
@canonicalPath {
file {path}/index.php
not path */
}
redir @canonicalPath {path}/ 308

# If the requested file does not exist, try index files
@indexFiles file {
try_files {path} {path}/index.php index.php
split_path .php
}
rewrite @indexFiles {http.matchers.file.relative}

# FrankenPHP!
@phpFiles path *.php
php @phpFiles
encode zstd gzip
file_server

respond 404
}
2 changes: 2 additions & 0 deletions caddy/frankenphp/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
// plug in Caddy modules here.
_ "github.com/caddyserver/caddy/v2/modules/standard"
_ "github.com/dunglas/frankenphp/caddy"
_ "github.com/dunglas/mercure/caddy"
_ "github.com/dunglas/vulcain/caddy"
)

func main() {
Expand Down
Loading

0 comments on commit acc4883

Please sign in to comment.