docker配置apache+php运行环境,php不解析
今天在配置docker配置apache+php运行环境时遇到一个问题,就是php不解析,直接源码展示
先说一下我的配置环境,我是直接安装centos7镜像,用dockerfile安装apache和php,转换成一个适合自己的用镜像,以下是我的dockerfile
FROM centos:latest
MAINTAINER hyl <[email protected]>
#清掉yum缓存
RUN yum -y update; yum clean all
#安装httpd
RUN yum install -y httpd
#添加epel源,php需要
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/epel-release.rpm
RUN rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm
#安装php7.0
#RUN yum install -y --enablerepo="webtatic" php70w php70w-cli php70w-common php70w-gd php70w-mbstring php70w-pdo php70w-xml php70w-mysql
#安装php7.2
RUN yum install -y --enablerepo="webtatic" mod_php72w php72w-cli php72w-common php72w-gd php72w-mbstring php72w-pdo php72w-xml php72w-mysql
#清掉yum缓存,及系统临时文件
RUN yum clean all
RUN rm -rf /tmp/*
RUN rm -rf /var/cache/yum/*
#删除测试配置及测试文件
RUN rm -f /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.d/autoindex.conf /etc/httpd/conf.d/userdir.conf /var/www/error/noindex.html
#开放80端口
EXPOSE 80
#制作启动文件
CMD ["-D","FOREGROUND"]
ENTRYPOINT ["/usr/sbin/httpd"]
上网找了很多解决方案,首先找到就需要在httpd.conf 添加php加载
进入到对应的docker容器执行以下命令,找到httpd.conf 所在目录,一般不去就够都会默认在 /etc/httpd/conf/httpd.conf
find / -name httpd.conf
添加一行内容:AddType application/x-httpd-php .php
找到配置文件方法同上,找到DirectoryIndex index.html
改为DirectoryIndex index.html index.htm index.php
修改完以上后再看仍然不行,然后我又看了别的资料说要开启防火墙,重装php等等,但多番尝试仍然不行
最终我找到了因为未加载php5_module模块
解决办法:添加模块
在配置文件中添加:LoadModule php5_module modules/libphp5.so
重启后仍然不行,并且发现docker无法启动,用docker logs 查看后,发现原来是因为我的 httpd/modules 为空,问题就在这里,docker run的时候绑定主机目录 /httpd ,而这个目录下缺少 modules/libphp5.so,由于docker绑定目录的规则是由主机覆盖容器目录的,所以在容器新建的时候无法在 httpd/modules 目录下创建 modules 缺少该文件,最后将主机目录绑定目录改成 只绑定vhost.conf 和 httpd.conf 文件就解决了。另外 httpd.conf 不需要添加 LoadModule php5_module modules/libphp5.so ,因为apache启动会自动加载的。