From 1d9de1c6354a8abbd44dae647da4f88b79dd3b67 Mon Sep 17 00:00:00 2001 From: luming Date: Mon, 15 Jan 2024 16:21:08 +0800 Subject: [PATCH] update document. --- LICENSE.txt | 21 + README.MD | 139 +++++- caddy-with-php/config/Caddyfile.php-fpm | 6 - caddy-with-php/entry.sh | 13 - caddy-with-php/{ => normal}/Dockerfile | 28 +- caddy-with-php/{ => normal}/config/Caddyfile | 2 +- caddy-with-php/{ => normal}/config/index.html | 0 caddy-with-php/{ => normal}/entrypoint.sh | 2 +- caddy-with-php/supervisor/Dockerfile | 122 ++++++ caddy-with-php/supervisor/conf/caddy.conf | 10 + caddy-with-php/supervisor/conf/listener.conf | 7 + caddy-with-php/supervisor/conf/php-fpm.conf | 10 + caddy-with-php/supervisor/config/Caddyfile | 26 ++ caddy-with-php/supervisor/config/index.html | 394 ++++++++++++++++++ caddy-with-php/supervisor/listener.php | 18 + caddy-with-php/supervisor/supervisord.conf | 17 + 16 files changed, 779 insertions(+), 36 deletions(-) create mode 100644 LICENSE.txt delete mode 100644 caddy-with-php/config/Caddyfile.php-fpm delete mode 100644 caddy-with-php/entry.sh rename caddy-with-php/{ => normal}/Dockerfile (62%) rename caddy-with-php/{ => normal}/config/Caddyfile (96%) rename caddy-with-php/{ => normal}/config/index.html (100%) rename caddy-with-php/{ => normal}/entrypoint.sh (80%) create mode 100644 caddy-with-php/supervisor/Dockerfile create mode 100644 caddy-with-php/supervisor/conf/caddy.conf create mode 100644 caddy-with-php/supervisor/conf/listener.conf create mode 100644 caddy-with-php/supervisor/conf/php-fpm.conf create mode 100644 caddy-with-php/supervisor/config/Caddyfile create mode 100644 caddy-with-php/supervisor/config/index.html create mode 100644 caddy-with-php/supervisor/listener.php create mode 100644 caddy-with-php/supervisor/supervisord.conf diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..155850f --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 rainerosion + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.MD b/README.MD index 0fa9fee..f6537ff 100644 --- a/README.MD +++ b/README.MD @@ -1,4 +1,141 @@ # CaddyServer with PHP-FPM -# This is a simple Docker image to run CaddyServer with PHP-FPM. +This is a simple Docker image to run CaddyServer with PHP-FPM. +## Usage + +The following examples use images that have already been built; you can build your own images as described below. + +### Single Container + +- Configure your Caddyfile and place it in a folder on your host. Exapmle: + +```plaintext +{ + email admin@rainss.cn +} +example.com:80 { + encode gzip + root * /www/websites + php_fastcgi localhost:9000 + file_server +} +``` + +- Run the container with the following command. Example: + +```bash +docker run -d -p 80:80 -p 443:443 -v /path/to/your/Caddyfile:/etc/caddy/Caddyfile -v /path/to/your/site:/www/websites --name caddyserver-php-fpm rainautos/caddyserver-php-fpm +``` + +### Docker Compose [Multi-container] + +You can use Docker Compose to run CaddyServer with PHP-FPM. For example: + +- Configure your Caddyfile and Using the following command to create a container. + +- Caddyfile + +```plaintext +{ + email admin@rainss.cn +} +example.com:80 { + encode gzip + root * /www/websites + # notice: php-fpm is the name of the container + php_fastcgi php-fpm:9000 + file_server +} +``` + +#### Using Dockerfile + +```bash +version: '3' +services: + webservice: + container_name: caddy + image: caddy:latest + ports: + - "80:80" + - "443:443" + volumes: + - ${PWD}/Caddyfile:/etc/caddy/Caddyfile + - ${PWD}/config:/config + - ${PWD}/data:/data + - ${PWD}/websites:/www/websites + depends_on: + - php-cgi + restart: unless-stopped + networks: + - website + php-cgi: + container_name: php-fpm + build: + context: ./php-fpm/8.2 + dockerfile: Dockerfile + volumes: + - ${PWD}/websites:/www/websites + restart: unless-stopped + networks: + - website +networks: + website: + driver: bridge +``` + +#### Using Repository Image + +```bash +```yaml +version: '3' +services: + webservice: + container_name: caddy + image: caddy:latest + ports: + - "80:80" + - "443:443" + - "443:443/udp" + volumes: + - /etc/localtime:/etc/localtime + - ${PWD}/Caddyfile:/etc/caddy/Caddyfile + - ${PWD}/opt/caddy/config:/config + - ${PWD}/opt/caddy/data:/data + - ${PWD}/websites:/www/websites + depends_on: + - php-cgi + restart: always + networks: + - website + php-cgi: + container_name: php-fpm + image: rainautos/php-fpm:8.1.15 + volumes: + - /etc/localtime:/etc/localtime + - ${PWD}/websites:/www/websites + restart: always + networks: + - website +networks: + website: + driver: bridge +``` +## Build images + +You can find the Dockerfiles in the `supervisor` or `php-fpm` folder to build the images. + +### Build Caddy + PHP-FPM image + +```bash +cd caddy-with-php/supervisor +docker build -t rainautos/caddyserver-php-fpm:latest . +``` + +### Build PHP-FPM image + +```bash +cd php-fpm/8.3 +docker build -t rainautos/php-fpm:latest . +``` \ No newline at end of file diff --git a/caddy-with-php/config/Caddyfile.php-fpm b/caddy-with-php/config/Caddyfile.php-fpm deleted file mode 100644 index d4533f1..0000000 --- a/caddy-with-php/config/Caddyfile.php-fpm +++ /dev/null @@ -1,6 +0,0 @@ -:80 { - encode gzip - root * /www/websites/public - php_fastcgi 127.0.0.1:9000 - file_server -} \ No newline at end of file diff --git a/caddy-with-php/entry.sh b/caddy-with-php/entry.sh deleted file mode 100644 index 5d04eab..0000000 --- a/caddy-with-php/entry.sh +++ /dev/null @@ -1,13 +0,0 @@ -#!/bin/sh -if [ "${1}" = "-D" ]; then - exec /usr/bin/supervisord -n -c /etc/supervisord.conf -else - exec "$@" -fi - -# first arg is `-f` or `--some-option` -#if [ "${1#-}" != "$1" ]; then -# set -- php-fpm "$@" -#fi -# -#exec "$@" \ No newline at end of file diff --git a/caddy-with-php/Dockerfile b/caddy-with-php/normal/Dockerfile similarity index 62% rename from caddy-with-php/Dockerfile rename to caddy-with-php/normal/Dockerfile index 6ad467f..54fc175 100644 --- a/caddy-with-php/Dockerfile +++ b/caddy-with-php/normal/Dockerfile @@ -5,6 +5,7 @@ LABEL maintainer="rainerosion " ENV XDG_CONFIG_HOME /config ENV XDG_DATA_HOME /data +ENV CADDY_VERSION 2.7.6 # build dependencies ENV BUILD_DEPS \ @@ -82,26 +83,25 @@ RUN set -eux; \ echo " /usr/share/caddy/info.php; \ apkArch="$(apk --print-arch)"; \ case "$apkArch" in \ - x86_64) binArch='amd64'; checksum='b74311ec8263f30f6d36e5c8be151e8bc092b377789a55300d5671238b9043de5bd6db2bcefae32aa1e6fe94c47bbf02982c44a7871e5777b2596fdb20907cbf' ;; \ - armhf) binArch='armv6'; checksum='88756642ca412db3a8da7a40b518861a6f524a8ac704021e8451d3cb38746f24243b1e561f4eec07e1575200d06bfd098783d2b7ee7ee07a971aed1c677da6e6' ;; \ - armv7) binArch='armv7'; checksum='118776e879c280556abb7c03ff7c0081eda23c2aee0472aef176f733785e9501defaeaf334cd2443e31294809beafaea831d2e695aa68045160082aa3a966e2f' ;; \ - aarch64) binArch='arm64'; checksum='62252ade5e8dcec13a66154ee1978d959370be049cce52e7c4edefff14ef70bbb21630e3735092719bc3c31214e89dff99e55970ff0adec8ac0a94c6415b059a' ;; \ - ppc64el|ppc64le) binArch='ppc64le'; checksum='65d27fe53f5e4fa79f3476f8902071c907aab74db1a2616342be3714d4252219fbb53b174ef588e20c51e7cfac84376c7a0a608091c2fe83b31dbf59dabeb237' ;; \ - s390x) binArch='s390x'; checksum='c562190962a2db0248a4190616dd2ebaa02df2cf62f1a2c71f9d9de18af2a297df8000a06a11e8d3929dfd64f0c081d1e61961687ca220007459f2dbd0be2c81' ;; \ - *) echo >&2 "error: unsupported architecture ($apkArch)"; exit 1 ;;\ + x86_64) binArch='amd64' ;; \ + armhf) binArch='armv6' ;; \ + armv7) binArch='armv7' ;; \ + aarch64) binArch='arm64' ;; \ + ppc64el|ppc64le) binArch='ppc64le' ;; \ + s390x) binArch='s390x' ;; \ + *) echo >&2 "error: unsupported architecture ($apkArch)"; exit 1 ;;\ esac; \ - wget -O /tmp/caddy.tar.gz "https://github.com/caddyserver/caddy/releases/download/v2.7.6/caddy_2.7.6_linux_${binArch}.tar.gz"; \ - echo "$checksum /tmp/caddy.tar.gz" | sha512sum -c; \ + wget -O /tmp/caddy.tar.gz "https://github.com/caddyserver/caddy/releases/download/v${CADDY_VERSION}/caddy_${CADDY_VERSION}_linux_${binArch}.tar.gz"; \ tar x -z -f /tmp/caddy.tar.gz -C /usr/bin caddy; \ rm -f /tmp/caddy.tar.gz; \ setcap cap_net_bind_service=+ep /usr/bin/caddy; \ chmod +x /usr/bin/caddy; \ caddy version; \ -# docker-php-ext-configure gd --enable-gd --with-jpeg --with-webp --with-freetype; \ -# docker-php-ext-install -j $(nproc) $PHP_EXTENSIONS; \ -# pecl install -o -f redis; \ -# pecl install memcached imagick; \ -# docker-php-ext-enable redis memcached imagick; \ + docker-php-ext-configure gd --enable-gd --with-jpeg --with-webp --with-freetype; \ + docker-php-ext-install -j $(nproc) $PHP_EXTENSIONS; \ + pecl install -o -f redis; \ + pecl install memcached imagick; \ + docker-php-ext-enable redis memcached imagick; \ apk del .build-deps; \ curl --tlsv1 -v -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer; diff --git a/caddy-with-php/config/Caddyfile b/caddy-with-php/normal/config/Caddyfile similarity index 96% rename from caddy-with-php/config/Caddyfile rename to caddy-with-php/normal/config/Caddyfile index 3b22e2c..55149a8 100644 --- a/caddy-with-php/config/Caddyfile +++ b/caddy-with-php/normal/config/Caddyfile @@ -19,7 +19,7 @@ # reverse_proxy localhost:8080 # Or serve a PHP site through php-fpm: - # php_fastcgi localhost:9000 + php_fastcgi localhost:9000 } # Refer to the Caddy docs for more information: diff --git a/caddy-with-php/config/index.html b/caddy-with-php/normal/config/index.html similarity index 100% rename from caddy-with-php/config/index.html rename to caddy-with-php/normal/config/index.html diff --git a/caddy-with-php/entrypoint.sh b/caddy-with-php/normal/entrypoint.sh similarity index 80% rename from caddy-with-php/entrypoint.sh rename to caddy-with-php/normal/entrypoint.sh index 2f1f911..05f3999 100644 --- a/caddy-with-php/entrypoint.sh +++ b/caddy-with-php/normal/entrypoint.sh @@ -2,5 +2,5 @@ set -e # php-fpm daemonize php-fpm -D -# start caddy +# execute cmd exec "$@" \ No newline at end of file diff --git a/caddy-with-php/supervisor/Dockerfile b/caddy-with-php/supervisor/Dockerfile new file mode 100644 index 0000000..1e43e45 --- /dev/null +++ b/caddy-with-php/supervisor/Dockerfile @@ -0,0 +1,122 @@ +FROM caddy:alpine as caddy-build + +FROM php:8.3-fpm-alpine3.19 +LABEL maintainer="rainerosion " + +ENV XDG_CONFIG_HOME /config +ENV XDG_DATA_HOME /data +ENV CADDY_VERSION 2.7.6 + +# build dependencies +ENV BUILD_DEPS \ + libzip-dev \ + icu-dev \ + postgresql-dev \ + libpng-dev \ + libwebp-dev \ + libjpeg-turbo-dev \ + curl-dev \ + sqlite-dev \ + oniguruma-dev \ + libmemcached-dev \ + zlib-dev \ + imagemagick-dev \ + freetype-dev +# runtime dependencies +ENV RUN_DEPS \ + autoconf \ + g++ \ + make \ + libzip \ + icu \ + postgresql-libs \ + libpng \ + libwebp \ + libjpeg-turbo \ + curl \ + sqlite-libs \ + oniguruma \ + libmemcached \ + shadow \ + zlib \ + imagemagick \ + freetype \ + ca-certificates \ + libcap \ + mailcap \ + supervisor +# php extensions +ENV PHP_EXTENSIONS \ + zip \ + intl \ + exif \ + pdo_mysql \ + pdo_pgsql \ + mysqli \ + pgsql \ + gd \ + curl \ + session \ + pdo_sqlite \ + fileinfo \ + mbstring \ + bcmath +# set default uid and gid for www-data +ARG UID=1000 +ARG GUID=1000 +# fpm user +ARG USER=www-data +# web server and php-fpm +RUN set -eux; \ + sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories; \ + apk update; \ + apk add --no-cache $RUN_DEPS; \ + usermod -u $UID -o $USER; \ + groupmod -g $GUID -o $USER; \ + apk add --no-cache --virtual .build-deps $BUILD_DEPS; \ + mkdir -p \ + /config/caddy \ + /data/caddy \ + /etc/caddy \ + /usr/share/caddy \ + /etc/supervisor/conf.d \ + /srv ; \ + wget -O /etc/caddy/Caddyfile "https://p.iblog.site/https://github.com/caddyserver/dist/raw/cd39178d252a610fee6aa8465c787d9c780007a2/config/Caddyfile"; \ + wget -O /usr/share/caddy/index.html "https://p.iblog.site/https://github.com/caddyserver/dist/raw/cd39178d252a610fee6aa8465c787d9c780007a2/welcome/index.html"; \ + echo " /usr/share/caddy/info.php; \ + apkArch="$(apk --print-arch)"; \ + case "$apkArch" in \ + x86_64) binArch='amd64' ;; \ + armhf) binArch='armv6' ;; \ + armv7) binArch='armv7' ;; \ + aarch64) binArch='arm64' ;; \ + ppc64el|ppc64le) binArch='ppc64le' ;; \ + s390x) binArch='s390x' ;; \ + *) echo >&2 "error: unsupported architecture ($apkArch)"; exit 1 ;;\ + esac; \ + wget -O /tmp/caddy.tar.gz "https://github.com/caddyserver/caddy/releases/download/v${CADDY_VERSION}/caddy_${CADDY_VERSION}_linux_${binArch}.tar.gz"; \ + tar x -z -f /tmp/caddy.tar.gz -C /usr/bin caddy; \ + rm -f /tmp/caddy.tar.gz; \ + setcap cap_net_bind_service=+ep /usr/bin/caddy; \ + chmod +x /usr/bin/caddy; \ + caddy version; \ + docker-php-ext-configure gd --enable-gd --with-jpeg --with-webp --with-freetype; \ + docker-php-ext-install -j $(nproc) $PHP_EXTENSIONS; \ + pecl install -o -f redis; \ + pecl install memcached imagick; \ + docker-php-ext-enable redis memcached imagick; \ + apk del .build-deps; \ + curl --tlsv1 -v -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer; + +COPY listener.php /srv/listener.php +COPY supervisord.conf /etc/supervisord.conf +COPY conf /etc/supervisor/conf.d/ + +EXPOSE 80 +EXPOSE 443 +EXPOSE 2019 +EXPOSE 443/udp + +WORKDIR /srv + +CMD ["supervisord","-n","-c","/etc/supervisord.conf"] \ No newline at end of file diff --git a/caddy-with-php/supervisor/conf/caddy.conf b/caddy-with-php/supervisor/conf/caddy.conf new file mode 100644 index 0000000..03822c3 --- /dev/null +++ b/caddy-with-php/supervisor/conf/caddy.conf @@ -0,0 +1,10 @@ +[program:caddy] +command=caddy run --config /etc/caddy/Caddyfile --adapter caddyfile +process_name=%(program_name)s_%(process_num)02d +numprocs=1 +autostart=true +autorestart=true +startsecs=0 +redirect_stderr=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 \ No newline at end of file diff --git a/caddy-with-php/supervisor/conf/listener.conf b/caddy-with-php/supervisor/conf/listener.conf new file mode 100644 index 0000000..3fdeb13 --- /dev/null +++ b/caddy-with-php/supervisor/conf/listener.conf @@ -0,0 +1,7 @@ +[eventlistener:subprocess-stopped] +command=php /srv/listener.php +process_name=%(program_name)s_%(process_num)02d +numprocs=1 +events=PROCESS_STATE_EXITED,PROCESS_STATE_STOPPED,PROCESS_STATE_FATAL +autostart=true +autorestart=unexpected \ No newline at end of file diff --git a/caddy-with-php/supervisor/conf/php-fpm.conf b/caddy-with-php/supervisor/conf/php-fpm.conf new file mode 100644 index 0000000..a0b2d70 --- /dev/null +++ b/caddy-with-php/supervisor/conf/php-fpm.conf @@ -0,0 +1,10 @@ +[program:php-fpm] +command=php-fpm --nodaemonize +process_name=%(program_name)s_%(process_num)02d +numprocs=1 +autostart=true +autorestart=true +startsecs=0 +redirect_stderr=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 \ No newline at end of file diff --git a/caddy-with-php/supervisor/config/Caddyfile b/caddy-with-php/supervisor/config/Caddyfile new file mode 100644 index 0000000..55149a8 --- /dev/null +++ b/caddy-with-php/supervisor/config/Caddyfile @@ -0,0 +1,26 @@ +# The Caddyfile is an easy way to configure your Caddy web server. +# +# Unless the file starts with a global options block, the first +# uncommented line is always the address of your site. +# +# To use your own domain name (with automatic HTTPS), first make +# sure your domain's A/AAAA DNS records are properly pointed to +# this machine's public IP, then replace ":80" below with your +# domain name. + +:80 { + # Set this path to your site's directory. + root * /usr/share/caddy + + # Enable the static file server. + file_server + + # Another common task is to set up a reverse proxy: + # reverse_proxy localhost:8080 + + # Or serve a PHP site through php-fpm: + php_fastcgi localhost:9000 +} + +# Refer to the Caddy docs for more information: +# https://caddyserver.com/docs/caddyfile \ No newline at end of file diff --git a/caddy-with-php/supervisor/config/index.html b/caddy-with-php/supervisor/config/index.html new file mode 100644 index 0000000..d85128a --- /dev/null +++ b/caddy-with-php/supervisor/config/index.html @@ -0,0 +1,394 @@ + + + + Caddy works! + + + + + + + +
+
+
+
+ + + + + + + + + + +

+ Congratulations! + おめでとう! + Felicidades! + 恭喜! + बधाई हो! + Поздравляю! + Вітаю! + 🎊 +

+ +

+ Your web server is working. Now make it work for you. 💪 +

+

+ Caddy is ready to serve your site over HTTPS: +

+
    +
  1. Point your domain's A/AAAA DNS records at this machine.
  2. +
  3. Upload your site's files to /var/www/html. +
  4. + Edit your Caddyfile at /etc/caddy/Caddyfile: +
      +
    1. Replace :80 with your domain name
    2. +
    3. Change the site root to /var/www/html
    4. +
    +
  5. +
  6. Reload the configuration: systemctl reload caddy
  7. +
  8. Visit your site!
  9. +
+

If that worked 🥳

+

+ Awesome! You won't have to look at this slanted page anymore. +

+

+ Remember, Caddy can do a lot more than serve static files. It's also a powerful reverse proxy and + application platform. You can use the Caddyfile to enable any other features you need. Or you could use + Caddy's API to configure it programmatically. +

+

+ Everything you need to know is either in the 📖 Caddy + documentation or the manual for your OS/platform. Have fun! +

+ +

If that didn't work 😶

+

+ It's okay, you can fix it! First check the following things: +

+
    +
  • Service status: systemctl status caddy
  • +
  • Logs: journalctl --no-pager -u caddy
  • +
  • Are your site's files readable by the caddy user and group? ls -la /var/www/html
  • +
  • Is the caddy home directory writeable? ls -la /var/lib/caddy
  • +
  • Ensure your domain's A and/or AAAA records point to your machine's public IP address: + dig example.com
  • +
  • Are your ports 80 and 443 externally reachable, and is Caddy able to bind to them? Check your + firewalls, port forwarding, and other network configuration.
  • +
+

+ WAIT! Before trying again, switch to Let's Encrypt's staging environment to + avoid being accidentally rate limited. Once you + get everything else working, it's safe to switch back. +

+

+ Depending on your DNS provider, it may take a while for the DNS records to propagate. Even when + everything is configured properly, automated challenges to obtain TLS certificates usually take several + seconds, but may take up to several minutes or hours. +

+ If you still need help, we have a great community! First try a search, and if your question is original, go ahead + and ask it! Remember to pay it forward and help others too. 😁 +

+

+ Visit Caddy on: + GitHub + or + Twitter + or + Our Forum +

+
+
+ +
+ © Copyright The Caddy Authors + +
+ The Caddy project is not responsible for the content, disposition, or behavior of this Web + property, which is independently owned and maintained. For inquiries, please contact the site owner or + hosting provider. +
+
+ + + + \ No newline at end of file diff --git a/caddy-with-php/supervisor/listener.php b/caddy-with-php/supervisor/listener.php new file mode 100644 index 0000000..a3656b7 --- /dev/null +++ b/caddy-with-php/supervisor/listener.php @@ -0,0 +1,18 @@ +