当前位置:   article > 正文

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

docker php-fpm

前言

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

部署php-fpm

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

./Dockerfile

  1. #根据你自身业务需求来选择官方的php基础镜像
  2. FROM php:7.4-fpm-alpine
  3. # 设置时区
  4. ENV TZ Asia/Shanghai
  5. # 创建supervisor进程管理器相关数据存在的文件夹
  6. RUN mkdir -p "/var/log/supervisor" && mkdir -p "/var/run"
  7. # 设置源,提高下载效率
  8. RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
  9. # 安装系统依赖
  10. RUN apk update && apk --no-cache add \
  11. autoconf \
  12. g++ \
  13. make \
  14. openssl-dev \
  15. libzip-dev \
  16. unzip \
  17. tzdata \
  18. supervisor
  19. # 安装Redis扩展
  20. RUN pecl install redis && docker-php-ext-enable redis
  21. # 安装PDO MySQL扩展
  22. RUN docker-php-ext-install pdo_mysql && docker-php-ext-enable pdo_mysql
  23. # 安装opcache扩展
  24. RUN docker-php-ext-install opcache && docker-php-ext-enable opcache
  25. # 安装Composer
  26. RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
  27. #工作目录
  28. WORKDIR /app
  29. # 定义容器启动时运行的命令
  30. 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

  1. ; supervisor config file
  2. [unix_http_server]
  3. file=/var/run/supervisor.sock ; (the path to the socket file)
  4. chmod=0700 ; sockef file mode (default 0700)
  5. [supervisord]
  6. logfile=/var/log/supervisor/supervisord.log ; (main log file;default $CWD/supervisord.log)
  7. pidfile=/var/run/supervisord.pid ; (supervisord pidfile;default supervisord.pid)
  8. childlogdir=/var/log/supervisor ; ('AUTO' child log dir, default $TEMP)
  9. ; the below section must remain in the config file for RPC
  10. ; (supervisorctl/web interface) to work, additional interfaces may be
  11. ; added by defining them in separate rpcinterface: sections
  12. [rpcinterface:supervisor]
  13. supervisor.rpcinterface_factory = supervisor.rpcinterface:make_main_rpcinterface
  14. [supervisorctl]
  15. serverurl=unix:///var/run/supervisor.sock ; use a unix:// URL for a unix socket
  16. ; The [include] section can just contain the "files" setting. This
  17. ; setting can list multiple files (separated by whitespace or
  18. ; newlines). It can also contain wildcards. The filenames are
  19. ; interpreted as relative to this file. Included files *cannot*
  20. ; include files themselves.
  21. [include]
  22. files = /etc/supervisor/conf.d/*.conf

supervisord/conf.d/supervisord.conf

  1. [supervisord]
  2. nodaemon=true
  3. [program:php-fpm]
  4. command=/usr/local/sbin/php-fpm -F
  5. autostart=true
  6. autorestart=true
  7. startretries=3
  8. priority=1
  9. stdout_logfile=/var/log/php-fpm.log
  10. redirect_stderr=true
  11. [program:crond]
  12. command=/usr/sbin/crond -f
  13. autostart=true
  14. autorestart=true
  15. stdout_logfile=/var/log/crond.log
  16. redirect_stderr=true
  17. priority=2
第四步 编写docker-compose.yml:

docker-compose.yml

  1. version: '3.8'
  2. services:
  3. php7.4fpm:
  4. build:
  5. dockerfile: Dockerfile
  6. image: php7.4fpm
  7. container_name: php7.4fpm
  8. restart: always
  9. volumes:
  10. # 映射应用程序目录
  11. - /Users/king/Code/laravel8:/app/www/laravel8
  12. # 映射Crontab定时任务配置
  13. - ./cronJob:/etc/crontabs/root
  14. # 映射supervisor配置文件
  15. - ./supervisord:/etc/supervisor
  16. # 映射php扩展配置 ps:首次构建时需要注释,否则容器内该目录会为空
  17. #- ./extensions:/usr/local/etc/php/conf.d
  18. # 映射fpm配置文件 ps:首次构建时需要注释,否则容器内该目录会为空
  19. #- ./fpm-conf:/usr/local/etc/php-fpm.d
  20. networks:
  21. default:
  22. external: true
  23. name: frontend
第五步 构建镜像和容器:
  • 拉去基础镜像
docker pull php:7.4-fpm-alpine
  • 创建网络
docker network create frontend
  • 容器编排
docker-compose up -d --build
  • 查看容器状态

  • 同步文件

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

  • 查看当前目录结构

部署nginx

第一步 编写Dockerfile:

./Dockerfile

  1. FROM nginx:alpine
  2. # 安装时区工具
  3. RUN set -ex \
  4. && sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
  5. && apk --update add tzdata \
  6. && cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  7. EXPOSE 80 443
  8. # 定义容器启动时运行的命令
  9. CMD ["nginx", "-g", "daemon off;"]
第二步 编写配置文件以及站点vhost:

./nginx.conf

  1. user nginx;
  2. worker_processes auto;
  3. error_log /var/log/nginx/error.log notice;
  4. pid /var/run/nginx.pid;
  5. events {
  6. worker_connections 1024;
  7. }
  8. http {
  9. include /etc/nginx/mime.types;
  10. default_type application/octet-stream;
  11. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  12. '$status $body_bytes_sent "$http_referer" '
  13. '"$http_user_agent" "$http_x_forwarded_for"';
  14. access_log /var/log/nginx/access.log main;
  15. sendfile on;
  16. keepalive_timeout 65;
  17. #压缩配置
  18. gzip on;
  19. gzip_comp_level 5;
  20. gzip_min_length 1k;
  21. gzip_buffers 4 16k;
  22. gzip_proxied any;
  23. gzip_vary on;
  24. gzip_types
  25. application/javascript
  26. application/x-javascript
  27. text/javascript
  28. text/css
  29. text/xml
  30. application/xhtml+xml
  31. application/xml
  32. application/atom+xml
  33. application/rdf+xml
  34. application/rss+xml
  35. application/geo+json
  36. application/json
  37. application/ld+json
  38. application/manifest+json
  39. application/x-web-app-manifest+json
  40. image/svg+xml
  41. text/x-cross-domain-policy;
  42. gzip_static on;
  43. gzip_disable "MSIE [1-6]\.";
  44. include /etc/nginx/conf.d/*.conf;
  45. }

./conf.d/default.conf

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. root /app/www/laravel8/public;
  5. index index.php index.html;
  6. location / {
  7. try_files $uri $uri/ /index.php?$query_string;
  8. }
  9. location ~ \.php$ {
  10. include fastcgi_params;
  11. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  12. fastcgi_pass php7.4fpm:9000; # PHP-FPM 容器的地址和端口
  13. fastcgi_index index.php;
  14. }
  15. location ~ /\.ht {
  16. deny all;
  17. }
  18. error_log /var/log/nginx/error-php7.4fpm.log;
  19. access_log /var/log/nginx/access-php7.4fpm.log;
  20. }
第三步 编写docker-compose.yml:

./docker-compose.yml

  1. version: '3.8'
  2. services:
  3. nginx:
  4. build:
  5. dockerfile: Dockerfile
  6. image: nginx
  7. container_name: nginx
  8. volumes:
  9. - ./nginx.conf:/etc/nginx/nginx.conf
  10. - ./conf.d:/etc/nginx/conf.d
  11. - ./log:/var/log/nginx
  12. restart: always
  13. ports:
  14. - "80:80"
  15. networks:
  16. default:
  17. external: true
  18. name: frontend
第四步 构建镜像和容器:
  • 拉去基础镜像
docker pull nginx:alpine
  • 容器编排
docker-compose up -d --build
  •  查看容器状态

  • 目录结构

 验证

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

大功告成!

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/715487
推荐阅读
相关标签
  

闽ICP备14008679号