docker compose mysql_docker-compose搭建nginx+mysql+php

闾丘冠玉
2023-12-01

前置工作

下载后执行

chmod +x /usr/local/bin/docker-compose

1. 创建好目录及文件

1.1 docker-compose.yml文件

version: '3'

services:

nginx:

build: ./nginx

ports:

- "80:80"

links:

- "php"

volumes:

- /var/www/docker-compose/nginx/conf/default.conf:/etc/nginx/conf.d/default.conf

- /var/www/docker-compose/www:/var/www

php:

build: ./php

ports:

- "9000:9000"

links:

- "mysql"

volumes:

- /var/www/docker-compose/www:/var/www

mysql:

build: ./mysql

ports:

- "3306:3306"

volumes:

- /var/www/docker-compose/mysql/data/:/var/lib/mysql/

environment:

MYSQL_ROOT_PASSWORD: 123456

1.2 nginx目录

1.2.1 Dockerfile文件

FROM nginx:1.12.2

RUN apt-get update && apt-get install -y vim

1.2.2 配置文件

server {

listen 80;

server_name localhost;

location / {

root /var/www; # 代码目录

index index.html index.htm index.php; # 增加index.php

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html

#

error_page 500 502 503 504 /50x.html;

location = /50x.html {

root /var/www/html;

}

location ~ \.php$ {

root /var/www; # 代码目录

fastcgi_pass php:9000; # 修改为php容器(compose文件中nginx段设置的links的php名称)

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; # 修改为$document_root

include fastcgi_params;

}

}

1.3 mysql目录

1.3.1 Dockerfile文件

FROM mysql:5.7

1.4 php目录

1.4.1 Dockerfile文件

FROM php:7.3-fpm

RUN apt-get update && apt-get install -y \

libfreetype6-dev \

libjpeg62-turbo-dev \

libpng12*-dev \

vim \

&& docker-php-ext-install pdo_mysql \

&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \

&& docker-php-ext-install gd \

2. 执行并测试

在docker-compose目录下执行命令,构建成功则可进行测试

docker-compose up -d

 类似资料: