0


Docker部署php运行环境(php-fpm+nginx)

前言

如果使用docker去部署一套php的运行环境,我们需要构建出nginx、php-fpm两个容器,nginx通过fast_cgi协议去转发php-fpm中的端口,从而实现web server的搭建,接下来以php的laravel框架为演示例子。

部署php-fpm

第一步 编写php-fpm镜像的Dockerfile:

./Dockerfile

#根据你自身业务需求来选择官方的php基础镜像
FROM php:7.4-fpm-alpine

# 设置时区
ENV TZ Asia/Shanghai

# 创建supervisor进程管理器相关数据存在的文件夹
RUN mkdir -p "/var/log/supervisor" && mkdir -p "/var/run"

# 设置源,提高下载效率
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 安装系统依赖
RUN apk update && apk --no-cache add \
    autoconf \
    g++ \
    make \
    openssl-dev \
    libzip-dev \
    unzip \
    tzdata \
    supervisor

# 安装Redis扩展
RUN pecl install redis && docker-php-ext-enable redis

# 安装PDO MySQL扩展
RUN docker-php-ext-install pdo_mysql && docker-php-ext-enable pdo_mysql

# 安装opcache扩展
RUN docker-php-ext-install opcache && docker-php-ext-enable opcache

# 安装Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer

#工作目录
WORKDIR /app

# 定义容器启动时运行的命令
CMD ["/usr/bin/supervisord", "-c", "/etc/supervisor/supervisord.conf"]
第二步 配置Crontab定时任务:

./cronJob

* * * * * /usr/local/bin/php /app/www/laravel8/artisan schedule:run >> /var/log/laravel8-crontab-task.log 2>&1
第三步 配置supervisor进程管理器:

./supervisord/supervisord.conf

; supervisor config file

[unix_http_server]
file=/var/run/supervisor.sock   ; (the path to the socket file)
chmod=0700                       ; sockef file mode (default 0700)

[supervisord]
logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
childlogdir=/var/log/supervisor            ; ('AUTO' child log dir, default $TEMP)

; the below section must remain in the config file for RPC
; (supervisorctl/web interface) to work, additional interfaces may be
; added by defining them in separate rpcinterface: sections
[rpcinterface:supervisor]
supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface

[supervisorctl]
serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL  for a unix socket

; The [include] section can just contain the "files" setting.  This
; setting can list multiple files (separated by whitespace or
; newlines).  It can also contain wildcards.  The filenames are
; interpreted as relative to this file.  Included files *cannot*
; include files themselves.

[include]
files = /etc/supervisor/conf.d/*.conf

supervisord/conf.d/supervisord.conf

[supervisord]
nodaemon=true

[program:php-fpm]
command=/usr/local/sbin/php-fpm -F
autostart=true
autorestart=true
startretries=3
priority=1
stdout_logfile=/var/log/php-fpm.log
redirect_stderr=true

[program:crond]
command=/usr/sbin/crond -f
autostart=true
autorestart=true
stdout_logfile=/var/log/crond.log
redirect_stderr=true
priority=2
第四步 编写docker-compose.yml:

docker-compose.yml

version: '3.8'

services:
  php7.4fpm:
    build:
      dockerfile: Dockerfile
    image: php7.4fpm
    container_name: php7.4fpm
    restart: always
    volumes:

      # 映射应用程序目录
      - /Users/king/Code/laravel8:/app/www/laravel8

      # 映射Crontab定时任务配置
      - ./cronJob:/etc/crontabs/root

      # 映射supervisor配置文件
      - ./supervisord:/etc/supervisor

      # 映射php扩展配置 ps:首次构建时需要注释,否则容器内该目录会为空
      #- ./extensions:/usr/local/etc/php/conf.d

      # 映射fpm配置文件 ps:首次构建时需要注释,否则容器内该目录会为空
      #- ./fpm-conf:/usr/local/etc/php-fpm.d
networks:
  default:
    external: true
    name: frontend
第五步 构建镜像和容器:
  • 拉去基础镜像
docker pull php:7.4-fpm-alpine
  • 创建网络
docker network create frontend
  • 容器编排
docker-compose up -d --build
  • 查看容器状态

  • 同步文件
#同步php扩展配置文件夹,后续可以直接在宿主机变更相关参数配置
docker cp php7.4fpm:/usr/local/etc/php/conf.d ./extensions

#同步fpm配置文件夹,后续可以直接在宿主机变更相关参数配置
docker cp php7.4fpm:/usr/local/etc/php-fpm.d ./fpm-conf
  • 查看当前目录结构

部署nginx

第一步 编写Dockerfile:

./Dockerfile

FROM nginx:alpine

# 安装时区工具
RUN set -ex \
    && sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
    && apk --update add tzdata \
    && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

EXPOSE 80 443

# 定义容器启动时运行的命令
CMD ["nginx", "-g", "daemon off;"]
第二步 编写配置文件以及站点vhost:

./nginx.conf

user  nginx;
worker_processes  auto;

error_log  /var/log/nginx/error.log notice;
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;
    keepalive_timeout  65;

    #压缩配置
    gzip on;
    gzip_comp_level 5;
    gzip_min_length 1k;
    gzip_buffers 4 16k;
    gzip_proxied any;
    gzip_vary on;
    gzip_types
      application/javascript
      application/x-javascript
      text/javascript
      text/css
      text/xml
      application/xhtml+xml
      application/xml
      application/atom+xml
      application/rdf+xml
      application/rss+xml
      application/geo+json
      application/json
      application/ld+json
      application/manifest+json
      application/x-web-app-manifest+json
      image/svg+xml
      text/x-cross-domain-policy;
    gzip_static on;  
    gzip_disable "MSIE [1-6]\.";

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

./conf.d/default.conf

server {

    listen 80;

    server_name localhost;

    root /app/www/laravel8/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_pass php7.4fpm:9000;  # PHP-FPM 容器的地址和端口
        fastcgi_index index.php;
    }

    location ~ /\.ht {
        deny all;
    }

    error_log  /var/log/nginx/error-php7.4fpm.log;
    access_log /var/log/nginx/access-php7.4fpm.log;
}
第三步 编写docker-compose.yml:

./docker-compose.yml

version: '3.8'

services:
  nginx:
    build:
      dockerfile: Dockerfile
    image: nginx
    container_name: nginx
    volumes:
      - ./nginx.conf:/etc/nginx/nginx.conf
      - ./conf.d:/etc/nginx/conf.d
      - ./log:/var/log/nginx
    restart: always
    ports:
      - "80:80"
networks:
  default:
    external: true
    name: frontend
第四步 构建镜像和容器:
  • 拉去基础镜像
docker pull nginx:alpine
  • 容器编排
docker-compose up -d --build
  • 查看容器状态

  • 目录结构

验证

如果以上步骤顺利操作,浏览器访问 http://127.0.0.1或http://localhost看响应结果。

大功告成!

标签: docker php nginx

本文转载自: https://blog.csdn.net/Enjun97/article/details/132364290
版权归原作者 erternalKing 所有, 如有侵权,请联系我们删除。

“Docker部署php运行环境(php-fpm+nginx)”的评论:

还没有评论