【问题标题】:Trouble configuring Laravel with nginx and docker-compose使用 nginx 和 docker-compose 配置 Laravel 时遇到问题
【发布时间】:2022-01-09 04:03:49
【问题描述】:

我在配置我的 Laravel 应用程序以在我的网络服务器上运行时遇到问题。

问题是我收到了 Welcome to nginx!使用浏览器时页面而不是我的 laravel 应用程序。

设置如下

  • 这是在 Ubuntu 20.04 LTS 上运行的服务器
  • nginx服务直接安装在机器上(不使用docker)
  • 我有一个 docker-compose 来运行 3 个容器:postgres(数据库)+ keycloak(身份验证服务器)+ 我的 laravel 应用程序。

Keycloak 和 postgres db 运行了几个月。

Laravel 应用路径 -> /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 配置路径:/etc/nginx/sites-available/www/dev.mycompany.com HTTP 尚未配置,我只是在编写规则以重定向所有无 https 请求之前使用 443 进行测试

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 - 路径: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

非常感谢提前

不是盖尔

【问题讨论】:

  • 为什么会这样:#fastcgi_pass dev.mycompany.be:9000; 被注释掉了? Dockerfile 运行 php-fpm 并公开端口 9000。由于所有容器都在同一台机器上运行,因此 php 容器应该可以通过 localhost 访问 fastcgi_pass localhost:9000
  • 你说得对,我已经像这样更改了我的 nginx 配置,但是 没有任何变化。仍然获得 Nginx 欢迎页面。 fastcgi_pass localhost:9000; #fastcgi_pass unix:/var/run/php/php8.0-fpm.sock;
  • 对 Dockefile 进行了更正 COPY composer.lock composer.json /var/www/dev.mycompany.com WORKDIR /var/www/dev.mycompany.com 更改 /var/www.dev.mycompany.com to /var/www/dev.mycompany.com 但仍然......没有任何变化进入容器我检查了应用程序文件是否在 /var/www/dev .mycompany.com 并且 Laravel 应用程序运行正常。该应用程序已经构建并生成了密钥,...... php artisan 工作正常......
  • 配置更改后,是否重新加载了nginx?
  • 是的,我做到了。但最终还是从头再来。

标签: laravel docker nginx docker-compose


【解决方案1】:

通过从零开始并完成此配置来使其正常工作

laravel 应用程序的 Dockerfile

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 配置

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;
}

码头工人撰写

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 应用程序 .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}"

它之前无法正常工作的原因可能是因为 nginx 配置中缺少 server_name 并且 Dockerfile 在脚本中有错误以正确使用 php 8 ???

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-29
    • 2023-03-17
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-22
    相关资源
    最近更新 更多