安裝部署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 目錄下創(chuàng)建 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 監(jiān)聽端口這個端口號,服務器一般配置為127.0.0.1:9000,在docker容器里我們必須要查看自己的PHP容器IP:
docker inspect 容器ID或容器名 |grep '"IPAddress"'
PHP容器IP地址
改完配置文件記得重啟服務。