Home > front end >  Trouble configuring Laravel with nginx and docker-compose
Trouble configuring Laravel with nginx and docker-compose

Time:12-08

I'm having trouble to config my Laravel application to run on my web server.

The problem is that I'm getting the Welcome to nginx! page and not my laravel app when using the browser.

The set up is the following

  • It's a server running on Ubuntu 20.04 LTS
  • The nginx service is installed directly on the machine (not using docker)
  • I have a docker-compose to run 3 containers: postgres (database) keycloak (auth server) My laravel app.

Keycloak and the postgres db are running like charms for months.

Laravel app path -> /var/www/dev.mycompany.com/

Laravel Dockerfile -> /var/www/dev.mycompany.com/Dockerfile

FROM php:8.0-fpm

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www.dev.mycompany.com

# Set working directory
WORKDIR /var/www.dev.mycompany.com

# Install dependencies
RUN apt update && apt install -y \
    build-essential \
    libpng-dev \
    libjpeg62-turbo-dev \
    libfreetype6-dev \
    locales \
    zip \
    jpegoptim optipng pngquant gifsicle \
    vim \
    unzip \
    git \
    curl

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

# Install extensions
RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
RUN docker-php-ext-install gd

# Install composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

# Add user for laravel application
RUN groupadd -g 1000 www
RUN useradd -u 1000 -ms /bin/bash -g www www

# Copy existing application directory contents
COPY . /var/www

# Copy existing application directory permissions
COPY --chown=www:www . /var/www.dev.mycompany.com

# Change current user to www
USER www

# Expose port 9000 and start php-fpm server
EXPOSE 9000
CMD ["php-fpm"]

Nginx - Laravel Config path: /etc/nginx/sites-available/www/dev.mycompany.com HTTP is not configured yet, I was just testing with 443 before writing a rule to redirect all none https requests

server {
    listen 443;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/dev.mycompany.com;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(. \.php)(/. )$;
        #fastcgi_pass dev.mycompany.com:9000;
        fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

docker-compose.yml - path: srv/

version: '3.1'

volumes:
  postgres_data:
      driver: local

services:
  postgres:
      image: postgres:latest
      restart: unless-stopped
      volumes:
        - postgres_data:/var/lib/postgresql/data
      environment:
        POSTGRES_DB: keycloak
        POSTGRES_USER: **********************
        POSTGRES_PASSWORD: **********************
      networks:
        - app-network
  keycloak:
      image: jboss/keycloak:latest
      restart: unless-stopped
      environment:
        DB_VENDOR: POSTGRES
        DB_ADDR: postgres
        DB_DATABASE: keycloak
        DB_USER: **********************
        DB_PASSWORD: **********************
        KEYCLOAK_USER: ************
        KEYCLOAK_PASSWORD: **********************
        PROXY_ADDRESS_FORWARDING: "true"
      ports:
        - 8080:8080
      depends_on:
        - postgres
      networks:
        - app-network
  app-backend:
      image: digitalocean.com/php
      build:
        context: .
        dockerfile: /var/www/dev.mycompany.com/Dockerfile
      restart: unless-stopped
      container_name: web-backend
      tty: true
      environment:
        SERVICE_NAME: app
        SERICE_TAGS: dev
      working_dir: /var/www/dev.mycompany.com
      volumes:
        - /var/www/dev.mycompany.com:/var/www/dev.mycompany.com
        - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
      networks:
        - app-network
networks:
  app-network:
    driver: bridge

Thanks A LOT by advance

NotGael

CodePudding user response:

Got it working by starting all over to zero and finishing with this config

Dockerfile for the laravel app

FROM php:7.4-fpm

# Arguments defined in docker-compose.yml ARG user ARG uid

# Install system dependencies RUN apt-get update && apt-get install -y \
    git \
    curl \
    libpng-dev \
    libonig-dev \
    libxml2-dev \
    zip \
    unzip

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

# Install PHP extensions RUN docker-php-ext-install pdo_mysql mbstring exif pcntl bcmath gd

# Get latest Composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer

# Create system user to run Composer and Artisan Commands RUN useradd -G www-data,root -u $uid -d /home/$user $user RUN mkdir -p /home/$user/.composer && \
    chown -R $user:$user /home/$user

# Set working directory WORKDIR /var/www/dev.mycompany.com

USER $user

Nginx config

server {
    listen 80;
    server_name dev.mycompany.com;
    root /var/www/dev.mycompany.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(. \.php)(/. )$;
        fastcgi_pass localhost: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_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

server {
    listen 443;
    server_name dev.mycompany.com;
    root /var/www/dev.mycompany.com/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }

    error_page 404 /index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(. \.php)(/. )$;
        fastcgi_pass localhost: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_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

docker-compose

version: "3.7"

services:
  postgres:
    image: postgres:latest
    restart: unless-stopped
    environment:
      POSTGRES_DB: ****************
      POSTGRES_USER: ****************
      POSTGRES_PASSWORD: ****************
    ports:
      - 5432:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app-network

  keycloak:
    image: jboss/keycloak:latest
    restart: unless-stopped
    environment:
      DB_VENDOR: POSTGRES
      DB_ADDR: postgres
      DB_DATABASE: ****************
      DB_USER: ****************
      DB_PASSWORD: ****************
      KEYCLOAK_USER: ****************
      KEYCLOAK_PASSWORD: ****************
      PROXY_ADDRESS_FORWARDING: "true"
    ports:
      - 8080:8080
    depends_on:
      - postgres
    networks:
      - app-network

  postgres-web:
    image: postgres:latest
    restart: unless-stopped
    environment:
      POSTGRES_DB: ****************
      POSTGRES_USER: ****************
      POSTGRES_PASSWORD: ****************
    ports:
      - 5433:5432
    volumes:
      - postgres_data:/var/lib/postgresql/data
    networks:
      - app-network

  web-backend:
    build:
      args:
        user: ****************
        uid: 1000
      context: ./
      dockerfile: /var/www/dev.mycompany.com/Dockerfile
    image: web-backend
    restart: unless-stopped
    working_dir: /var/www/dev.mycompany.com/
    ports:
      - 9000:9000
    volumes:
      - /var/www/dev.mycompany.com:/var/www/dev.mycompany.com
    networks:
      - app-network

volumes:
  postgres_data:
    driver: local

networks:
  app-network:
    driver: bridge

Laravel app .env

APP_NAME=web-backend
APP_ENV=prod
APP_KEY=
APP_DEBUG=false
APP_URL=http://dev.mycompany.com

LOG_CHANNEL=stack
LOG_DEPRECATIONS_CHANNEL=null
LOG_LEVEL=debug

DB_CONNECTION=pgsql
DB_HOST=127.0.0.1
DB_PORT=5433
DB_DATABASE=****************
DB_USERNAME=****************
DB_PASSWORD=****************

BROADCAST_DRIVER=log
CACHE_DRIVER=file
FILESYSTEM_DRIVER=local
QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MAIL_MAILER=smtp
MAIL_HOST=mailhog
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"

AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_DEFAULT_REGION=us-east-1
AWS_BUCKET=
AWS_USE_PATH_STYLE_ENDPOINT=false

PUSHER_APP_ID=
PUSHER_APP_KEY=
PUSHER_APP_SECRET=
PUSHER_APP_CLUSTER=mt1

MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"

The reason it was not working before maybe because the server_name was missing in the nginx config and the Dockerfile had error in the script to correctly work with php 8 ???

  • Related