Deploy Laravel with PHP-FPM and NGINX

Hi,
I just started using Qovery and I need to deploy a Laravel project. I have issues deploying the project using PHP-FPM and nginx.

In local development, I use docker-compose to share a volume with php-fpm:

version: "3"
services:
  api-doorin-php:
    restart: always
    build:
      context: .
      dockerfile: Dockerfile
    env_file:
        - .env
    environment:
        - DB_HOST=host.docker.internal
    volumes:
        - volume-doorin:/app
  api-doorin-nginx:
    restart: always
    image: nginx:latest
    ports:
        - "8000:80"
    volumes:
        - volume-doorin:/app
        - ./nginx-local.conf:/etc/nginx/conf.d/default.conf
    depends_on:
        - api-doorin-php
  api-doorin-queue:
    restart: always
    build:
      context: .
      dockerfile: Dockerfile
    env_file:
        - .env
    environment:
        - DB_HOST=host.docker.internal
    command: php artisan queue:work --queue=fileUpload,default
    depends_on:
        - api-doorin-php
    volumes:
        - volume-doorin:/app
    links:
        - api-doorin-php
volumes:
  volume-doorin:

My Dockerfile is like this:

# Base image
FROM php:8.1-fpm

# Install system dependencies
RUN apt-get update && apt-get install -y \
        git \
        curl \
        libpng-dev \
        libonig-dev \
        libxml2-dev \
        zip \
        unzip \
        libzip-dev \
        tzdata \
        libmagickwand-dev --no-install-recommends \
        locales \
        locales-all \
        libreoffice \
        ghostscript

# Install PHP extensions
RUN docker-php-ext-install exif gd intl mysqli pdo_mysql zip bcmath calendar gettext \
        && docker-php-ext-enable exif gd intl mysqli pdo_mysql zip bcmath calendar gettext

# Install Imagick
RUN pecl install imagick \
    && docker-php-ext-enable imagick

# Allow PDF to image conversion in Imagick
RUN sed -i '92 a<!--' /etc/ImageMagick-6/policy.xml \
    && sed -i '98 a/-->' /etc/ImageMagick-6/policy.xml

# Install xdebug
RUN pecl install xdebug-3.1.5 \
        && docker-php-ext-enable xdebug

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Application setup
COPY . /var/www/html
RUN usermod -u 1000 www-data \
    && chown -R www-data:www-data /var/www/html
WORKDIR /var/www/html

# Environment configuration
ENV TZ=Europe/Paris \
    LC_ALL=fr_FR.utf8 \
    LANG=fr_FR.utf8 \
    LANGUAGE=fr_FR.utf8

RUN rm /etc/localtime && echo $TZ > /etc/localtime

# Set PHP configuration
COPY ./confphp.ini /usr/local/etc/php/conf.d/confphp.ini

# Install composer
RUN php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/local/bin --filename=composer \
        && chmod +x /usr/local/bin/composer

# Install dependencies
RUN touch /var/www/html/storage/logs/laravel.log \
    && mkdir -p /var/www/html/storage/framework/cache/data \
    && composer install --optimize-autoloader --no-dev \
    && composer dump-autoload \
    && php artisan optimize:clear \
    && chown -R www-data:www-data /var/www/html/storage \
    && chmod -R 755 /var/www/html/storage \
    && chown -R www-data:www-data /var/www/html/storage/framework/cache/data \
    && chmod -R 755 /var/www/html/storage/framework/cache/data \
    && chgrp -R www-data storage bootstrap/cache \
    && chmod -R ug+rwx storage bootstrap/cache

EXPOSE 9000

CMD ["php-fpm", "-F"]

I managed to build and deploy the app for the php-fpm and the laravel queue parts of the docker-compose. But I didn’t manage to run the nginx part.

I tried to build it with a new Dockerfile:

FROM nginx:1.25.3-alpine

# Copy Nginx configuration
COPY nginx.conf /etc/nginx/conf.d/default.conf

# Expose Nginx port
EXPOSE 80

# Start Nginx
CMD ["nginx", "-g", "daemon off;"]

But it is not connected to the PHP code from the other Dockerfile (in the docker-compose it is connected via a volume).

I tried to update the nginx.conf file that I use with docker-compose:

server {
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /app/public;
    
    client_max_body_size 0;
    client_body_buffer_size 128k;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;
    output_buffers 1 32k;
    postpone_output 1460;
    
    index index.php;

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

    location ~ \.php {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass api-doorin-php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ \.php$ {
        return 404;
    }
}

In local, I have fastcgi_pass api-doorin-php:9000; which is the shared volume that I need to update to “connect” to the php-fpm container exposing on port 9000.

Another solution that I tried is to used a multi-stage Dockerfile with PHP-FPM and then NGINX stages but I did not manage to make it work because I need both the PHP-FPM and the NGINX to run.

I am currently trying to connect a NGINX container to a PHP-FPM container in local but maybe it is not the right way to do it.

Thanks a lot.

Hello @Thibault_Chatelain

I think you need to follow step 4 of this documentation to expose your service publicly. You can find the details in the following link

I hope it will help

I managed to make my laravel project work with a single Dockerfile by simply adding nginx to my php-fpm container and run both php-fpm and nginx at the same time. It is not perfect but it is the only way that I managed to make it run on Qovery.

# Build Stage
FROM php:8.1-fpm AS builder

# Install system dependencies
RUN apt-get update && apt-get install -y \
        git \
        curl \
        libpng-dev \
        libonig-dev \
        libxml2-dev \
        zip \
        unzip \
        libzip-dev \
        tzdata \
        libmagickwand-dev --no-install-recommends \
        locales \
        locales-all \
        libreoffice \
        ghostscript \
        nginx
        
# Install PHP extensions
RUN docker-php-ext-install exif gd intl mysqli pdo_mysql zip bcmath calendar gettext sockets \
        && docker-php-ext-enable exif gd intl mysqli pdo_mysql zip bcmath calendar gettext

# Install Imagick
RUN pecl install imagick \
    && docker-php-ext-enable imagick

# Allow PDF to image conversion in Imagick
RUN sed -i '92 a<!--' /etc/ImageMagick-6/policy.xml \
    && sed -i '98 a/-->' /etc/ImageMagick-6/policy.xml

# Install xdebug
RUN pecl install xdebug-3.1.5 \
        && docker-php-ext-enable xdebug

# Clear cache
RUN apt-get clean && rm -rf /var/lib/apt/lists/*

# Setup node js source
RUN curl -sL https://deb.nodesource.com/setup_16.x -o nodesource_setup.sh
RUN ["sh",  "./nodesource_setup.sh"]

# Application setup
COPY . /app
RUN usermod -u 1000 www-data \
    && chown -R www-data:www-data /app
WORKDIR /app

# Environment configuration
ENV TZ=Europe/Paris \
    LC_ALL=fr_FR.utf8 \
    LANG=fr_FR.utf8 \
    LANGUAGE=fr_FR.utf8

RUN rm /etc/localtime && echo $TZ > /etc/localtime

# Set PHP configuration
COPY ./confphp.ini /usr/local/etc/php/conf.d/confphp.ini

COPY ./nginx.conf /etc/nginx/nginx.conf

# Install composer
RUN php -r "readfile('https://getcomposer.org/installer');" | php -- --install-dir=/usr/local/bin --filename=composer \
        && chmod +x /usr/local/bin/composer

# Install dependencies
RUN touch /app/storage/logs/laravel.log \
    && mkdir -p /app/storage/framework/cache/data \
    && composer install --optimize-autoloader --no-dev \
    && composer dump-autoload \
    && php artisan optimize:clear \
    && chown -R www-data:www-data /app/storage \
    && chmod -R 755 /app/storage \
    && chown -R www-data:www-data /app/storage/framework/cache/data \
    && chmod -R 755 /app/storage/framework/cache/data \
    && chgrp -R www-data storage bootstrap/cache \
    && chmod -R ug+rwx storage bootstrap/cache

EXPOSE 80

RUN chmod +x docker/entrypoint.sh

CMD ["sh", "docker/entrypoint.sh"]
#!/bin/bash
# migrate databases
php artisan migrate
# start processes
php-fpm -D &&  nginx -g "daemon off;"

To make it work properly with two containers, in local we use either shared volume or shared network. I am working on a solution to add an env variable PHP_FPM_IP to the nginx conf and to pass the IP of the php-fpm container to the nginx conf.

fastcgi_pass ${PHP_FPM_IP}:9000;

Okay, I managed to make it work with 2 containers (nginx + php-fpm).

The first thing that I missed is that the php-fpm container should expose the port 9000 as TCP.

Then I have this nginx.conf

server {
    listen 80;
    server_name localhost;

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    
    root /app/public;
        
    client_max_body_size 0;
    client_body_buffer_size 128k;
    client_header_buffer_size 1k;
    large_client_header_buffers 4 4k;
    output_buffers 1 32k;
    postpone_output 1460;
    
    index index.php;

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

    location ~ \.php {
        include fastcgi_params;
        fastcgi_pass ${PHP_FPM_IP}:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    location ~ \.php$ {
        return 404;
    }
}

The ${PHP_FPM_IP} variable should come from your service variables.
You need to set an alias for php-fpm container host internal (looks like this app-xxxxxxxxx-php-fpm). So basically you fall to a similar configuration of docker-compose (just the name of the container is dynamic).

Also note that the PHP_FPM_IP variable has to be set on the nginx container so you need to copy it as a template COPY ./nginx.conf etc/nginx/templates/default.conf.template

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.