[关闭]
@gabe 2020-05-21T10:27:39.000000Z 字数 3333 阅读 1593

使用docker-compose 快速搭建Laravel,Nginx,Mysql开发环境

未分类


准备工作

第一步,下载Laravel,安装依赖

  1. $ cd ~
  2. $ git clone https://github.com/laravel/laravel.git laravel-app

使用php composer 安装Laravle依赖,这里使用的是composer容器,

  1. $ cd ~/laravel-app

先配置下镜像加速

  1. $ docker run --rm -v $(pwd):/app composer config repo.packagist composer https://mirrors.aliyun.com/composer/

安装依赖

  1. $ docker run --rm -v $(pwd):/app composer install -vvv

设置文件权限

  1. sudo chown -R $USER:$USER ~/laravel-app

第二步,创建docer-compose配置文件docker-compose.yml

  1. $ vim docker-compose.yml

内容:

  1. version: '3'
  2. services:
  3. #PHP Service
  4. app:
  5. build:
  6. context: .
  7. dockerfile: Dockerfile
  8. image: digitalocean.com/php
  9. container_name: app
  10. restart: unless-stopped
  11. tty: true
  12. environment:
  13. SERVICE_NAME: app
  14. SERVICE_TAGS: dev
  15. working_dir: /var/www
  16. volumes:
  17. - ./:/var/www
  18. - ./php/local.ini:/usr/local/etc/php/conf.d/local.ini
  19. networks:
  20. - app-network
  21. #Nginx Service
  22. webserver:
  23. image: nginx:alpine
  24. container_name: webserver
  25. restart: unless-stopped
  26. tty: true
  27. ports:
  28. - "80:80"
  29. - "443:443"
  30. volumes:
  31. - ./:/var/www
  32. - ./nginx/conf.d/:/etc/nginx/conf.d/
  33. networks:
  34. - app-network
  35. #MySQL Service
  36. db:
  37. image: mysql:5.7.22
  38. container_name: db
  39. restart: unless-stopped
  40. tty: true
  41. ports:
  42. - "3306:3306"
  43. environment:
  44. MYSQL_DATABASE: laravel
  45. MYSQL_ROOT_PASSWORD: your_mysql_root_password
  46. SERVICE_TAGS: dev
  47. SERVICE_NAME: mysql
  48. volumes:
  49. - dbdata:/var/lib/mysql/
  50. - ./mysql/my.cnf:/etc/mysql/my.cnf
  51. networks:
  52. - app-network
  53. #Docker Networks
  54. networks:
  55. app-network:
  56. driver: bridge
  57. #Volumes
  58. volumes:
  59. dbdata:
  60. driver: local

第三步,数据持久化

创建Dockerfile

  1. $ vim ~/laravel-app/Dockerfile

内容

  1. FROM php:7.2-fpm
  2. # Copy composer.lock and composer.json
  3. COPY composer.lock composer.json /var/www/
  4. # Set working directory
  5. WORKDIR /var/www
  6. # Install dependencies
  7. RUN apt-get update && apt-get install -y \
  8. build-essential \
  9. libpng-dev \
  10. libjpeg62-turbo-dev \
  11. libfreetype6-dev \
  12. locales \
  13. zip \
  14. jpegoptim optipng pngquant gifsicle \
  15. vim \
  16. unzip \
  17. git \
  18. curl
  19. # Clear cache
  20. RUN apt-get clean && rm -rf /var/lib/apt/lists/*
  21. # Install extensions
  22. RUN docker-php-ext-install pdo_mysql mbstring zip exif pcntl
  23. RUN docker-php-ext-configure gd --with-gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ --with-png-dir=/usr/include/
  24. RUN docker-php-ext-install gd
  25. # Install composer
  26. RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
  27. # Add user for laravel application
  28. RUN groupadd -g 1000 www
  29. RUN useradd -u 1000 -ms /bin/bash -g www www
  30. # Copy existing application directory contents
  31. COPY . /var/www
  32. # Copy existing application directory permissions
  33. COPY --chown=www:www . /var/www
  34. # Change current user to www
  35. USER www
  36. # Expose port 9000 and start php-fpm server
  37. EXPOSE 9000
  38. CMD ["php-fpm"]

配置php

  1. $ mkdir ~/laravel-app/php
  2. $ ~/laravel-app/php/local.ini

内容

  1. upload_max_filesize=40M
  2. post_max_size=40M

配置nginx

  1. $ mkdir -p ~/laravel-app/nginx/conf.d
  2. $ vim ~/laravel-app/nginx/conf.d/app.conf

内容

  1. server {
  2. listen 80;
  3. index index.php index.html;
  4. error_log /var/log/nginx/error.log;
  5. access_log /var/log/nginx/access.log;
  6. root /var/www/public;
  7. location ~ \.php$ {
  8. try_files $uri =404;
  9. fastcgi_split_path_info ^(.+\.php)(/.+)$;
  10. fastcgi_pass app:9000;
  11. fastcgi_index index.php;
  12. include fastcgi_params;
  13. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  14. fastcgi_param PATH_INFO $fastcgi_path_info;
  15. }
  16. location / {
  17. try_files $uri $uri/ /index.php?$query_string;
  18. gzip_static on;
  19. }
  20. }

配置mysql

  1. $ mkdir ~/laravel-app/mysql
  2. $ ~/laravel-app/mysql/my.cnf

内容

  1. [mysqld]
  2. general_log = 1
  3. general_log_file = /var/lib/mysql/general.log

修改环境变量,运行容器

  1. $ cp .env.example .env

.env内容

  1. DB_CONNECTION=mysql
  2. DB_HOST=db
  3. DB_PORT=3306
  4. DB_DATABASE=laravel
  5. DB_USERNAME=laraveluser
  6. DB_PASSWORD=your_laravel_db_password

运行

  1. docker-compose up -d
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注