向往美好 發表於 2020-6-25 09:54:00

Docker安装PHP

<p><img src="https://freeaihub.com/article/images/php-docker.jpg"></p>
<h2 id="本文章转自httpsfreeaihubcomarticleinstall-php-in-dockerhtml该页可在线运行以下内容">本文章转自https://freeaihub.com/article/install-php-in-docker.html,该页可在线运行以下内容</h2>
<p>本节将介绍在线使用Docker安装PHP解析器的步骤。通过本节的实操,您可以掌握从Docker环境的使用,PHP镜像以及Nginx服务器的拉取、导入、容器的启动的全部过程,从而具备使用Docker安装并部署PHP与ngninx的能力。本节要求您具备的基本能力有Linux,Docker,以及nginx.</p>
<h2 id="php镜像下载">PHP镜像下载</h2>
<pre><code class="language-bash">#为节约下载时间,可直接导入本地镜像
#docker pull php:7.1.30-fpm
docker load &lt; /share/images/php:7.1.30-fpm.tar
</code></pre>
<p>验证</p>
<pre><code class="language-bash">docker images
</code></pre>
<p>出现如下提示说明镜像pull成功了</p>
<pre><code>REPOSITORY          TAG               IMAGE ID            CREATED             SIZE
php               7.1.30-fpm          0b13895891aa      11 months ago       391MB
</code></pre>
<h2 id="制作配置文件">制作配置文件</h2>
<p>创建<code>~/nginx/conf/</code>配置等目录:</p>
<pre><code class="language-bash">mkdir -p ~/nginx/www ~/nginx/logs ~/nginx/conf
</code></pre>
<p>创建nginx配置文件</p>
<pre><code class="language-bash">vim ~/nginx/conf/php.conf
</code></pre>
<p>在nginx配置文件中填入以下内容</p>
<pre><code>server {
    listen       80;
    server_namelocalhost;

    location / {
      root   /usr/share/nginx/html;
      indexindex.html index.htm index.php;
    }

    error_page   500 502 503 504/50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }

    location ~ \.php$ {
      fastcgi_pass   php:9000;
      fastcgi_indexindex.php;
      fastcgi_paramSCRIPT_FILENAME/www/$fastcgi_script_name;
      include      fastcgi_params;
    }
}
</code></pre>
<p>配置文件说明:</p>
<ul>
<li>php:9000: 表示 php 服务的 URL。</li>
<li>/www/: 是 php 容器中 php 文件的存储路径,映射到本地的 ~/nginx/www 目录。</li>
</ul>
<p>创建首页文件</p>
<p>在<code>~/nginx/www</code>目录下创建index.php首页文件,代码如下:</p>
<pre><code class="language-bash">cat &gt; ~/nginx/www/index.php &lt;&lt; EOF
&lt;?php
   phpinfo();
?&gt;
EOF
</code></pre>
<h2 id="启动php容器和nginx容器">启动PHP容器和nginx容器</h2>
<pre><code class="language-bash">docker run --name myphp7 -v ~/nginx/www:/www -d php:7.1.30-fpm
</code></pre>
<pre><code class="language-bash">docker load &lt; /share/images/nginx.tar

docker run --name php-nginx -p 80:80 -v ~/nginx/www:/usr/share/nginx/html -v ~/nginx/conf:/etc/nginx/conf.d --link myphp7:php -d nginx
</code></pre>
<p>命令说明:</p>
<ul>
<li>--name php : 将容器命名为 php。</li>
<li>-v ~/nginx/www:/www : 将右侧云环境中的目录<code>~/nginx/www</code> 挂载到容器的<code>/www</code>目录下</li>
</ul>
<h2 id="验证">验证:</h2>
<p>使用<code>curl</code>命令验证</p>
<pre><code class="language-bash">curl localhost
</code></pre>
<p>或直接打开网址<code>{host0.http_url}</code>进行验证。</p>
<h2 id="总结">总结</h2>
<p>通过本节我们在线完成了从PHP镜像的pull,到容器的运行,再通过nginx容器完成对php服务的访问。相信通过本节的在线实操,您能更好的掌握如何使用Docker安装PHP。</p><br><br>
来源:https://www.cnblogs.com/freeaihub/p/13191172.html
頁: [1]
查看完整版本: Docker安装PHP