欢迎来到站圈网!

java numpy线性方程组KEY_Oc36cfac82e3python线性插值pygameselenium

投稿中心

Linux

当前位置: 主页 > 服务器教程 > Linux

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

时间:2023-11-17|作者:|点击:

前言

如果使用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
  • 查看容器状态

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

  • 同步文件

#同步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
  • 查看当前目录结构

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

部署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
  •  查看容器状态

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

  • 目录结构

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

 验证

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

到此这篇关于Docker部署php运行环境(php-fpm+nginx)的文章就介绍到这了,更多相关Docker部署php运行环境内容请搜索站圈网(www.zhano.cn)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.zhano.cn)!

版权声明:本文内容由小编收集网络,均来自网络用户自发贡献,版权归原作者所有,Web前端、电脑编程学习类网站不拥有其著作权,亦不承担相应法律责任。具体规则请查看《Web前端、电脑编程学习类网站用户服务协议》和《Web前端、电脑编程学习类网站知识产权保护指引》。如果您发现本站中有涉嫌抄袭的内容,填写联系本站管理员,一经查实,管理员将立刻删除涉嫌侵权内容。

上一篇:使用Docker部署Tomcat的实现示例

下一篇:nginx-ingress-controller部署配置详解

本文标题:Docker部署php运行环境(php-fpm+nginx)

本文地址:https://www.zhano.cn/Linux/70897.html

AD300

广告投放 | 联系我们 | 版权申明 | SiteMap

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:15234109 | 邮箱:15234109#qq.com(#换成@)

Copyright © 2019-2023 豫ICP备19001789号