使用Docker容器化Apache,MySQL和PHP [英] Containerizing Apache, MySQL and PHP With Docker
问题描述
我一直在网上搜索并阅读手册,但似乎无法理解Docker设置的问题.
目标
要对Apache,PHP和MySQL进行容器化,从而允许在每个项目的基础上对其进行自定义.部署堆栈的 only 依赖项应为docker.所有其他依赖项/操作都应该能够通过Dockerfile
构建/运行.
概念证明
通过docker-compose.yml
文件从我的Apache + MySQL + PHP堆栈中-我想定位一个index.php
页面以成功呈现Hello Docker!
以及所有可用数据库的列表.
问题
当我在浏览器中访问docker.dev/index.php
时,只能查看PHP源代码,而不能运行PHP代码.这是我看到的:
<?php
/**
* This file:
* Has not been tested
* Does not use prepared statements
* Is for Proof of Concept only!
*/
$host = '127.0.0.1';
$user = 'root';
$pass = 'docker';
$conn = new mysqli($host, $user, $pass);
$sql = 'show databases';
$results = $conn->query($sql);
?>
<h1>Hello Docker!</h1>
<ul>
<?php while ($row = $results->fetch_assoc()) : ?>
<li><?= $row['Database'] ?></li>
<?php endwhile ?>
</ul>
我的理解(可能是错误的)是Apache正确地处理了虚拟主机,但不知道如何通过Apache PHP模块加载PHP文件.
我已经将Apache设置为depends_on
PHP,并且已经通过network
(以及MySQL)将它们链接在一起,但是显然我缺少了某些东西,否则一切都会按我的意愿进行.)>
我在github上创建了一个存储库,该存储库应允许您使用一些简单的命令来测试我的设置:
git clone https://github.com/dambrogia/docker-testing.git
cd docker-testing
docker-compose up -d
您还必须在主机上的hosts文件中将docker.dev
添加到127.0.0.1
!
当我访问docker.dev/index.php
时,如何呈现PHP而不是阅读其源代码?
我不希望尽可能使用PHP和Apache组合图像.我想要三个独立的容器-PHP,Apache,MySQL.
我解决了这个问题,并为对更深入的解释或概念证明感兴趣的人创建了一个回购协议.
请参阅我的存储库: https://github.com/dambrogia/docker-testing
TL; DR
我用来解决此问题的方法是通过fcgi://php:9000
将对所有.php
文件的所有apache请求代理到PHP-FPM.默认端口9000
您可以在运行中查看此Apache设置 解决方案
I solved this question and created a repo for anyone who is interested a more in depth explanation or proof of concept.
Please see my repo: https://github.com/dambrogia/docker-testing
TL; DR
The approach I used to solve this was proxying all apache requests to any .php
files to PHP-FPM via fcgi://php:9000
. Port 9000 is the default
You can see this Apache setting in action here.
The /var/www/html/$1
portion of the setting is where the files are mapped within the PHP container.
这篇关于使用Docker容器化Apache,MySQL和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!