运用php:7.4-fpm-buster镜像安排PHP7+Nginx环境
<h1 style="color: black; text-align: left; margin-bottom: 10px;">1. 介绍</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.1 介绍</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">前面福哥<span style="color: black;">已然</span>教给<span style="color: black;">大众</span><span style="color: black;">运用</span>php:7.4-apache-buster<span style="color: black;">基本</span>镜像搭建PHP运行环境了,这个是以模块方式(Module)运行PHP程序的,相比较这种方式<span style="color: black;">更加多</span>人会<span style="color: black;">选取</span>主流的以FastCGI方式运行PHP程序,以FastCGI方式运行PHP程序就要用到PHP的FPM服务(FastCGI Process Manager)了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">今天福哥就带着<span style="color: black;">大众</span><span style="color: black;">运用</span>php:7.4-fpm-buster<span style="color: black;">基本</span>镜像来搭建以FastCGI方式运行PHP程序的环境。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">1.2 环境</span></strong></p>
<div style="color: black; text-align: left; margin-bottom: 10px;">
<div style="color: black; text-align: left; margin-bottom: 10px;">
<div style="color: black; text-align: left; margin-bottom: 10px;">
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">镜像版本</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">php:7.4-fpm-buster</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">操作系统</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">CentOS 7 x86_64 2003</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">服务器</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">TFCentOS7x64</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">IP</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">192.168.168.68</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">端口</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">80</span></span></p>
</div>
</div>
</div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">2. 安装</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1 Dockerfile</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.1 <span style="color: black;">基本</span>镜像</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">福哥<span style="color: black;">选取</span>的是php:7.4-fpm-buster这个<span style="color: black;">基本</span>镜像,这个<span style="color: black;">便是</span>FastCGI方式的。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">https://hub.docker.com/_/php/tags?page=1&name=7.4-fpm-buster</span></p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/4f34accdc04c4816aa437f0d052c8c82~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=z7UhHVJikOx6FzwsclAaXg67nqY%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;">此刻</span>hub.docker.com依然打不开,<span style="color: black;">大众</span>就选这个版本吧!</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">拉取镜像php:7.4-fpm-buster,添加到registry.tongfu.net:5000私有仓库里面。</p><span style="color: black;">docker</span> <span style="color: black;">pull</span> <span style="color: black;">php</span><span style="color: black;">:7.4-fpm-buster</span>
<span style="color: black;">docker</span> <span style="color: black;">tag</span> <span style="color: black;">php</span><span style="color: black;">:7.4-fpm-buster</span> <span style="color: black;">registry</span><span style="color: black;">.tongfu</span><span style="color: black;">.net</span><span style="color: black;">:5000</span>/<span style="color: black;">php</span><span style="color: black;">:7.4-fpm-buster</span>
<span style="color: black;">docker</span> <span style="color: black;">rmi</span> <span style="color: black;">php</span><span style="color: black;">:7.4-fpm-buster</span>
<span style="color: black;">docker</span> <span style="color: black;">images</span> | <span style="color: black;">grep</span> <span style="color: black;">php</span>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/a179a38f35ba489782dd2edd3a8978f0~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=atSHnEG2MylRPfj2ytY93CaZWU4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.2 创建镜像目录</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">创建镜像目录php7.4nginx并切换到php7.4nginx目录下面。</p>mkdir /tongfu.net/<span style="color: black;">data</span>/dockerfile/tfphp7<span style="color: black;">.4</span>nginx
cd /tongfu.net/<span style="color: black;">data</span>/dockerfile/tfphp7<span style="color: black;">.4</span>nginx<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/041660a59d214cbe8726c95ff7619f2b~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=rPFVZOmL42I91fC5H5pa0NlqMPg%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.3 依赖镜像</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">第1</span>行写上依赖镜像php:7.4-fpm-buster。</p><span style="color: black;">FROM</span> <span style="color: black;">registry</span><span style="color: black;">.tongfu</span><span style="color: black;">.net</span><span style="color: black;">:5000</span>/<span style="color: black;">php</span><span style="color: black;">:7.4-fpm-buster</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.4 <span style="color: black;">守护</span>者信息</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这是福哥写的<span style="color: black;">守护</span>者信息。</p># <span style="color: black;">for</span> <span style="color: black;">MAINTAINER</span>
<span style="color: black;">MAINTAINER</span> <span style="color: black;">Author</span>: <span style="color: black;">Andy</span> <span style="color: black;">Bogate</span>
<span style="color: black;">MAINTAINER</span> <span style="color: black;">Email</span>: <span style="color: black;">tongfu</span><span style="color: black;">@tongfu</span>.net
MAINTAINER Home <span style="color: black;">page:</span> <span style="color: black;">https:</span>//tongfu.net
MAINTAINER<span style="color: black;">Datetime:</span> <span style="color: black;">2023</span>/<span style="color: black;">03</span>/<span style="color: black;">18</span>
MAINTAINER <span style="color: black;">Version:</span> v1.<span style="color: black;">0</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.5 暴露端口</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">供给</span>服务的端口<span style="color: black;">一般</span><span style="color: black;">状况</span>下是80和443,<span style="color: black;">咱们</span>把它们暴露出来。</p><span style="color: black;"># for EXPOSE</span>
<span style="color: black;">EXPOSE</span> <span style="color: black;">80</span> <span style="color: black;">443</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.6 主目录</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">福哥设置镜像的主目录是/tongfu.net/web。</p><span style="color: black;"># for WORKDIR</span>
<span style="color: black;">WORKDIR</span> /tongfu.net/web<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.7 设置时区</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">默认时区是UTC,<span style="color: black;">咱们</span>把它改成CST,<span style="color: black;">亦</span><span style="color: black;">便是</span>中国标准时间。</p><span style="color: black;"># for timezone</span>RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
&&<span style="color: black;">echo</span> <span style="color: black;">"Asia/Shanghai"</span> > /etc/timezone<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.8 安装工具和依赖库</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">后面的制作过程需要用到wget工具,这个需要<span style="color: black;">经过</span>apt-get安装一下。除此之外还要安装<span style="color: black;">有些</span>依赖库,<span style="color: black;">包含</span>图形库、zip和xml等等。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>镜像本身<span style="color: black;">仅有</span>一个FPM服务,FPM服务只能处理php程序,要处理其他类型的文件还得需要安装Nginx服务。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>后面要是ps命令检测服务健康状态,<span style="color: black;">因此</span>还需要安装procps服务。</p><span style="color: black;"># install wget & nginx</span>
RUN apt-get <span style="color: black;">update</span> \
&& apt-<span style="color: black;">get</span> -y <span style="color: black;">install</span>wget vim nginx procps
RUN apt-<span style="color: black;">get</span> -y <span style="color: black;">install</span> libwebp-dev libjpeg-dev libpng-dev libfreetype6-dev
RUN apt-<span style="color: black;">get</span> -y <span style="color: black;">install</span> libzip-dev
RUN apt-<span style="color: black;">get</span> -y <span style="color: black;">install</span> libc-<span style="color: black;">client</span>-dev libkrb5-dev
RUN apt-<span style="color: black;">get</span> -y <span style="color: black;">install</span> libxml2-dev<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.9 释放PHP源码</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">这个php:7.4-fpm-buster<span style="color: black;">包括</span>了PHP的源码,<span style="color: black;">由于</span>后面需要<span style="color: black;">运用</span>PHP源码安装PHP扩展,<span style="color: black;">因此</span>先要释放PHP源码。</p><span style="color: black;"># extract PHP source</span>
<span style="color: black;">RUN</span> docker-php-source extract<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.10 GD图形库扩展</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">GD图形库<span style="color: black;">能够</span>让<span style="color: black;">咱们</span><span style="color: black;">经过</span>PHP直接加工<span style="color: black;">各样</span><span style="color: black;">照片</span>文件,这个库<span style="color: black;">能够</span>用来实现<span style="color: black;">照片</span>验证码、照片加工等等功能的。</p><span style="color: black;"># gd extension</span>
RUN docker-php-ext-configure gd \
<span style="color: black;">--with-jpeg=/usr/include \</span>
<span style="color: black;">--with-freetype=/usr/include/ \</span>
&& docker-php-ext-<span style="color: black;">install</span> gd \
&& docker-php-ext-<span style="color: black;">enable</span> gd<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.11 MySQL扩展</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">福哥是<span style="color: black;">运用</span>PDO来连接MySQL数据库,而MySQL的PDO库PHP资源包里<span style="color: black;">包括</span>了,只要安装起来就行了。</p><span style="color: black;"># extension of mysqli for MySQL</span>
RUN docker-php-ext-<span style="color: black;">install</span> mysqli \
&& docker-php-ext-<span style="color: black;">enable</span> mysqli
<span style="color: black;"># extension of PDO for MySQL</span>
RUN docker-php-ext-<span style="color: black;">install</span> pdo_mysql \
&& docker-php-ext-<span style="color: black;">enable</span> pdo_mysql<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.12 Memcached扩展</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">memcached库需要从PECL下载,福哥<span style="color: black;">选取</span>的是3.1.5版本,<span style="color: black;">这儿</span>直接<span style="color: black;">经过</span>phpize编译后就<span style="color: black;">能够</span>通过make安装了。memcached库依赖libmemcached库,福哥<span style="color: black;">选取</span>的是1.0.18版本,<span style="color: black;">尽可能</span>不要改避免版本冲突~</p><span style="color: black;"># library of Memcached</span>
<span style="color: black;">RUN wget https</span>:<span style="color: black;">//launchpad.net/libmemcached/1.0/1.0.18/+download/libmemcached-1.0.18.tar.gz \
&& tar -xzvf libmemcached-1.0.18.tar.gz \
&& cd libmemcached-1.0.18 \
&& sed -i s/opt_servers \=\= false/\!opt_servers/g clients/memflush.cc \
&& ./configure \
&& make && make install \
&& cd ../ \
&& rm -f libmemcached-1.0.18.tar.gz \
&& rm -rf libmemcached-1.0.18</span>
<span style="color: black;">
# extension of Memcached</span>
<span style="color: black;">RUN wget http</span>:<span style="color: black;">//pecl.php.net/get/memcached-3.1.5.tgz \
&& tar -xzvf memcached-3.1.5.tgz \
&& cd memcached-3.1.5 \
&& /usr/local/bin/phpize \
&& ./configure --with-php-config=/usr/local/bin/php-config \
--disable-memcached-sasl \
&& make && make install \
&& cd ../ \
&& rm -f memcached-3.1.5.tgz \
&& rm -rf memcached-3.1.5 \
&& docker-php-ext-enable memcached</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.13 Redis扩展</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">redis库需要从PECL下载,福哥<span style="color: black;">选取</span>的是4.1.0版本,<span style="color: black;">这儿</span>直接<span style="color: black;">经过</span>phpize编译后就<span style="color: black;">能够</span><span style="color: black;">经过</span>make安装了。</p><span style="color: black;"># extension of Redis</span>
<span style="color: black;">RUN wget http</span>:<span style="color: black;">//pecl.php.net/get/redis-4.1.0.tgz \
&& tar -xzvf redis-4.1.0.tgz \
&& cd redis-4.1.0 \
&& /usr/local/bin/phpize \
&& ./configure --with-php-config=/usr/local/bin/php-config \
&& make && make install \
&& cd ../ \
&& rm -f redis-4.1.0.tgz \
&& rm -rf redis-4.1.0 \
&& docker-php-ext-enable redis</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.14 MongoDB扩展</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MongoDB库需要从PECL下载,福哥<span style="color: black;">选取</span>的是1.7.4版本,<span style="color: black;">这儿</span>直接<span style="color: black;">经过</span>phpize编译后就<span style="color: black;">能够</span><span style="color: black;">经过</span>make安装了。</p><span style="color: black;"># extension of MongoDB</span>
<span style="color: black;">RUN wget http</span>:<span style="color: black;">//pecl.php.net/get/mongodb-1.7.4.tgz \
&& tar -xzvf mongodb-1.7.4.tgz \
&& cd mongodb-1.7.4 \
&& /usr/local/bin/phpize \
&& ./configure --with-php-config=/usr/local/bin/php-config \
&& make && make install \
&& cd ../ \
&& rm -f mongodb-1.7.4.tgz \
&& rm -rf mongodb-1.7.4 \
&& docker-php-ext-enable mongodb</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.15 其他扩展</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">除了前面安装的几个扩展之外,福哥还安装了imap、zlib、soap、bcmath这些扩展,<span style="color: black;">意见</span><span style="color: black;">大众</span><span style="color: black;">亦</span>把这些安装上。</p><span style="color: black;"># imap extension</span>
RUN docker-php-ext-configure imap \
<span style="color: black;">--with-kerberos \</span>
<span style="color: black;">--with-imap-ssl \</span>&& docker-php-ext-<span style="color: black;">install</span> imap \
&& docker-php-ext-<span style="color: black;">enable</span> imap
<span style="color: black;"># zlib extension</span>RUN mv /usr/src/php/ext/zlib/config0.m4 /usr/src/php/ext/zlib/config.m4 \
&& docker-php-ext-<span style="color: black;">install</span> zlib \
&& docker-php-ext-<span style="color: black;">enable</span> zlib
<span style="color: black;"># soap extension</span>
RUN docker-php-ext-<span style="color: black;">install</span> soap \
&& docker-php-ext-<span style="color: black;">enable</span> soap
<span style="color: black;"># bcmath extension</span>
RUN docker-php-ext-<span style="color: black;">install</span> bcmath \
&& docker-php-ext-<span style="color: black;">enable</span> bcmath<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.1.16 清理PHP源码</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">此刻</span>PHP的扩展都<span style="color: black;">根据</span>好了,<span style="color: black;">已然</span>不需要源码了,清理掉吧。</p><span style="color: black;"># source delete</span>RUN docker-php-source<span style="color: black;">delete</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.2 多进程</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">福哥今天制作的镜像是基于FastCGI方式运行的,<span style="color: black;">这儿</span>面有FPM和Nginx两个服务,<span style="color: black;">亦</span><span style="color: black;">便是</span>说<span style="color: black;">运用</span>这个镜像<span style="color: black;">起步</span>的容器里面就会运行两个进程。<span style="color: black;">然则</span><span style="color: black;">根据</span>Docker的设计思想一个容器里只能跑一个进程,怎么办?</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实按PHP的FastCGI方式的设计,应该是<span style="color: black;">起步</span>两个容器<span style="color: black;">一起</span>处理用户的请求。静态内容由Nginx容器负责处理,PHP程序由FPM容器负责处理。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>福哥不想弄<span style="color: black;">那样</span>麻烦,就想把FPM服务和Nginx服务放在<span style="color: black;">一块</span>!要实现这个目的,就需要修改镜像的ENTRYPOINT。一个ENTRYPOINT只能运行一个进程,为<span style="color: black;">认识</span>决这个问题,福哥要自定义这个ENTRYPOINT,用ENTRYPOINT<span style="color: black;">起步</span>一个Shell脚本,在Shell脚本里面<span style="color: black;">同期</span><span style="color: black;">起步</span>FPM服务和Nginx服务。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下面是福哥写的一个ENTRYPOINT<span style="color: black;">起步</span>Shell脚本start_service,用来<span style="color: black;">同期</span><span style="color: black;">起步</span>FPM服务和Nginx服务。</p><span style="color: black;">#!/bin/sh
</span>
<span style="color: black;"># argv</span>
<span style="color: black;"># functions</span>
<span style="color: black;">function</span>_main
{<span style="color: black;"># start fpm</span>
/usr/<span style="color: black;">local</span>/sbin/php-fpm -D
<span style="color: black;"># start nginx</span>
/usr/sbin/nginx
<span style="color: black;"># wait secs</span>
sleep 6s
<span style="color: black;"># service</span>
<span style="color: black;">while</span> [ 1 ] ;
<span style="color: black;">do</span>
<span style="color: black;">if</span> [ <span style="color: black;">""</span> = <span style="color: black;">"`ps -ef | grep -v grep | grep php-fpm: master process`"</span> ] ; <span style="color: black;">then</span>
<span style="color: black;">echo</span> <span style="color: black;">"php is down"</span>
<span style="color: black;">exit</span> 1
<span style="color: black;">fi</span>
<span style="color: black;">if</span> [ <span style="color: black;">""</span> = <span style="color: black;">"`ps -ef | grep -v grep | grep nginx: master process`"</span> ] ; <span style="color: black;">then</span>
<span style="color: black;">echo</span> <span style="color: black;">"nginx is down"</span>
<span style="color: black;">exit</span> 2
<span style="color: black;">fi</span>
sleep 6s
<span style="color: black;">done</span>
}
<span style="color: black;"># main</span>
_main <span style="color: black;">"<span style="color: black;">$@</span>"</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在创建新的镜像的时候把start_service配置文件拷贝进去,并且授权可执行权限,最后将ENTRYPOINT的命令改成start_service。</p><span style="color: black;"># entry point</span>
<span style="color: black;">COPY</span>start_service /usr/sbin/start_service
RUN chmod +x /usr/sbin/start_service
ENTRYPOINT [<span style="color: black;">"/usr/sbin/start_service"</span>, <span style="color: black;">""</span>]<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.3 php配置文件</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.3.1 php.ini</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">php.ini的存放目录是/usr/local/etc/php/,镜像<span style="color: black;">供给</span>了php-ini-development、php.ini-production两个配置文件模板,<span style="color: black;">咱们</span><span style="color: black;">能够</span><span style="color: black;">按照</span><span style="color: black;">运用</span>场景<span style="color: black;">选取</span><span style="color: black;">研发</span>版本还是生产版本。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">起步</span>临时容器,从容器里复制php.ini-development出来放到镜像目录下。</p><span style="color: black;">docker</span> <span style="color: black;">run</span> <span style="color: black;">-tid</span> <span style="color: black;">--name</span> <span style="color: black;">ttt</span> <span style="color: black;">-h</span> <span style="color: black;">ttt</span> <span style="color: black;">registry</span><span style="color: black;">.tongfu</span><span style="color: black;">.net</span><span style="color: black;">:5000</span>/<span style="color: black;">php</span><span style="color: black;">:7.4-fpm-buster</span>
<span style="color: black;">docker</span> <span style="color: black;">cp</span> <span style="color: black;">ttt</span>:/<span style="color: black;">usr</span>/<span style="color: black;">local</span>/<span style="color: black;">etc</span>/<span style="color: black;">php</span>/<span style="color: black;">php</span><span style="color: black;">.ini-development</span> <span style="color: black;">php</span><span style="color: black;">.ini</span>
<span style="color: black;">docker</span> <span style="color: black;">rm</span> <span style="color: black;">-f</span> <span style="color: black;">ttt</span>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/c7e786006f9b4a869c85cabfa46c5775~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=ADPzWWvzfI3fGOudeGtRkKJY4%2F8%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">设置错误日志路径,将PHP的执行错误存起来。</p><span style="color: black;">error_log</span>= /tmp/php_errors.log<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/bbbb36b520cd4777af8ec17c3606eebd~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=niHK6PM53KU40e7cXgET5418iVU%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">修改php.ini设置时区为Asia/Shanghai。</p><span style="color: black;">date.timezone</span> = Asia/Shanghai<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/dfde45649a614aa5b33e2a4d76576e7d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=Mh4verinOR0m3rAAMtqajaZ674s%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">复制php.ini覆盖镜像里面的php.ini配置文件。</p><span style="color: black;"># php.ini</span>
COPY php.ini /usr/<span style="color: black;">local</span>/etc/php/php.ini<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.4 nginx配置文件</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.4.1 模块配置</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">镜像安装的nginx<span style="color: black;">包含</span>modules-available和modules-enabled两个模块配置目录,modules-available里面<span style="color: black;">包括</span><span style="color: black;">所有</span>的可用模块配置文件,modules-enabled里面是启用的模块配置文件。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要启用模块就从modules-available<span style="color: black;">选取</span>模块配置文件移动到modules-enabled下面。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要停用模块就从modules-enabled<span style="color: black;">选取</span>模块配置文件移动回modules-available下面。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>nginx默认就支持rewrite,这个模块就<span style="color: black;">不消</span><span style="color: black;">调节</span>了!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.4.2 站点配置</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">镜像安装的nginx<span style="color: black;">包含</span>sites-available和sites-enabled两个站点配置目录,sites-available里面<span style="color: black;">包括</span><span style="color: black;">所有</span>的可用站点配置文件,sites-enabled里面是启用的站点配置文件。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要启用站点就从sites-available<span style="color: black;">选取</span>站点配置文件<span style="color: black;">创立</span>符号链接到sites-enabled下面。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">要停用站点就将sites-enabled下面删除站点配置文件的符号链接。</span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>镜像的nginx服务是福哥<span style="color: black;">经过</span>apt-get安装的,加上nginx的默认站点的配置文件default里面有<span style="color: black;">非常多</span>示例的配置乱七八糟的,<span style="color: black;">因此</span><span style="color: black;">这儿</span>福哥打算自己写一个精简版本的默认站点配置文件default。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">福哥在默认站点的server里面利用rewrite实现了一个PHP的伪静态重定向配置,<span style="color: black;">能够</span>将<span style="color: black;">各样</span>html请求转交给htmlRouteMap.php处理。</p><span style="color: black;">server</span> {
<span style="color: black;">listen</span> <span style="color: black;">80</span> default_server;
<span style="color: black;">listen</span> [::]:<span style="color: black;">80</span> default_server;
<span style="color: black;">server_name</span> _;
<span style="color: black;">rewrite</span><span style="color: black;"> ^\/(.*)\.html</span>/htmlRouteMap.php?tfphp_static_name=<span style="color: black;">$1</span>;
<span style="color: black;">location</span> / {
<span style="color: black;">root</span> /var/www/html;
}
<span style="color: black;">location</span> <span style="color: black;">~ \.php$</span> {
<span style="color: black;">client_body_buffer_size</span> <span style="color: black;">1m</span>;
<span style="color: black;">root</span> /var/www/html;
<span style="color: black;">fastcgi_pass</span>localhost:<span style="color: black;">9000</span>;
<span style="color: black;">fastcgi_index</span> index.php;
<span style="color: black;">fastcgi_param</span> SCRIPT_FILENAME <span style="color: black;">$document_root</span><span style="color: black;">$fastcgi_script_name</span>;
<span style="color: black;">include</span> fastcgi_params;
}
}<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在创建新的镜像的时候把default配置文件拷贝进去。</p><span style="color: black;"># nginx sites</span>
COPY <span style="color: black;">default</span> /etc/nginx/sites-enabled/<span style="color: black;">default</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">2.5 创建镜像</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>下面的命令创建tfphp:7.4-nginx-1.0.0镜像。</p><span style="color: black;">docker</span> <span style="color: black;">build</span> <span style="color: black;">-f</span> <span style="color: black;">Dockerfile</span> \
<span style="color: black;">-t</span> <span style="color: black;">registry</span><span style="color: black;">.tongfu</span><span style="color: black;">.net</span><span style="color: black;">:5000</span>/<span style="color: black;">tfphp</span><span style="color: black;">:7.4-nginx-1.0.0</span> ./<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/96076d7e69894b558eb1d448b31de2df~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=QFiGLBO6JsUPvcDJsOqde1VMYz4%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">查看镜像</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">瞧瞧</span>新的镜像,<span style="color: black;">此刻</span>有apache版本、nginx版本两个tfphp的镜像了。</p><span style="color: black;">docker</span> images | grep tfphp<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/8a8b7e738a804f148975a8b2c9fe12a6~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=4VNUKqKmPC5ahVhGoCnqpy2mJFM%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">3. 测试</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3.1 程序目录和程序文件</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">既然都是php环境,<span style="color: black;">那样</span><span style="color: black;">运用</span>tfphp:7.4-apache-1.0.0的测试代码就<span style="color: black;">能够</span>了!</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3.2 删除容器</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">由于</span>前面<span style="color: black;">咱们</span><span style="color: black;">运用</span>apache版本的tfphp<span style="color: black;">起步</span>了一个容器了,<span style="color: black;">此刻</span>要测试nginx版本的tfphp就需要先把这个tfphp容器删除掉。</p><span style="color: black;">docker</span> rm -f tfphp<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/bccc69ae457b442e9732ccbf2a3e5761~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=kcKBReW3%2Bk9Yuhlv2rANV%2BVy5TU%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3.3 <span style="color: black;">起步</span>容器</span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">运用</span>下面的命令基于tfphp:7.4-nginx-1.0.0镜像<span style="color: black;">起步</span>一个容器,将80端口和443端口映射到宿主机上面,将/var/www/html这个nginx主目录映射到前面<span style="color: black;">创立</span>的目录上面。</p>docker run -tid \
<span style="color: black;">--name tfphp \</span>
<span style="color: black;">-h tfphp \</span>
<span style="color: black;">-p 80:80 \</span>
<span style="color: black;">-p 443:443 \</span>
<span style="color: black;">-v /tongfu.net/data/docker/data/tfphp/html:/var/www/html \</span>
registry.tongfu.net:5000/tfphp:7.4-nginx-1.0.0<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/c00887e7cba449519697778a1f5dbc45~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=BsrhTLunm%2FvLyoNGuvHeKC1gwYU%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><strong style="color: blue;"><span style="color: black;">3.4 浏览器<span style="color: black;">拜访</span></span></strong></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">打开浏览器<span style="color: black;">拜访</span><span style="color: black;">http://192.168.168.68/index.php</span>,<span style="color: black;">能够</span>看到PHP系统信息。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">能够</span>看到Server API一栏的信息是FPM/FastCGI,证明<span style="color: black;">咱们</span>的这个运行环境是FastCGI模式的。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p26-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/414e3f98d7e746339f1f3f3fbcbca24f~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=WWDubxuqYbqdG%2F6I1DjFtkcVl8w%3D" style="width: 50%; margin-bottom: 20px;"></div>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">输入网址<span style="color: black;">http://192.168.168.68/tongfu.net/docker/tfphp.html</span>,<span style="color: black;">能够</span>看到请求html资源路径被htmlRouteMap.php打印出来了。</p>
<div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/d44899a98e2a4f53b26b172952ddd861~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728208245&x-signature=%2B6B0Z9hOPTaDMOZ8sT2iqLLg2vE%3D" style="width: 50%; margin-bottom: 20px;"></div>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">4. 总结</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">今天福哥在TFCentOS7x64服务器上面<span style="color: black;">经过</span>Dockerfile创建了一个镜像tfphp:7.4-nginx-1.0.0,并且<span style="color: black;">经过</span>这个新创建的镜像tfphp:7.4-nginx-1.0.0<span style="color: black;">起步</span>了一个容器,这个镜像用来运行基于PHP语言<span style="color: black;">研发</span>的应用程序。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">后面福哥会在这个镜像tfphp:7.4-nginx-1.0.0<span style="color: black;">基本</span>之上进行二次扩展制作新的镜像,<span style="color: black;">而后</span><span style="color: black;">按照</span>镜像的用途针对nginx和php进行自定义的配置,敬请期待~</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://m.tongfu.net/home/35/blog/513983.html</p>
“板凳”(第三个回帖的人) 论坛外链网http://www.fok120.com/
页:
[1]