Skip to content

Commit

Permalink
Moved from PHP-FPM to Octane + Swoole
Browse files Browse the repository at this point in the history
  • Loading branch information
VampireAotD committed Jun 13, 2024
1 parent 5c2756c commit fe5757d
Show file tree
Hide file tree
Showing 29 changed files with 679 additions and 157 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ APP_PORT=9000
XDEBUG_MODE=coverage

SERVER_PORT=80
NGINX_LOGS_TO_LOGSTASH=false

NODE_PORT=5173

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/frontend-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
node-version: [ 20.x ]
pnpm-version: [ 9.2 ]
pnpm-version: [ 9.3 ]

steps:
- uses: actions/checkout@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
node-version: [ 20.x ]
pnpm-version: [ 9.2 ]
pnpm-version: [ 9.3 ]

steps:
- uses: actions/checkout@v4
Expand Down
56 changes: 12 additions & 44 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
compose := $(shell command -v docker-compose || echo docker compose)
frontend := $(compose) exec node pnpm
frontend := $(compose) exec app pnpm

.PHONY: help
help:
Expand All @@ -13,54 +13,27 @@ install: ## Create .env files, install frontend and backend dependencies, build
.PHONY: up
up: ## Start all containers.
$(compose) up -d
@make supervisor

.PHONY: down
down: ## Shut down all containers and remove orphans.
$(compose) down --remove-orphans

.PHONY: build
build: ## Build images.
$(compose) up -d --build

.PHONY: supervisor
supervisor: ## Launch supervisor. Use is to manage Horizon, scheduler, Telegram long polling.
$(compose) exec -d app supervisord -c /etc/supervisor/supervisord.conf

.PHONY: scheduler
scheduler: ## Launch Laravel scheduler.
$(compose) exec -d app ./artisan schedule:work

.PHONY: horizon
horizon: ## Launch Laravel Horizon. Horizon is used to launch and manage queues.
$(compose) exec -d app ./artisan horizon

.PHONY: horizon-pause
horizon-pause: ## Pause Laravel Horizon.
$(compose) exec -d app ./artisan horizon:pause
.PHONY: down
down: ## Shut down all containers and remove orphans.
$(compose) down --remove-orphans

.PHONY: horizon-continue
horizon-continue: ## Resume Laravel Horizon.
$(compose) exec -d app ./artisan horizon:continue
.PHONY: octane-status
octane-status: ## Check Laravel Octane status.
$(compose) exec app ./artisan octane:status

.PHONY: horizon-status
horizon-status: ## Check Laravel Horizon status.
$(compose) exec app ./artisan horizon:status

.PHONY: horizon-terminate
horizon-terminate: ## Terminate Laravel Horizon.
$(compose) exec -d app ./artisan horizon:terminate

.PHONY: app-sh
app-sh: ## Enter app container.
$(compose) exec app sh

.PHONY: test
test: ## Run backend tests.
@make test-db-up
$(compose) exec app ./artisan test
@make test-db-down

.PHONY: psalm
psalm: ## Run Psalm.
$(compose) exec app vendor/bin/psalm
Expand All @@ -78,13 +51,6 @@ optimize: ## Optimize Laravel app.
$(compose) exec app ./artisan optimize:clear;
$(compose) exec app ./artisan optimize;

.PHONY: ide-helper
ide-helper: ## Generate Laravel IDE helpers.
$(compose) exec app ./artisan ide-helper:generate;
$(compose) exec app ./artisan ide-helper:meta;
$(compose) exec app ./artisan ide-helper:models -M;
$(compose) exec app ./artisan ide-helper:eloquent

.PHONY: test-db-up
test-db-up: ## Start testing database.
$(compose) -f compose.testing.yml up --build -d
Expand All @@ -93,9 +59,11 @@ test-db-up: ## Start testing database.
test-db-down: ## Shut down testing database.
$(compose) -f compose.testing.yml down

.PHONY: backup
backup: ## Create database backup.
./scripts/mysql/backup.sh
.PHONY: test
test: ## Run backend tests.
@make test-db-up
$(compose) exec app ./artisan test
@make test-db-down

.PHONY: frontend-watch
frontend-watch: ## Start frontend dev server.
Expand Down
31 changes: 8 additions & 23 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,28 +37,15 @@ services:
networks:
- local

node:
build:
context: docker/node
nginx:
depends_on:
app:
condition: service_started
container_name: anilibrary-node
tty: true
volumes:
- ./src:/app
ports:
- ${NODE_PORT}:5173
networks:
- local

nginx:
condition: service_healthy
build:
context: docker/nginx
depends_on:
app:
condition: service_started
container_name: anilibrary-nginx
environment:
NGINX_LOGS_TO_LOGSTASH: ${NGINX_LOGS_TO_LOGSTASH}
volumes:
- ./src/public:/anilibrary/public
ports:
Expand All @@ -68,24 +55,22 @@ services:
- anilibrary

app:
build:
context: docker/php
depends_on:
database:
condition: service_healthy
redis:
condition: service_healthy
build:
context: docker/php
container_name: anilibrary
tty: true
environment:
XDEBUG_MODE: ${XDEBUG_MODE}
PHP_OPCACHE_VALIDATE_TIMESTAMPS: 1
extra_hosts:
- "host.docker.internal:host-gateway"
- "host.docker.internal:host-gateway" # for xdebug
volumes:
- ./src:/anilibrary
ports:
- ${APP_PORT}:9000
- ${NODE_PORT}:5173
networks:
- local
- anilibrary
Expand Down
43 changes: 29 additions & 14 deletions docker/nginx/conf.d/default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,20 @@ server {
index index.php;
charset utf-8;

resolver 127.0.0.11;

location /index.php {
try_files /not_exists @octane;
}

location / {
try_files $uri $uri/ /index.php?$query_string;
try_files $uri $uri/ @octane;
}

error_page 404 /index.php;
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }

location ~ \.php$ {
try_files $uri =404;
fastcgi_buffers 16 32k;
fastcgi_buffer_size 64k;
fastcgi_busy_buffers_size 64k;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass anilibrary:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_path_info;
}
error_page 404 /index.php;

location ~ /\.(?!well-known).* {
deny all;
Expand All @@ -39,4 +35,23 @@ server {
location ~* (?:#.*#|\.(?:bak|conf|dist|fla|in[ci]|log|orig|psd|sh|sql|sw[op])|~)$ {
deny all;
}

location @octane {
set $suffix "";

if ($uri = /index.php) {
set $suffix ?$query_string;
}

proxy_http_version 1.1;
proxy_set_header Host $http_host;
proxy_set_header Scheme $scheme;
proxy_set_header SERVER_PORT $server_port;
proxy_set_header REMOTE_ADDR $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;

proxy_pass http://anilibrary:8000$suffix;
}
}
4 changes: 2 additions & 2 deletions docker/nginx/docker-entrypoint.d/logs-to-logstash.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env sh

set -e
set -eu

if [ "$APP_ENV" = "production" ]; then
if [ "$NGINX_LOGS_TO_LOGSTASH" = "true" ]; then
sed -i "s|access_log.*|access_log syslog:server=$LOGSTASH_URL logstash;|g" /etc/nginx/conf.d/default.conf
sed -i "s|error_log.*|error_log syslog:server=$LOGSTASH_URL notice;|g" /etc/nginx/conf.d/default.conf
fi
38 changes: 38 additions & 0 deletions docker/nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,49 @@ http {
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;

send_timeout 10;
large_client_header_buffers 4 8k;
client_body_timeout 12;
client_header_timeout 12;
client_header_buffer_size 1k;
client_max_body_size 128m;

open_file_cache max=1000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;

gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_types
text/css
text/javascript
text/xml
text/plain
text/x-component
application/javascript
application/x-javascript
application/json
application/xml
application/rss+xml
application/atom+xml
font/truetype
font/opentype
application/vnd.ms-fontobject
image/svg+xml;

log_format logstash '$remote_addr - $remote_user [$time_local] "$host" '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';

map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}

include /etc/nginx/conf.d/*.conf;
}
15 changes: 0 additions & 15 deletions docker/node/Dockerfile

This file was deleted.

62 changes: 49 additions & 13 deletions docker/php/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,26 +1,62 @@
# syntax=docker/dockerfile:1

ARG PHP_VERSION=8.3
ARG COMPOSER_VERSION=2.7.7
ARG NODE_VERSION=20.14
ARG PNPM_VERSION=9.3.0

FROM php:${PHP_VERSION}-fpm-alpine
FROM node:${NODE_VERSION}-alpine AS node

RUN --mount=type=cache,target=/root/.npm \
npm install -g pnpm@${PNPM_VERSION}

FROM composer:${COMPOSER_VERSION} as composer

FROM php:${PHP_VERSION}-cli-alpine

ARG GROUP_ID=1000
ARG USER_ID=1000

RUN apk add --no-cache ${PHPIZE_DEPS} libzip-dev zip unzip git curl gmp-dev supervisor linux-headers \
&& pecl install xdebug \
&& docker-php-ext-install -j$(nproc) pdo_mysql zip exif pcntl bcmath gmp opcache \
&& docker-php-ext-enable xdebug \
&& addgroup -g ${GROUP_ID} anilibrary \
&& adduser -u ${USER_ID} -D -S -G anilibrary anilibrary \
&& apk del linux-headers ${PHPIZE_DEPS}
RUN apk update \
&& apk add --no-cache libstdc++ libpq libzip-dev gmp-dev oniguruma-dev curl git zip unzip supervisor \
&& apk add --no-cache --virtual .build-deps $PHPIZE_DEPS linux-headers brotli-dev pcre-dev pcre2-dev zlib-dev \
&& pecl install xdebug swoole \
&& docker-php-ext-install -j$(nproc) \
pcntl \
mbstring \
bcmath \
sockets \
opcache \
exif \
pdo_mysql \
zip \
gmp \
&& docker-php-ext-enable xdebug swoole \
&& docker-php-source delete \
&& rm -rf /var/cache/apk/* /tmp/* /var/tmp/* \
&& apk del .build-deps

RUN addgroup -g ${GROUP_ID} anilibrary \
&& adduser -u ${USER_ID} -D -S -G anilibrary anilibrary

USER anilibrary

COPY ./conf.d /usr/local/etc/php/conf.d
COPY /supervisor/conf.d /etc/supervisor/conf.d
COPY /supervisor/supervisord.conf /etc/supervisor/supervisord.conf
COPY ./php.ini /usr/local/etc/php/php.ini
COPY ./supervisor/conf.d /etc/supervisor/conf.d
COPY ./supervisor/supervisord.conf /etc/supervisor/supervisord.conf

COPY --from=composer /usr/bin/composer /usr/local/bin/composer

USER anilibrary
COPY --from=node /usr/lib /usr/lib
COPY --from=node /usr/local/lib /usr/local/lib
COPY --from=node /usr/local/include /usr/local/include
COPY --from=node /usr/local/bin /usr/local/bin

WORKDIR /anilibrary

EXPOSE 8000
EXPOSE 5173

ENTRYPOINT ["supervisord", "-c", "/etc/supervisor/supervisord.conf"]

WORKDIR /anilibrary
HEALTHCHECK --start-period=5s --interval=2s --timeout=5s --retries=8 CMD php artisan octane:status || exit 1
Loading

0 comments on commit fe5757d

Please sign in to comment.