一、概述

在上一篇文章介绍了nginx+php-fpm,链接如下:

https://www.cnblogs.com/xiao987334176/p/12918413.html

nginx和php-fpm是2个独立的镜像,在实际环境部署过程中,发现配置比较麻烦,排错比较耗费实际。

因此,需要将nginx和php-fpm 这2个镜像合并为一个。

二、nginx+php-fpm封装

目录结构

由于crunchgeek/php-fpm:7.3-r7 镜像比较大,有1.08GB。

因此需要使用alpine:3.11重新封装才行。

在dockerhub上面,php已经有官方的镜像了,php:7.3-fpm-alpine3.11。

由于项目php7cms依赖于组件mysqli,因此需要额外安装才行。

新建目录/opt/alpine_nginx_php7.3,结构如下:

  1. ./
  2. ├── default.conf
  3. ├── dockerfile
  4. ├── index.html
  5. ├── php.ini
  6. ├── repositories
  7. └── run.sh

default.conf

  1. server {
  2. listen 80;
  3. server_name localhost;
  4.  
  5. root /var/www/html;
  6. index index.html index.htm index.nginx-debian.html;
  7.  
  8. location / {
  9. try_files $uri $uri/ =404;
  10. }
  11. location ~ \.php$ {
  12. fastcgi_pass 127.0.0.1:9000;
  13. fastcgi_index index.php;
  14. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  15. include fastcgi_params;
  16. }
  17. }

dockerfile

  1. FROM php:7.3-fpm-alpine3.11
  2. ADD repositories /etc/apk/repositories
  3. ADD default.conf /
  4. ADD index.html /
  5. ADD run.sh /
  6. ADD php.ini /usr/local/etc/php/
  7. RUN apk update && apk add nginx && \
  8. apk add m4 autoconf make gcc g++ linux-headers && \
  9. docker-php-ext-install pdo_mysql opcache mysqli && \
  10. mkdir /run/nginx && \
  11. mv /default.conf /etc/nginx/conf.d && \
  12. mv /index.html /var/www/html && \
  13. touch /run/nginx/nginx.pid && \
  14. chmod 755 /run.sh && \
  15. apk del m4 autoconf make gcc g++ linux-headers
  16.  
  17. EXPOSE 80
  18. EXPOSE 9000
  19.  
  20. ENTRYPOINT ["/run.sh"]

注意:这里我额外安装了pdo_mysql,因为某些php项目用的是这个模块。opcache是用来做性能加速的。

index.html

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Welcome to nginx!</title>
  5. <style>
  6. body {
  7. width: 35em;
  8. margin: 0 auto;
  9. font-family: Tahoma, Verdana, Arial, sans-serif;
  10. }
  11. </style>
  12. </head>
  13. <body>
  14. <h1>Welcome to nginx!</h1>
  15. <p>If you see this page, the nginx web server is successfully installed and
  16. working. Further configuration is required.</p>
  17.  
  18. <p>For online documentation and support please refer to
  19. <a href="http://nginx.org/">nginx.org</a>.<br/>
  20. Commercial support is available at
  21. <a href="http://nginx.com/">nginx.com</a>.</p>
  22.  
  23. <p><em>Thank you for using nginx.</em></p>
  24. </body>
  25. </html>

php.ini

  1. [PHP]
  2. engine = On
  3. short_open_tag = Off
  4. precision = 14
  5. output_buffering = 4096
  6. zlib.output_compression = Off
  7. implicit_flush = Off
  8. unserialize_callback_func =
  9. serialize_precision = -1
  10. disable_functions =
  11. disable_classes =
  12. zend.enable_gc = On
  13. expose_php = On
  14. max_execution_time = 30
  15. max_input_time = 60
  16. memory_limit = 128M
  17. error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
  18. display_errors = Off
  19. display_startup_errors = Off
  20. log_errors = On
  21. log_errors_max_len = 1024
  22. ignore_repeated_errors = Off
  23. ignore_repeated_source = Off
  24. report_memleaks = On
  25. html_errors = On
  26. variables_order = "GPCS"
  27. request_order = "GP"
  28. register_argc_argv = Off
  29. auto_globals_jit = On
  30. post_max_size = 8M
  31. auto_prepend_file =
  32. auto_append_file =
  33. default_mimetype = "text/html"
  34. default_charset = "UTF-8"
  35. doc_root =
  36. user_dir =
  37. enable_dl = Off
  38. cgi.fix_pathinfo=0
  39. file_uploads = On
  40. upload_max_filesize = 2M
  41. max_file_uploads = 20
  42. allow_url_fopen = On
  43. allow_url_include = Off
  44. default_socket_timeout = 60
  45. [CLI Server]
  46. cli_server.color = On
  47. [Date]
  48. [filter]
  49. [iconv]
  50. [imap]
  51. [intl]
  52. [sqlite3]
  53. [Pcre]
  54. [Pdo]
  55. [Pdo_mysql]
  56. pdo_mysql.default_socket=
  57. [Phar]
  58. [mail function]
  59. SMTP = localhost
  60. smtp_port = 25
  61. mail.add_x_header = Off
  62. [ODBC]
  63. odbc.allow_persistent = On
  64. odbc.check_persistent = On
  65. odbc.max_persistent = -1
  66. odbc.max_links = -1
  67. odbc.defaultlrl = 4096
  68. odbc.defaultbinmode = 1
  69. [Interbase]
  70. ibase.allow_persistent = 1
  71. ibase.max_persistent = -1
  72. ibase.max_links = -1
  73. ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
  74. ibase.dateformat = "%Y-%m-%d"
  75. ibase.timeformat = "%H:%M:%S"
  76. [MySQLi]
  77. mysqli.max_persistent = -1
  78. mysqli.allow_persistent = On
  79. mysqli.max_links = -1
  80. mysqli.default_port = 3306
  81. mysqli.default_socket =
  82. mysqli.default_host =
  83. mysqli.default_user =
  84. mysqli.default_pw =
  85. mysqli.reconnect = Off
  86. [mysqlnd]
  87. mysqlnd.collect_statistics = On
  88. mysqlnd.collect_memory_statistics = Off
  89. [OCI8]
  90. [PostgreSQL]
  91. pgsql.allow_persistent = On
  92. pgsql.auto_reset_persistent = Off
  93. pgsql.max_persistent = -1
  94. pgsql.max_links = -1
  95. pgsql.ignore_notice = 0
  96. pgsql.log_notice = 0
  97. [bcmath]
  98. bcmath.scale = 0
  99. [browscap]
  100. [Session]
  101. session.save_handler = files
  102. session.use_strict_mode = 0
  103. session.use_cookies = 1
  104. session.use_only_cookies = 1
  105. session.name = PHPSESSID
  106. session.auto_start = 0
  107. session.cookie_lifetime = 0
  108. session.cookie_path = /
  109. session.cookie_domain =
  110. session.cookie_httponly =
  111. session.cookie_samesite =
  112. session.serialize_handler = php
  113. session.gc_probability = 1
  114. session.gc_divisor = 1000
  115. session.gc_maxlifetime = 1440
  116. session.referer_check =
  117. session.cache_limiter = nocache
  118. session.cache_expire = 180
  119. session.use_trans_sid = 0
  120. session.sid_length = 26
  121. session.trans_sid_tags = "a=href,area=href,frame=src,form="
  122. session.sid_bits_per_character = 5
  123. [Assertion]
  124. zend.assertions = -1
  125. [COM]
  126. [mbstring]
  127. [gd]
  128. [exif]
  129. [Tidy]
  130. tidy.clean_output = Off
  131. [soap]
  132. soap.wsdl_cache_enabled=1
  133. soap.wsdl_cache_dir="/tmp"
  134. soap.wsdl_cache_ttl=86400
  135. soap.wsdl_cache_limit = 5
  136. [sysvshm]
  137. [ldap]
  138. ldap.max_links = -1
  139. [dba]
  140. [opcache]
  141. [curl]
  142. [openssl]

此文件是从容器里面copy出来的,路径为:/usr/local/etc/php/php.ini-production

去除了注释和多余的空行。

此配置文件,修改了cgi.fix_pathinfo=0

如果需要更改其他配置,修改此文件即可。

repositories

  1. https://mirrors.aliyun.com/alpine/v3.11/main/
  2. https://mirrors.aliyun.com/alpine/v3.11/community/

这个是阿里云的alpine更新源

run.sh

  1. #!/bin/sh
  2.  
  3. # 后台启动
  4. php-fpm -D
  5. # 关闭后台启动,hold住进程
  6. nginx -g 'daemon off;'

封装镜像

  1. cd /opt/alpine_nginx_php7.3
  2. docker build -t alpine_nginx_php7.3:1 .

查看镜像大小

  1. # docker images|grep alpine_nginx_php7.3
  2. alpine_nginx_php7.3 1 927ddfbdd027 14 minutes ago 78.4MB

可以看到这个镜像只有78.4MB。

运行镜像

  1. docker run -it --name alpine_nginx_php7.3 -p 80:80 alpine_nginx_php7.3:1 .

访问首页

  1. http://ip地址/

效果如下:

phpinfo页面

新建test.php

  1. cd /opt/alpine_nginx_php7.3
  2. vi test.php

内容如下:

  1. <?php
  2. phpinfo();
  3. ?>

拷贝到容器中

  1. docker cp test.php alpine_nginx_php7.3:/var/www/html/

访问test.php

  1. http://ip地址/test.php

效果如下:

三、运行PHP7CMS

下载源代码

源代码下载地址:

http://down.chinaz.com/soft/38829.htm

下载完成后,在windows10电脑中解压。

进入linux系统,创建空目录/opt/php7cms,将解压文件夹PHP7CMS的所有内容上传到/opt/php7cms中。

此时/opt/php7cms目录结构如下:

  1. # tree -L 1
  2. .
  3. ├── admin.php
  4. ├── api
  5. ├── cache
  6. ├── config
  7. ├── index.php
  8. ├── install.php
  9. ├── LICENSE
  10. ├── php7cms
  11. ├── README.md
  12. ├── static
  13. ├── template
  14. ├── uploadfile
  15. ├── 安装方法.txt
  16. └── 安装环境.docx

-L 参数表示控制深度,这里只展示第一层。

在此目录新建dockerfile

  1. FROM alpine_nginx_php7.3:1
  2. ADD default.conf /etc/nginx/conf.d
  3. ADD . /var/www/html/PHP7CMS
  4. RUN chown www-data:www-data -R /var/www/html

在此目录新建default.conf

  1. server {
  2. listen 80;
  3. server_name localhost;
  4. root /var/www/html/PHP7CMS;
  5. index index.php index.html index.htm;
  6.  
  7. location ~ \.php$ {
  8. fastcgi_pass 127.0.0.1:9000;
  9. fastcgi_index index.php;
  10. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  11. include fastcgi_params;
  12. }
  13. }

这个文件用来将nginx默认的配置覆盖掉

封装镜像

  1. cd /opt/php7cms
  2. docker build -t php7cms:1 .

运行docker

先将之前运行的nginx_php删除掉,再运行php7cms。否则会端口冲突

  1. docker rm -f alpine_nginx_php7.3
  2. docker run -d -it --restart=always --name php7cms -p 80:80 php7cms:1

由于php7cms依赖于mysql,还得运行一个mysql才行。

  1. mkdir -p /data/mysql/data
  2. docker run -d --name mysql5.7 --restart=always -e MYSQL_ROOT_PASSWORD=abcd@1234 -p 3306:3306 -v /data/mysql/data:/var/lib/mysql mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

新建空的数据库cms

  1. # docker exec -it mysql5.7 /bin/bash
  2. # mysql -u root -pabcd@1234
  3. mysql> create database cms default character set utf8mb4 collate utf8mb4_unicode_ci;

安装向导

我的服务器ip地址为:10.212.20.213

访问安装页面

  1. http://10.212.20.213/install.php

输入数据库连接信息

点击下一步后,提示安装完成。

登录后台页面

默认用户名和密码都是admin

登录成功后,效果如下:

访问首页

  1. http://10.212.20.213/

效果如下:

nginx+php-fpm docker镜像合二为一的更多相关文章

  1. 制作nginx+php的docker镜像方法

    制作nginx+php的docker镜像方法一.准备安装的工具工具:docker-17.06.0-ce.nginx-1.13.2.PHP-5.5.38 .supervisor配置思路:1.安装dock ...

  2. Nginx+PHP7.3.9 Docker镜像制作

    最近因项目需要制作了多个版本的php docker镜像,制作过程可谓是一波三折,因基于yum的方式安装php的方式在安装扩展插件时很不方便,不容易找到插件对应的yum源,所以PHP在docker镜像中 ...

  3. jenkins自动打包生成docker镜像后自动发布并nginx代理访问

    之前曾写过docker及jenkins基础使用  https://www.cnblogs.com/xiaochangwei/category/816943.html 现在搭建环境的功能为: 1.jen ...

  4. Docker镜像+nginx 部署 vue 项目

    一.打包vue项目 在开发完的vue项目输入如下命名,打包生成dist文件夹 yarn build / npm run build 此时根目录会多出一个文件夹:dist文件夹,里面就是我们要发布的东西 ...

  5. Docker镜像实战(ssh、systemctl、nginx、tomcat、mysql)

    Docker镜像实战 1.构建ssh镜像 2.构建systemctl 镜像 3.构建nginx镜像 4.构建tomcat镜像 5.构建mysql镜像 1.构建ssh镜像: 创建镜像目录 mkdir / ...

  6. 理解Docker(2):Docker 镜像

    本系列文章将介绍Docker的有关知识: (1)Docker 安装及基本用法 (2)Docker 镜像 (3)Docker 容器的隔离性 - 使用 Linux namespace 隔离容器的运行环境 ...

  7. 第四章 使用Docker镜像和仓库(二)

    第四章 使用Docker镜像和仓库(二) 回顾: 开始学习之前,我先pull下来ubuntu和fedora镜像 [#9#cloudsoar@cloudsoar-virtual-machine ~]$s ...

  8. 第四章 使用Docker镜像和仓库

    第4章 使用Docker镜像和仓库 回顾: 回顾如何使用 docker run 创建最基本的容器 $sudo docker run -i -t --name another_container_mum ...

  9. 《第一本docker书》第4章 使用docker镜像和仓库 读书笔记

    docker最底端是一个引导文件系统,即bootfs. 第二层是root文件系统rootfs,位于引导文件系统之上. 在传统的Linux引导过程中,root文件系统会最先以只读的方式加载,当引导结束并 ...

随机推荐

  1. mitmproxy使用详解

    mitmproxy 相比Charles.fiddler的优点在于,它可以命令行方式或脚本的方式进行mock mitmproxy不仅可以像Charles那样抓包,还可以对请求数据进行二次开发,进入高度二 ...

  2. Educational Codeforces Round 90 (Rated for Div. 2) D. Maximum Sum on Even Positions(dp)

    题目链接:https://codeforces.com/contest/1373/problem/D 题意 给出一个大小为 $n$ 的数组 $a$,下标为 $0 \sim n - 1$,可以进行一次反 ...

  3. 找新朋友 HDU - 1286 欧拉函数模板题

    题意: 求出来区间[1,n]内与n互质的数的数量 题解: 典型的欧拉函数应用,具体见这里:Relatives POJ - 2407 欧拉函数 代码: 1 #include<stdio.h> ...

  4. Codeforces Round #327 (Div. 1) C. Three States

    C. Three States time limit per test 5 seconds memory limit per test 512 megabytes input standard inp ...

  5. Vue的七种传值方式

    目录 1,父传子 2,子传父 3,兄弟组件传值 4,父组件使用子组件的数据和方法 5,子组件使用父组件的数据和方法 6,Vuex传值 6.1,定义store 6.2,挂载 6.3,使用 7,路由传值 ...

  6. Logstash 日志收集(补)

    收集 Tomcat 日志 安装 Tomcat # 安装 jdk [root@web01 ~]# rpm -ivh jdk-8u181-linux-x64.rpm # 下载 [root@web01 ~] ...

  7. Xtrabackup 物理备份

    目录 Xtrabackup 安装 Xtrabackup 备份介绍 Xtrabackup全量备份 准备备份目录 全量备份 查看全量备份内容 Xtrabackup 全量备份恢复数据 删除所有数据库 停止数 ...

  8. printf,sprintf,fprintf的区别与联系

    在写代码过程中总会遇到printf和sprintf,既然这两个都遇到了,那么不妨再加一个fprintf吧. 他们三个都是将格式化字符串输出,区别就是他们输出的目标不一样. (1).printf,是把格 ...

  9. PAT l2-010 排座位 【并查集】

    L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...

  10. springboot demo(一)快速开始

    快速入门 maven构建项目 1.访问http://start.spring.io/ 2.选择构建工具Maven Project.Spring Boot版本2.26以及一些工程基本信息,点击" ...