本文分享的是利用docker搭建php7和nginx运行环境的全过程,分享出来供大家参考学习,下面来看看详细的介绍:
环境介绍
根目录: /docker
网站根目录:/docker/www
nginx相关目录:/docker/nginx/conf.d
准备工作
1、使用docker加速器
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://68abbefd.m.daocloud.ioservice docker restart2、下载相关镜像
docker pull nginxdocker pull php:7.1.0-fpm3、建立相关目录
mkdir -p /docker/wwwmkdir -p /docker/nginx/conf.d4、编辑default.conf
vim /docker/nginx/conf.d/default.conf# 以下为示例内容server {listen80 default_server;server_name _;root/usr/share/nginx/html;location / {index index.html index.htm index.php;autoindex off;}location ~ \.php(.*)$ {root/var/www/html/;fastcgi_pass 172.17.0.2:9000;fastcgi_index index.php;fastcgi_split_path_info ^((?U). \.php)(/?. )$;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;includefastcgi_params;}}搭建环境
1、启动php镜像
docker run -p 9000:9000 --name myphp \-v /docker/www/:/var/www/html/ \--privileged=true \-d php:7.1.0-fpm#查看php镜像的ip地址docker inspect --format='{{.NetworkSettings.IPAddress}}' myphp172.17.0.2#修改default.conf配置文件,使fastcgi_pass的值为 172.17.0.2:9000vim /docker/nginx/conf.d/default.conffastcgi_pass 172.17.0.2:9000;2、启动nginx镜像
docker run -p 80:80 --name mynginx \-v /docker/www:/usr/share/nginx/html \-v /docker/nginx/conf.d:/etc/nginx/conf.d \--privileged=true \-d nginx3、查看镜像运行状态
docker psCONTAINER IDIMAGECOMMANDCREATEDSTATUSPORTSNAMES93213e1eac73nginx"nginx -g 'daemon off" 3 seconds agoUp 2 seconds0.0.0.0:80->80/tcp mynginxe93281652098php:7.1.0-fpm"docker-php-entrypoin" 8 minutes agoUp 8 minutes0.0.0.0:9000->9000/tcpmyphp4、生成php测试文件info.php
echo " /docker/www/info.php浏览器访问 http://localhost/info.php 验证
nginx虚拟机配置
以配置www.test.com虚拟机为例,项目目录地址为/docker/www/test.com/
vim /docker/nginx/conf.d/test.com.conf# 示例内容如下server {listen80;server_name www.test.com;root/usr/share/nginx/html/test.com/;location / {index index.html index.htm index.php;autoindex off;}location ~ \.php(.*)$ {root/var/www/html/test.com/;fastcgi_pass 172.17.0.2:9000;fastcgi_index index.php;fastcgi_split_path_info ^((?U). \.php)(/?. )$;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_param PATH_INFO $fastcgi_path_info;fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;includefastcgi_params;}}#重启nginx镜像docker restart mynginxdocker常用命令
1、停止所有正在运行的容器
docker kill $(docker ps -a -q)2、删除所有已停止运行的容器
docker rm $(docker ps -a -q)3、查看容器运行状态
docker stats4、进入容器内进行命令行操作
docker exec -it content-name-or-id /bin/bash常见问题
CentOS7 环境下因为宿主的SELINUX,导致在nginx容器内无法访问配置文件(default.conf),进而容器无法提供web服务
解决方法:
【如何利用docker搭建php7和nginx运行环境全过程】#############方法一##############在宿主主机关闭SELINUX#临时关闭setenforce 0#永久关闭 修改/etc/sysconfig/selinux文件SELINUX=disabled#############方法二##############以特权方式运行容器#--privileged参数为truedocker run -it --privileged=true -d nginx