本文分享的是利用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 restart
2、下载相关镜像
docker pull nginxdocker pull php:7.1.0-fpm
3、建立相关目录
mkdir -p /docker/wwwmkdir -p /docker/nginx/conf.d
4、编辑default.conf
vim /docker/nginx/conf.d/default.conf# 以下为示例内容server { listen 80 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; include fastcgi_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 nginx
3、查看镜像运行状态
docker psCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES93213e1eac73 nginx "nginx -g 'daemon off" 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp mynginxe93281652098 php:7.1.0-fpm "docker-php-entrypoin" 8 minutes ago Up 8 minutes 0.0.0.0:9000->9000/tcp myphp
4、生成php测试文件info.php
echo "<?php phpinfo();" > /docker/www/info.php
浏览器访问 http://localhost/info.php 验证
nginx虚拟机配置
以配置www.test.com虚拟机为例,项目目录地址为/docker/www/test.com/
新闻热点
疑难解答