Turker
发布于 2024-12-01 / 28 阅读
0

Docker Compose 方式部署 DNMP——Docker下的Nginx、MySQL、PHP 环境

Docker Compose 方式部署 DNMP——Docker下的Nginx、MySQL、PHP 环境

1.项目简介

DNMP(Docker+Nginx+MySQL+PHP)是一个利用Docker Compose容器化部署Nginx、MySQL、PHP的项目。支持绑定任意多个域名;支持直接在宿主机中查看并修改各环境的配置文件、日志文件、PHP源码、MySQL数据等;支持安装常用的Nginx、MySQL、PHP插件。且所有镜像均来源于Docker官方仓库,安全可靠。

环境组件

Nginx: 1.26
MySQL: 8.0
PHP: 可配置版本

目录结构

.
├── .env
├── docker-compose.yml
├── mysql
│   ├── conf.d
│   ├── data
│   └── logs
└── nginx
    ├── conf.d
    ├── fastcgi-php.conf
    ├── fastcgi_params
    ├── logs
    ├── nginx.conf
    ├── ssl
    └── www

2.快速部署

2.1 安装环境

Ubuntu 24.04 LTS
Docker version 27.3.1, build ce12230
Docker Compose version v2.29.7

2.2 配置文件

Docker Compose 配置文件 docker-compose.yml:

# docker-compose.yml
version: '3.8'
services:
  nginx:
    image: nginx:1.26
    container_name: nginx
    restart: unless-stopped
    ports:
      - "${NGINX_PORT}:80"
      - "${NGINX_SSL_PORT}:443"
    volumes:
      - ./nginx/conf.d:/etc/nginx/conf.d
      - ./nginx/nginx.conf:/etc/nginx/nginx.conf
      - ./nginx/ssl:/etc/nginx/ssl
      - ./nginx/www:/usr/share/nginx/html
      - ./nginx/fastcgi-php.conf:/etc/nginx/fastcgi-php.conf
      - ./nginx/fastcgi_params:/etc/nginx/fastcgi_params
      - ./nginx/logs:/var/log/nginx
    environment:
      - TZ=${TZ}
    networks:
      - app_network
    depends_on:
      - php

  mysql:
    image: mysql:8.0
    container_name: mysql
    restart: unless-stopped
    environment:
      - MYSQL_ROOT_PASSWORD=${MYSQL_ROOT_PASSWORD}
      - TZ=${TZ}
    volumes:
      - ./mysql/data:/var/lib/mysql
      - ./mysql/conf.d:/etc/mysql/conf.d
      - ./mysql/logs:/var/log/mysql
    ports:
      - "${MYSQL_PORT}:3306"
    networks:
      - app_network

  php:
    image: php:${PHP_VERSION}-fpm
    container_name: php
    restart: unless-stopped
    volumes:
      - ./nginx/www:/var/www/html
    environment:
      - TZ=${TZ}
    networks:
      - app_network

networks:
  app_network:
    external: true

环境变量文件 .env:

#.env
# Nginx
NGINX_HOST=localhost
NGINX_PORT=80
NGINX_SSL_PORT=443

# MySQL
MYSQL_ROOT_PASSWORD=vJcK4ckeqjYL0LkGCnER
MYSQL_PORT=3306

# PHP
PHP_VERSION=8.0
PHP_PORT=9000

# TimeZone
TZ=Asia/Shanghai

Nginx 主配置文件 nginx.conf

# nginx.conf
user  nginx;
worker_processes  1;

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

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 /dev/null;
    #access_log  /var/log/dnmp/nginx.access.log  main;
    # hide verson string
    server_tokens  off;
    sendfile        on;
    #tcp_nopush     on;
    client_max_body_size 100M;
    keepalive_timeout  65;
    #gzip  on;
    include /etc/nginx/conf.d/*.conf;
}

FastCGI 配置文件 fastcgi-php.conf

# fastcgi-php.conf
# regex to split $uri to $fastcgi_script_name and $fastcgi_path
fastcgi_split_path_info ^(.+\.php)(/.+)$;

# Check that the PHP script exists before passing it
try_files $fastcgi_script_name =404;

# Bypass the fact that try_files resets $fastcgi_path_info
# see: http://trac.nginx.org/nginx/ticket/321
set $path_info $fastcgi_path_info;
#fastcgi_param PATH_INFO $path_info;
fastcgi_read_timeout 3600;

fastcgi_index index.php;

FastCGI 参数设置文件 fastcgi_params

# fastcgi_params
fastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;
fastcgi_param  QUERY_STRING       $query_string;
fastcgi_param  REQUEST_METHOD     $request_method;
fastcgi_param  CONTENT_TYPE       $content_type;
fastcgi_param  CONTENT_LENGTH     $content_length;

fastcgi_param  SCRIPT_NAME        $fastcgi_script_name;
fastcgi_param  REQUEST_URI        $request_uri;
fastcgi_param  DOCUMENT_URI       $document_uri;
fastcgi_param  DOCUMENT_ROOT      $document_root;
fastcgi_param  SERVER_PROTOCOL    $server_protocol;
fastcgi_param  REQUEST_SCHEME     $scheme;
fastcgi_param  HTTPS              $https if_not_empty;

fastcgi_param  GATEWAY_INTERFACE  CGI/1.1;
fastcgi_param  SERVER_SOFTWARE    nginx;

fastcgi_param  REMOTE_ADDR        $remote_addr;
fastcgi_param  REMOTE_PORT        $remote_port;
fastcgi_param  SERVER_ADDR        $server_addr;
fastcgi_param  SERVER_PORT        $server_port;
fastcgi_param  SERVER_NAME        $server_name;

# PHP only, required if PHP was built with --enable-force-cgi-redirect
fastcgi_param  REDIRECT_STATUS    200;

2.3 部署命令

首先新建Docker网络

#创建app_network
docker network create app_network
#查看Docker网络情况
docker network ls

接着直接拉取镜像并开始安装

docker-compose up -d

常用的命令如下:

docker-compose up                         # 创建并且启动所有容器
docker-compose up -d                      # 创建并且后台运行方式启动所有容器
docker-compose up nginx php mysql         # 创建并且启动nginx、php、mysql的多个容器
docker-compose up -d nginx php mysql     # 创建并且已后台运行的方式启动nginx、php、mysql容器

docker-compose start nginx                  # 启动服务
docker-compose stop nginx                   # 停止服务
docker-compose restart nginx              # 重启服务
docker-compose build nginx                 # 构建或者重新构建服务

docker-compose rm php                     # 删除并且停止php容器
docker-compose down                       # 停止并删除容器,网络,图像和挂载卷

当然,你也可以在你的~/.bashrc或者~/.zshrc文件中加上:

# Docker Compose 基础命令
alias dc='docker-compose'
alias dcu='docker-compose up'
alias dcud='docker-compose up -d'
alias dcd='docker-compose down'
alias dcr='docker-compose restart'
alias dcs='docker-compose stop'
alias dcl='docker-compose logs'
alias dclf='docker-compose logs -f'

# 构建相关
alias dcb='docker-compose build'
alias dcbu='docker-compose build --no-cache'

# 容器管理
alias dcps='docker-compose ps'
alias dcex='docker-compose exec'
alias dcrm='docker-compose rm'

# 清理命令
alias dcp='docker-compose pull'
alias dcdown='docker-compose down --remove-orphans'
alias dcdownv='docker-compose down --remove-orphans -v'

# 组合命令
alias dcrestart='docker-compose down && docker-compose up -d'
alias dcrebuild='docker-compose down && docker-compose build --no-cache && docker-compose up -d'

# 查看资源使用
alias dcstats='docker stats $(docker ps --format={{.Names}})'

# 进入容器
alias dcbash='docker-compose exec'  # 使用: dcbash service_name bash

# 显示所有运行容器的日志
alias dclogall='docker-compose logs --tail=100 -f $(docker-compose ps -q)'

# 进入nginx,php,mysql
alias dnginx='docker exec -it nginx /bin/sh'
alias dphp='docker exec -it php /bin/sh'
alias dmysql='docker exec -it mysql /bin/bash'

2.4 服务使用

服务访问

Nginx: http://localhost:{NGINX_PORT}
MySQL: 
  主机:localhost
  端口:{MYSQL_PORT}
  用户名:root
  密码:{MYSQL_ROOT_PASSWORD}

目录说明

nginx/www/: Web 项目根目录
nginx/conf.d/: Nginx 配置文件目录
nginx/ssl/: SSL 证书存放目录
mysql/data/: MySQL 数据存储目录
mysql/conf.d/: MySQL 配置文件目录

3.常见问题

注意事项
1. 首次使用前请确保已正确配置 .env 文件
2. 确保所需端口未被其他服务占用
3. 项目代码请放置在 nginx/www/ 目录下
4. MySQL 数据将持久化存储在 mysql/data/ 目录
5. 确保目录有正确的权限