docker compose 部署 NGINX + PHP+MySQL

安装DOCKER

sudo apt-get update
sudo apt-get remove  docker.io containerd runc
sudo apt-get install     ca-certificates     curl     gnupg     lsb-release
sudo mkdir -p /etc/apt/keyrings
echo   "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
  $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
 sudo apt-get update
 sudo apt-get install docker-ce docker-ce-cli containerd.io docker-compose-plugin
,docker compose

安装docker compose

 sudo apt-get update
 sudo apt-get install docker-compose-plugin

# 查看安装:
 sudo docker compose version

创建数据文件夹:

mkdir /data
cd /data
mkdir -p mysql/conf.d
mkdir -p mysql/data
mkdir -p nginx/conf.d
mkdir -p nginx/log
mkdir -p php/conf.d
mkdir www

nginx 配置文件

sudo docker run --name nginx -d nginx
#复制出来配置文件:
sudo docker cp nginx:/etc/nginx/conf.d/default.conf nginx/conf.d/

创建php的Dockerfile:

leon@VM-0-6-ubuntu:/data$ cat php/Dockerfile

FROM php:7.4-fpm
RUN apt-get update && apt-get install -y \
		libfreetype6-dev \
		libjpeg62-turbo-dev \
		libpng-dev \
		libvpx-dev libwebp-dev \
	&& docker-php-ext-configure gd  --with-webp=/usr/include/webp --with-jpeg=/usr/include --with-freetype=/usr/include/freetype2 \
	&& docker-php-ext-install -j$(nproc) gd \
    && docker-php-ext-install mysqli \
    && docker-php-ext-install pdo_mysql 
docker build -t myphp:1.0 ./

复制出php的配置文件

sudo docker run --name php -d myphp:1.0
sudo docker cp php:/usr/local/etc/php/php.ini-production php/conf.d/php.ini

修改nginx/conf.d/default.conf

leon@VM-0-6-ubuntu:/data$ cat nginx/conf.d/default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

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

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

    #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   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    
    location ~ \.php$ {
       root           /usr/share/nginx/html;
       fastcgi_pass   compose-php:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
       fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
       include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

default.conf 中的注意事项

nginx的default.conf 里面

  1. root 要修改成绝对路径,否则会出现file not exist 的情况。默认是相对于当前文件的相对路径。
  2. fastcgi_pass 中要改成 docker-compose 中定义的php 的container_name。

复制数据库的配置文件

docker run --name mysql --rm mysql:5.7
docker cp mysql:/etc/mysql/conf.d mysql/conf.d

文件目录:

leon@VM-0-6-ubuntu:/data$ tree /data
/data
├── docker-compose.yaml
├── mysql
│ ├── conf.d
│ │ ├── docker.cnf
│ │ ├── mysql.cnf
│ │ └── mysqldump.cnf
│ └── data
├── nginx
│ ├── conf.d
│ │ └── default.conf
│ └── log
│ ├── access.log
│ └── error.log
├── php
│ ├── Dockerfile
│ └── conf.d
│ └── php.ini
└── www
└── info.php

docker-compose.yaml

version: "3"
services:
  nginx:
    image: nginx:latest
    container_name: "compose-nginx"
    restart: always
    ports:
      - "80:80"
      - "443:443"
    environment:
      - TZ=Asia/Shanghai
    depends_on:
      - "php"
    volumes:
      - "/data/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf"
      - "/data/nginx/log:/var/log/nginx"
      - "/data/www:/usr/share/nginx/html"
    networks:
      - net-app
  php:
    image: myphp:1.0
    container_name: "compose-php"
    restart: always
    environment:
      - TZ=Asia/Shanghai
    volumes:
      - "/data/www:/usr/share/nginx/html"
      - "/data/php/conf.d/php.ini:/usr/local/etc/php/php.ini"
    networks:
      - net-app
  mysql:
    image: mysql:5.7
    container_name: "compose-mysql"
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_USER: admin
      MYSQL_PASSWORD: admin
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/conf.d:/etc/mysql/conf.d
    networks:
      - net-app
networks:
    net-app:

启动和停止服务

到/data目录下,(有docker-compose.yaml文件的目录下),执行

#启动
docker compose up -d
#停止
docker compose stop
#卸载
docker compose down 

浏览器访问

echo ''> www/info.php

curl http://localhost/info.php

或浏览器打开访问

连接数据库:

sudo docker run -it --network data_net-app mysql:5.7 mysql -uroot -proot -hcompose-mysql

docker compose 部署 NGINX + PHP+MySQL_第1张图片

你可能感兴趣的:(docker,kubernetes,nginx,php,docker)