Skip to content

Commit

Permalink
- switch from nginx to nginx unit (#379)
Browse files Browse the repository at this point in the history
* - nginx unit on local environment

* - nginx unit on beta/prod environment

* - fix make beta-deploy on deployment

* Update environment/prod/app/nginx-unit.config.json

Co-authored-by: Kamil Piech <[email protected]>

* Update environment/dev/app/nginx-unit.config.json

Co-authored-by: Kamil Piech <[email protected]>

---------

Co-authored-by: Kamil Piech <[email protected]>
  • Loading branch information
mtracz and kamilpiech97 authored Jan 23, 2024
1 parent 553ffc3 commit 9f8af38
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 154 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ build:
@docker compose --file ${DOCKER_COMPOSE_FILE} build --pull

run:
@docker compose --file ${DOCKER_COMPOSE_FILE} up --detach
@docker compose --file ${DOCKER_COMPOSE_FILE} up --detach \
&& sleep 2 && make unit-reload-config

stop:
@docker compose --file ${DOCKER_COMPOSE_FILE} stop
Expand Down Expand Up @@ -61,6 +62,10 @@ dev:
create-test-db:
@docker compose --file ${DOCKER_COMPOSE_FILE} exec ${DOCKER_COMPOSE_DATABASE_CONTAINER} bash -c 'createdb --username=${DATABASE_USERNAME} ${TEST_DATABASE_NAME} &> /dev/null && echo "Created database for tests (${TEST_DATABASE_NAME})." || echo "Database for tests (${TEST_DATABASE_NAME}) exists."'

unit-reload-config:
@echo "Reloading Nginx Unit config..." \
&& docker compose --file ${DOCKER_COMPOSE_FILE} exec ${DOCKER_COMPOSE_APP_CONTAINER} curl -X PUT --data-binary @/application/environment/dev/app/nginx-unit.config.json --unix-socket /var/run/control.unit.sock http://localhost/config

encrypt-beta-env:
@docker compose --file ${DOCKER_COMPOSE_FILE} run \
--rm \
Expand Down
5 changes: 3 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ services:
container_name: toby-app-dev
working_dir: /application
volumes:
- ./environment/dev/app/nginx.conf:/etc/nginx/nginx.conf:ro
- toby-unit-data:/var/lib/unit
- ./environment/dev/app/php.ini:/usr/local/etc/php/conf.d/zzz-overrides.ini:ro
- ./environment/dev/app/php-fpm.conf:/usr/local/etc/php-fpm.d/zzz-overrides.conf:ro
- ./environment/dev/app/supervisord.conf:/etc/supervisor/custom-supervisord.conf:ro
- .:/application
ports:
Expand Down Expand Up @@ -121,3 +120,5 @@ volumes:
name: toby-pgsql-data
toby-redis-data:
name: toby-redis-data
toby-unit-data:
name: toby-unit-data
68 changes: 52 additions & 16 deletions environment/dev/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,54 @@
FROM composer/composer:2.5.8-bin as composer-bin
ARG PHP_VERSION=8.3.0
ARG PHP_MODULE_NAME=php${PHP_VERSION}
# https://github.com/nginx/unit/tags
ARG UNIT_VERSION=1.31.1-1

FROM node:20.8.0-bullseye-slim as node
# https://hub.docker.com/r/composer/composer
FROM composer/composer:2.6.6-bin as composer-bin

FROM php:8.3.0-fpm-bullseye
# https://hub.docker.com/_/node
FROM node:21.4.0-bullseye-slim as node

FROM php:${PHP_VERSION}-cli-bullseye as unit-modules-builder

RUN apt-get update && apt-get install --assume-yes \
git \
libpcre2-dev

ARG UNIT_VERSION
ARG PHP_MODULE_NAME

RUN git clone https://github.com/nginx/unit \
&& cd unit \
&& git checkout ${UNIT_VERSION} \
&& ./configure \
&& ./configure php --module=${PHP_MODULE_NAME} \
&& make ${PHP_MODULE_NAME}

FROM php:${PHP_VERSION}-cli-bullseye as app

ARG UNIT_VERSION
# https://packages.nginx.org/unit/debian/pool/unit/u/unit/
ARG NGINX_UNIT_VERSION="${UNIT_VERSION}~bullseye"
ARG PHP_MODULE_NAME

COPY --from=unit-modules-builder /unit/build/lib/unit/modules/${PHP_MODULE_NAME}.unit.so /usr/lib/unit/modules/${PHP_MODULE_NAME}.unit.so

# Install Nginx Unit
RUN curl --output /usr/share/keyrings/nginx-keyring.gpg https://unit.nginx.org/keys/nginx-keyring.gpg \
&& echo 'deb [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ bullseye unit' | tee --append /etc/apt/sources.list.d/unit.list \
&& echo 'deb-src [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ bullseye unit' | tee --append /etc/apt/sources.list.d/unit.list \
&& apt-get update && apt-get install --assume-yes \
systemd \
unit=${NGINX_UNIT_VERSION} \
# Redirect logs to stdout
&& ln --symbolic --force /dev/stdout /var/log/unit.log

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

ARG USER_NAME=host-user
ARG USER_ID=1000
ARG PHP_FPM_GROUP=www-data
ARG NGINX_UNIT_GROUP=unit

RUN adduser \
--disabled-password \
Expand All @@ -17,10 +57,10 @@ RUN adduser \
&& usermod \
--append \
--groups \
${PHP_FPM_GROUP} \
${NGINX_UNIT_GROUP} \
${USER_NAME}

#Add node and npm
# Add node and npm
COPY --from=node --chown=${USER_NAME}:root /usr/local/lib/node_modules /usr/local/lib/node_modules
COPY --from=node --chown=${USER_NAME}:root /usr/local/bin/node /usr/local/bin/node
RUN ln --symbolic /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm \
Expand All @@ -31,16 +71,10 @@ RUN ln --symbolic /usr/local/lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/
# Use the default development configuration
RUN mv "${PHP_INI_DIR}/php.ini-development" "${PHP_INI_DIR}/php.ini"

# For other versions check: http://nginx.org/packages/mainline/debian/pool/nginx/n/nginx/
ARG NGINX_VERSION="1.25.0-1~bullseye"
ARG PHPREDIS_VERSION=5.3.7
# https://pecl.php.net/package/redis
ARG PHPREDIS_VERSION=6.0.2

RUN apt-get update \
&& apt-get install --assume-yes gpg \
&& curl https://nginx.org/keys/nginx_signing.key | gpg --dearmour --output /etc/apt/trusted.gpg.d/apt.nginx.org.gpg > /dev/null \
&& echo "deb https://nginx.org/packages/mainline/debian bullseye nginx" | tee /etc/apt/sources.list.d/nginx.list \
&& apt-get update && apt-get install --assume-yes \
nginx=${NGINX_VERSION} \
RUN apt-get update && apt-get install --assume-yes \
libpcre3-dev \
gnupg \
zip \
Expand All @@ -64,7 +98,7 @@ RUN curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmour --o
&& apt-get install --assume-yes postgresql-client-15

# For other versions check: https://pecl.php.net/package/xdebug
ARG XDEBUG_VERSION=3.3.0alpha3
ARG XDEBUG_VERSION=3.3.0
ARG INSTALL_XDEBUG=true

RUN if [ ${INSTALL_XDEBUG} = true ]; then \
Expand All @@ -77,3 +111,5 @@ COPY ./entrypoint.sh /entrypoint.sh
WORKDIR /application

ENTRYPOINT ["/entrypoint.sh"]

EXPOSE 80
38 changes: 38 additions & 0 deletions environment/dev/app/nginx-unit.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"listeners": {
"*:80": {
"pass": "routes"
}
},

"routes": [
{
"match": {
"uri": "!/index.php"
},
"action": {
"share": "/application/public$uri",
"fallback": {
"pass": "applications/laravel"
}
}
}
],

"applications": {
"laravel": {
"type": "php 8.3.0",
"root": "/application/public/",
"script": "index.php",
"limits": {
"timeout": 120,
"requests": 1000
},
"processes": {
"max": 10,
"spare": 1,
"idle_timeout": 20
}
}
}
}
44 changes: 0 additions & 44 deletions environment/dev/app/nginx.conf

This file was deleted.

8 changes: 0 additions & 8 deletions environment/dev/app/php-fpm.conf

This file was deleted.

11 changes: 2 additions & 9 deletions environment/dev/app/supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,8 @@ pidfile = /var/run/supervisord.pid
user = root
nodaemon = true

[program:nginx]
command=nginx
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes = 0
stderr_logfile = /dev/stderr
stderr_logfile_maxbytes = 0

[program:php-fpm]
command = php-fpm
[program:unitd]
command = unitd --no-daemon --control unix:/var/run/control.unit.sock
stdout_logfile = /dev/stdout
stdout_logfile_maxbytes = 0
stderr_logfile = /dev/stderr
Expand Down
63 changes: 49 additions & 14 deletions environment/prod/app/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
ARG PHP_VERSION=8.3.0
ARG PHP_MODULE_NAME=php${PHP_VERSION}
# https://github.com/nginx/unit/tags
ARG UNIT_VERSION=1.31.1-1

### PHP DEPENDENCIES ###
FROM composer:2.5.8 as vendor
FROM composer:2.6.6 as vendor

WORKDIR /app_composer_dependencies

Expand All @@ -14,7 +19,7 @@ RUN composer install \
--ignore-platform-reqs

### FRONTEND ###
FROM node:20.8.0-alpine3.17 as frontend
FROM node:21.4.0-bullseye-slim as frontend

WORKDIR /app_frontend_dependencies

Expand All @@ -27,25 +32,56 @@ COPY resources/css ./resources/css/

RUN npm run build

### NGINIX UNIT PHP MODULE ###
FROM php:${PHP_VERSION}-cli-bullseye as unit-modules-builder

RUN apt-get update && apt-get install --assume-yes \
git \
libpcre2-dev

ARG UNIT_VERSION
ARG PHP_MODULE_NAME

RUN git clone https://github.com/nginx/unit \
&& cd unit \
&& git checkout ${UNIT_VERSION} \
&& ./configure \
&& ./configure php --module=${PHP_MODULE_NAME} \
&& make ${PHP_MODULE_NAME}


### APPLICATION ###
FROM php:8.3.0-fpm-bullseye as app
FROM php:${PHP_VERSION}-cli-bullseye as app

ARG UNIT_VERSION
# https://packages.nginx.org/unit/debian/pool/unit/u/unit/
ARG NGINX_UNIT_VERSION="${UNIT_VERSION}~bullseye"
ARG PHP_MODULE_NAME

COPY --from=unit-modules-builder /unit/build/lib/unit/modules/${PHP_MODULE_NAME}.unit.so /usr/lib/unit/modules/${PHP_MODULE_NAME}.unit.so

# Install Nginx Unit
RUN curl --output /usr/share/keyrings/nginx-keyring.gpg https://unit.nginx.org/keys/nginx-keyring.gpg \
&& echo 'deb [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ bullseye unit' | tee --append /etc/apt/sources.list.d/unit.list \
&& echo 'deb-src [signed-by=/usr/share/keyrings/nginx-keyring.gpg] https://packages.nginx.org/unit/debian/ bullseye unit' | tee --append /etc/apt/sources.list.d/unit.list \
&& apt-get update && apt-get install --assume-yes \
systemd \
unit=${NGINX_UNIT_VERSION} \
# Redirect logs to stdout
&& ln --symbolic --force /dev/stdout /var/log/unit.log

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

# For other versions check: http://nginx.org/packages/mainline/debian/pool/nginx/n/nginx/
ARG NGINX_VERSION="1.25.0-1~bullseye"
# Install Postgresql client (https://www.postgresql.org/download/linux/debian/)
ARG POSTGRESQL_CLIENT_VERSION=15
ARG PHPREDIS_VERSION=5.3.7
# https://pecl.php.net/package/redis
ARG PHPREDIS_VERSION=6.0.2

RUN apt-get update \
&& apt-get install --assume-yes gpg \
&& curl https://nginx.org/keys/nginx_signing.key | gpg --dearmour --output /etc/apt/trusted.gpg.d/apt.nginx.org.gpg > /dev/null \
&& echo "deb https://nginx.org/packages/mainline/debian bullseye nginx" | tee /etc/apt/sources.list.d/nginx.list \
&& curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmour --output /etc/apt/trusted.gpg.d/apt.postgresql.org.gpg >/dev/null \
&& echo "deb http://apt.postgresql.org/pub/repos/apt bullseye-pgdg main" | tee /etc/apt/sources.list.d/pgdg.list \
&& apt-get update && apt-get install --assume-yes \
nginx=${NGINX_VERSION} \
postgresql-client-${POSTGRESQL_CLIENT_VERSION} \
libpcre3-dev \
libpq-dev \
Expand All @@ -68,21 +104,20 @@ RUN apt-get update \

RUN mv "${PHP_INI_DIR}/php.ini-production" "${PHP_INI_DIR}/php.ini"
COPY ./environment/prod/app/php.ini "${PHP_INI_DIR}/conf.d/zzz-overrides-php.ini"
COPY ./environment/prod/app/php-fpm.conf "/usr/local/etc/php-fpm.d/zzz-overrides.conf"
COPY ./environment/prod/app/nginx.conf "/etc/nginx/nginx.conf"
COPY ./environment/prod/app/supervisord.conf "/etc/supervisor/custom-supervisord.conf"
COPY ./environment/prod/app/entrypoint.sh /entrypoint.sh
COPY ./environment/prod/app/nginx-unit.config.json /var/lib/unit/conf.json

WORKDIR /application

# Copy Frontend build
COPY --chown=www-data:www-data --from=frontend /app_frontend_dependencies/public/build ./public/build/
COPY --chown=unit:unit --from=frontend /app_frontend_dependencies/public/build ./public/build/

# Copy Composer dependencies
COPY --chown=www-data:www-data --from=vendor /app_composer_dependencies/vendor ./vendor/
COPY --chown=unit:unit --from=vendor /app_composer_dependencies/vendor ./vendor/

# Copy App files
COPY --chown=www-data:www-data . .
COPY --chown=unit:unit . .

RUN composer dump-autoload --optimize

Expand Down
Loading

0 comments on commit 9f8af38

Please sign in to comment.