安装部署php+nginx
查找Docker Hub上的php镜像
docker search php
这里我拉取官方的镜像,标签为7.0-fpm
docker pull php:7.0-fpm
等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为php,标签为5.6-fpm的镜像
docker images
启动PHP
// 将项目目录挂载到nginx项目目录
docker run --name summer-php -v /www:/usr/share/nginx/html -d php:7.0-fpm
启动 Nginx
// 把 summer-php 的网络并入 nginx (把php并入nginx) 并把对应的目录挂载上
docker run --name summer-php-nginx -p 8083:80 -d -v /www:/usr/share/nginx/html:ro -v /conf/conf.d:/etc/nginx/conf.d:ro -v /logs:/var/log/nginx --link summer-php:php nginx
接下来我们在/www 目录下创建 index.php,代码如下:
<?php
echo phpinfo();
?>
--------------------------------划重点------------------------------------------
此时访问192.168.99.100:8083,会出现404 或者file not found;
解决方法:
在上一章我们拷贝了nginx容器内站点配置目录到本地目录下的 conf 目录,此时本地conf/conf.d目录下会有一个默认配置文件default.conf,打开此文件进行修改nginx配置:
server {
listen 80;
server_name localhost;
root /usr/share/nginx/html;
index index.html index.htm index.php;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm index.php;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
# pass the PHP scripts to FastCGI server
location ~ \.php$ {
fastcgi_pass 172.17.0.2:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
其中说明一下:fastcgi_pass 监听端口这个端口号,服务器一般配置为127.0.0.1:9000,在docker容器里我们必须要查看自己的PHP容器IP:
docker inspect 容器ID或容器名 |grep '"IPAddress"'
PHP容器IP地址
改完配置文件记得重启服务。