Docker的Laravel项目

时间:2019-09-15 17:01:38

标签: laravel docker nginx docker-compose

我遵循本教程:

当我运行命令“ docker-compose up -d”并在浏览器中打开时,出现此错误消息“无法连接”。 我认为问题出在nginx(laravel.conf)的配置之外 我尝试仅对HTTP应用一些测试,但我无法使其正常工作。 预先感谢

[DockerFile]

# Set master image
FROM php:7.2-fpm-alpine

# Copy composer.lock and composer.json
COPY composer.lock composer.json /var/www/html/

# Set working directory
WORKDIR /var/www/html

# Install Additional dependencies
RUN apk update && apk add --no-cache \
    build-base shadow vim curl \
    php7 \
    php7-fpm \
    php7-common \
    php7-pdo \
    php7-pdo_mysql \
    php7-mysqli \
    php7-mcrypt \
    php7-mbstring \
    php7-xml \
    php7-openssl \
    php7-json \
    php7-phar \
    php7-zip \
    php7-gd \
    php7-dom \
    php7-session \
    php7-zlib

# Add and Enable PHP-PDO Extenstions
RUN docker-php-ext-install pdo pdo_mysql
RUN docker-php-ext-enable pdo_mysql

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

# Remove Cache
RUN rm -rf /var/cache/apk/*

# Add UID '1000' to www-data
RUN usermod -u 1000 www-data

# Copy existing application directory permissions
COPY --chown=www-data:www-data . /var/www/html

# Change current user to www
USER www-data

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

[docker-compose]

version: '3'
services:

  #Laravel App
  app:
    build:
      context: .
      dockerfile: Dockerfile
    image: hakase-labs/laravel
    container_name: app
    restart: unless-stopped
    tty: true
    environment:
      SERVICE_NAME: app
      SERVICE_TAGS: dev
    working_dir: /var/www/html
    volumes:
      - ./:/var/www/html
    networks:
      - mynet

  #Nginx Service
  nginx:
    image: nginx:alpine
    container_name: nginx
    restart: unless-stopped
    tty: true
    ports:
      - "8081:80"
      - "8082:443"
    volumes:
      - ./:/var/www/html
      - ./nginx/conf.d/:/etc/nginx/conf.d/
      - ./nginx/ssl/:/etc/nginx/ssl/
      - ./nginx/logs/:/var/log/nginx/
    networks:
      - mynet

  #MySQL Service
  db:
    image: mysql:5.7
    container_name: db
    restart: unless-stopped
    tty: true
    ports:
      - "3306:3306"
    environment:
      MYSQL_DATABASE: laraveldb
      MYSQL_USER: laravel
      MYSQL_PASSWORD: laravelpassworddb
      MYSQL_ROOT_PASSWORD: rootpasswordmysql
    volumes:
      - mysqldata:/var/lib/mysql/
    networks:
      - mynet

#Docker Networks
networks:
  mynet:
    driver: bridge
#Volumes
volumes:
  mysqldata:
    driver: local
server {
    listen 80;
    server_name laravel.hakase-labs.io;

    return 301 https://laravel.hakase-labs.io$request_uri;
}

[laravel.conf]

server {
    listen 443 ssl http2;
    server_name laravel.hakase-labs.io;

    ssl_certificate /etc/nginx/ssl/fullchain.pem;
    ssl_certificate_key /etc/nginx/ssl/privkey.pem;

    # Log files for Debug
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    # Laravel web root directory
    root /var/www/html/public;
    index index.php index.html;

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

    # Nginx Pass requests to PHP-FPM
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

1 个答案:

答案 0 :(得分:1)

好吧,我的声誉不到50分,我无法发表评论。我问您是否将ssl添加到它指定的路径?

sudo cp /path/to/ssl/fullchain.pem nginx/ssl/

sudo cp /path/to/ssl/privkey.pem nginx/ssl/

您是否照原样复制和粘贴?还是您已经添加了ssl的路径?