Win10 Docker 搭建lnmp环境运行PHP项目

Win10 Docker 搭建lnmp环境运行PHP项目

  • win10下载、安装Docker
    • 配置阿里云docker镜像加速
    • 拉取镜像-php、nginx、mysql
    • 创建目录
    • 配置docker-compose.yml文件
    • nginx配置
    • 用docker-compose命令启动容器
    • 访问项目
  • 填坑
  • 使用Dockerfile定制php镜像
    • 命令

win10下载、安装Docker

参考:win10下载、安装Docker

配置阿里云docker镜像加速

{
  "registry-mirrors": ["https://xxxxxxxxx.mirror.aliyuncs.com"],
  "insecure-registries": [],
  "debug": false,
  "experimental": true,
  "features": {
    "buildkit": true
  }
}

Win10 Docker 搭建lnmp环境运行PHP项目_第1张图片

拉取镜像-php、nginx、mysql

  • 下载对应的镜像
$ docker pull php:7.3.25-fpm-buster 
$ docker pull nginx
$ docker pull mysql:5.7

需要其他版本的镜像可以到hub.docker.com,搜索php并通过tags找到自己想要的版本,如果不用管版本直接docker pull php即可。

创建目录

  • 目录截图
    Win10 Docker 搭建lnmp环境运行PHP项目_第2张图片
  • 目录介绍
.\mysql\config mysql配置目录
.\mysql\logs mysql日志目录
.\mysql\lib mysql数据目录
.\php php配置目录
.\nginx/www	项目根目录
.\nginx/conf 虚拟域名配置目录
.\nginx/logs nginx访问日志目录
.\nginx/nginx.conf nginx配置文件

配置docker-compose.yml文件

  • 目录截图
    Win10 Docker 搭建lnmp环境运行PHP项目_第3张图片
  • 目录介绍
version: "3"

services:
  mysql:
    hostname: mysql
    restart: always
    image: mysql:5.7
    container_name: mysql
    ports:
      - "3306:3306"
    volumes:
      - .\mysql\config:/etc/mysql
      - .\mysql\log:/var/log/mysql
      - .\mysql\lib:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: user
      MYSQL_PASSWORD: user123
  php:
    image: php:7.3.25-fpm-buster 
    hostname: php
    restart: always
    container_name: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
      - "9000:9000"
    links:
      - mysql:mysql
    volumes:
      - .\nginx/www:/var/www/html
      - .\php\php.ini:/usr/local/etc/php/php.ini
  nginx:
    hostname: nginx
    restart: always
    container_name: nginx
    image: nginx
    ports:
      - "80:80"
      - "443:443"
    links:
      - "php:php"
    volumes:
      - .\nginx/www:/usr/share/nginx/html
      - .\nginx/conf:/etc/nginx/conf.d
      - .\nginx/logs:/var/log/nginx
      - .\nginx/nginx.conf:/etc/nginx/nginx.conf

nginx配置

  • nginx.con配置(文件位置:.\nginx/nginx.conf
user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

  • nginx虚拟域名配置(目录位置:.\nginx/conf
    • 新建php.conf
server {
	listen	80;
	server_name	localhost;
    index index.php index.html error/index.html;

	location / {
		root	/usr/share/nginx/html/;
		index	index.php index.html index.htm ;
	}

	error_page	500 502 503 504	/50x.html;
	location = /50x.html {
		root	/usr/share/nginx/html/xm/public;
	}

	location ~ \.php$ {
		fastcgi_pass	php:9000;
		fastcgi_index	index.php;
		fastcgi_param	SCRIPT_FILENAME	/var/www/html/$fastcgi_script_name;
        # fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include		fastcgi_params;
	}
}1

注意:fastcgi_pass php:9000; php必须填写php的镜像名称

用docker-compose命令启动容器

docker-compose up -d

访问项目

  • 新建index.php

  • 浏览器访问
    Win10 Docker 搭建lnmp环境运行PHP项目_第4张图片

填坑

  • 导入项目后发现链接数据库失败
      1. php镜像没有按照pdo_mysql
      1. 数据库配置文件,数据库配置文件记得填写mysql镜像名:mysql 例:DB_HOST=mysql

使用Dockerfile定制php镜像

  • 配置Dockerfile(文件名即为:Dockerfile
FROM php:7.3.25-fpm-buster
RUN docker-php-ext-install pdo_mysql
  • 配置php.ini(文件位置:.\php\php.ini),开启pdo_mysql
找到;extension=pdo_mysql 去掉;

extension=pdo_mysql
  • 开始构建镜像
    • 通过目录下的 Dockerfile 构建一个 nginx:v1(镜像名称:镜像标签)。
    • 注:最后的 . 代表本次执行的上下文路径,
docker build -t nginx:v1 .
docker ps -a

在这里插入图片描述

  • 重新配置docker-compose.yml文件
version: "3"

services:
  mysql:
    hostname: mysql
    restart: always
    image: mysql:5.7
    container_name: mysql
    ports:
      - "3306:3306"
    volumes:
      - .\mysql\config:/etc/mysql
      - .\mysql\log:/var/log/mysql
      - .\mysql\lib:/var/lib/mysql
    environment:
      MYSQL_ROOT_PASSWORD: 123456
      MYSQL_USER: user
      MYSQL_PASSWORD: user123
  php:
    image: php:v1
    hostname: php
    restart: always
    container_name: php
    build:
      context: ./php
      dockerfile: Dockerfile
    ports:
      - "9000:9000"
    links:
      - mysql:mysql
    volumes:
      - .\nginx/www:/var/www/html
      - .\php\php.ini:/usr/local/etc/php/php.ini
  nginx:
    hostname: nginx
    restart: always
    container_name: nginx
    image: nginx
    ports:
      - "80:80"
      - "443:443"
    links:
      - "php:php"
    volumes:
      - .\nginx/www:/usr/share/nginx/html
      - .\nginx/conf:/etc/nginx/conf.d
      - .\nginx/logs:/var/log/nginx
      - .\nginx/nginx.conf:/etc/nginx/nginx.conf
  • 重新启动容器
    • 关闭容器
     docker-compose down
    
    • 启动容器
     docker-compose up -d
    
  • 访问项目,项目即可使用mysql

命令

  • 使用 Compose 命令构建和运行您的应用
docker-compose up -d 
  • 停止up命令所启动的容器,并移除网络
docker-compose down
  • 启动容器
docker-compose start nginx
  • 停止容器
docker-compose stop nginx
  • 重启容器
docker-compose restart nginx
  • 进入容器
docker exec -it nginx bash
  • 通过目录下的 Dockerfile 构建一个 nginx:v1(镜像名称:镜像标签)
    • 执行完后就会生成一个(镜像名称:镜像标签),在yml里面加上这个版本的镜像即可
docker build -t nginx:v1 .

你可能感兴趣的:(Linux,Docker,docker,nginx,mysql,linux)