分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

基于Docker部署PHP7开发环境

发布时间:2023-09-06 01:41责任编辑:熊小新关键词:PHPDocker

为什么使用Docker

  • 一处编译,到处使用
  • 迁移方便,和宿主环境无关
  • 环境和宿主机隔离
  • 快速部署开发环境

很常用的一个场景:换了电脑后,每次都需要重新安装PHP开发环境,浪费了很多时间。使用Docker,只需要预先编译一次,后续无需再次编译,就算从Windows换到了Mac、Linux,我们编译好的环境依然可以使用:只需要导入或者pull下来就行。

环境要求

  • Docker (Windows使用DockerToolbox代替)
  • Git

DockerToolbox集成了Git、VirtualBox、Docker-machine。

安装docker

官方下载地址:
https://store.docker.com/search?type=edition&offering=community

Unix/Linux上安装

Docker 要求 Ubuntu 系统的内核版本高于 3.10 ,通过 uname -r 命令查看你当前的内核版本:

[root@bogon ~]# ?uname -r3.10.0-327.22.2.el7.x86_64

Docker支持以下的CentOS版本:

  • CentOS 7 (64-bit)
  • CentOS 6.5 (64-bit) 或更高的版本

Docker 支持以下的 Ubuntu 版本:

  • Ubuntu Precise 12.04 (LTS)
  • Ubuntu Trusty 14.04 (LTS)
  • Ubuntu Wily 15.10
  • 其他更新的版本……
curl -fsSL https://get.docker.com/ | sh# daocloud.io 国内镜像curl -sSL https://get.daocloud.io/docker | sh

该安装包适用于 Ubuntu,Debian,Centos 等大部分主流 Linux 发行版。

CentOS7支持使用yum安装:

yum updateyum install docker

安装后查看版本:

docker version

启动docker服务:

service docker start # 或者systemctl start docker

centos7使用systemctl 替代service 管理服务。

Windows上安装

Windows10 专业版/企业版可以下载:https://download.docker.com/win/stable/Docker%20for%20Windows%20Installer.exe 。

其它版本需要使用:Docker Toolbox:https://docs.docker.com/toolbox/overview/ 。实际还是安装了个虚拟机。

DockerToolbox下载地址:https://download.docker.com/win/stable/DockerToolbox.exe

按提示安装即可。

安装完毕打开DockerToolbox, 或者打开Git Bash命令行:

$ docker-machine create --driver=virtualbox defaultRunning pre-create checks...(default2) No default Boot2Docker ISO found locally, downloading the latest release...(default2) Latest release for github.com/boot2docker/boot2docker is v18.01.0-ce(default2) Downloading C:\Users\YJC\.docker\machine\cache\boot2docker.iso from https://github.com/boot2docker/boot2docker/releases/download/v18.01.0-ce/boot2docker.iso...

创建machine的时候回去检查boot2docker.iso是否存在,缓存位置:C:\Users\YJC\.docker\machine\cacheYJC是PC的用户名,以实际为准。boot2docker.iso下载会比较慢,大小约45M。可以提前通过其它方法下载好后放在该目录。

最新的boot2docker.iso下载地址:https://github.com/boot2docker/boot2docker/releases/download/v18.01.0-ce/boot2docker.iso。
为方便下载,网盘也存了一份:链接: https://pan.baidu.com/s/1i6QGIg9 密码: fsmb

如果没有出错,就可以进入这个虚拟机里面的Docker Linux里了:

$ docker-machine start defaultStarting "default"...(default) Check network to re-create if needed...(default) Waiting for an IP...Machine "default" was started.Waiting for SSH to be available...Detecting the provisioner...Waiting for SSH to be available...Detecting the provisioner...Restarted machines may have new IP addresses. You may need to re-run the `docker-machine env` command.$ docker-machine lsNAME ?????ACTIVE ??DRIVER ??????STATE ????URL ????????????????????????SWARM ??DOCKER ???ERRORSdefault ??- ???????virtualbox ??Running ??tcp://192.168.99.101:2376 ??????????Unknown ??Unable to query docker version: Get https://192.168.99.101:2376/v1.15/version: x509: certificate is valid for 192.168.99.100, not 192.168.99.101$ docker-machine ssh default ???????????????????????## ????????. ?????????????????## ## ## ???????== ??????????????## ## ## ## ## ???=== ??????????/"""""""""""""""""\___/ === ?????~~~ {~~ ~~~~ ~~~ ~~~~ ~~~ ~ / ?===- ~~~ ??????????\______ o ??????????__/ ????????????\ ???\ ????????__/ ?????????????\____\_______/ _ ????????????????_ ??____ ????_ ???????????_| |__ ??___ ??___ | |_|___ \ __| | ___ ??___| | _____ _ __| '_ \ / _ \ / _ \| __| __) / _` |/ _ \ / __| |/ / _ \ '__|| |_) | (_) | (_) | |_ / __/ (_| | (_) | (__| ??< ?__/ ||_.__/ \___/ \___/ \__|_____\__,_|\___/ \___|_|\_\___|_|Boot2Docker version 18.01.0-ce, build HEAD : 0bb7bbd - Thu Jan 11 16:32:39 UTC 2018Docker version 18.01.0-ce, build 03596f5docker@default:~$

docker-machine命令:

Commands: ?active ???????????????Print which machine is active ?config ???????????????Print the connection config for machine ?create ???????????????Create a machine ?env ??????????????????Display the commands to set up the environment for the Docker client ?inspect ??????????????Inspect information about a machine ?ip ???????????????????Get the IP address of a machine ?kill ?????????????????Kill a machine ?ls ???????????????????List machines ?provision ????????????Re-provision existing machines ?regenerate-certs ?????Regenerate TLS Certificates for a machine ?restart ??????????????Restart a machine ?rm ???????????????????Remove a machine ?ssh ??????????????????Log into or run a command on a machine with SSH. ?scp ??????????????????Copy files between machines ?start ????????????????Start a machine ?status ???????????????Get the status of a machine ?stop ?????????????????Stop a machine ?upgrade ??????????????Upgrade a machine to the latest version of Docker ?url ??????????????????Get the URL of a machine ?version ??????????????Show the Docker Machine version or a machine docker version ?help ?????????????????Shows a list of commands or help for one command

建议打开VirtualBox给default配置共享目录:

默认会配置c/Users

配置选项:自动挂载、固定分配。

如果需要手动挂载目录:

mount -t vboxsf www /data

/data是容器内挂载点。

部署开发环境

使用php71-fpm-centos68-phalcon-withext

为了使用方便,我已经把 php7.1+fpm+nginx+phalcon 开发环境的镜像编译好了。基于自行编译的 php71-fpm-centos68 镜像,父镜像是 centos:6.8

注:php71-fpm-centos68-phalcon-withextphp71-fpm-centos68-phalcon 功能一样,只是PHP扩展部分采用直接将php71-fpm-centos68-phalcon镜像编译好的so文件复制到镜像内,而省去了编译的过程,且节约了空间(php71-fpm-centos68 800M,php71-fpm-centos68-phalcon 1.4G,php71-fpm-centos68-phalcon-withext 831M)。

php7.1-phalcon的开发环境包含:

  • CentOS 6.8
  • PHP 7.1.12
  • php-fpm
  • Nginx 1.12.2
  • Redis 3.2.6

包含的PHP附加扩展:

  • swoole 1.10.1
  • phpredis 3.0.0
  • yac
  • yar 2.0.2
  • phpalcon 3.3.1
  • xhprof_tideways 4.1.5
  • msgpack
  • protobuf
  • yaconf
  • mongodb
  • seaslog
  • gearman 1.1.18

相关目录:

  • php根目录:/usr/local/php/
  • php可执行程序目录:/usr/local/php/bin/、/usr/local/php/sbin/
  • php配置目录:/usr/local/php/etc/
  • nginx根目录:/usr/local/nginx/
  • nginx可执行程序目录:/usr/local/nginx/sbin/
  • nginx配置目录:/usr/local/nginx/conf/

下载打包好的镜像文件ysphp.tar.gz (239M,密码: nged),然后:

tar xvf ysphp.tar.gzdocker load --input ysphp.tar

或者:

docker pull hub.c.163.com/jiancaigege/php71-fpm-centos68-phalcon-withext:latest

启动容器

确保本地有 /work/目录(Windows请使用VirtualBox共享文件夹)。

1、手动启动容器

  • 使用容器内默认配置:

    docker run -d --restart=always --name ysphp ?-p 80:80 ?-v /work/:/work/ ??php71-fpm-centos68-phalcon-withext && docker ps -a
  • 自定义配置:
    如果需要修改php、nginx配置文件,需要先git clone -b yphp git@github.com:52fhy/docker-images.git。该git仓库里放的是容器内一致的配置文件。可以基于该配置文件进行自定义修改。将docker-images/yphp下的yphp目录拷贝至/work/。然后执行:

    docker run -d --restart=always --name ysphp ?-p 80:80 ?-v /work/:/work/ ??-v "/work/yphp/php/etc/php.ini":/usr/local/php/etc/php.ini ??-v "/work/yphp/nginx/conf/nginx.conf":/usr/local/nginx/conf/nginx.conf ??-v "/work/yphp/nginx/conf/vhost/":/usr/local/nginx/conf/vhost/ ??-v "/work/yphp/nginx/logs/":/usr/local/nginx/logs/ ??php71-fpm-centos68-phalcon-withext && docker ps -a

容器对外暴露80端口。

如果当前环境是Mac或者Linux,直接访问 http://127.0.0.1:80/ 就可以访问容器内的80端口提供的http服务了。
如果当前环境是Docker ToolBox,那么需要获取 所在machine的IP地址,然后访问:http://machine-ip:80/ 即可。

2、使用yphp.sh脚本启动容器

执行:

sh yphp.sh init

即可启动容器服务。

yphp脚本的说明:https://github.com/52fhy/docker-images/tree/yphp

参考资料

1、Docker学习笔记 - 飞鸿影~ - 博客园
http://www.cnblogs.com/52fhy/p/5638571.html
2、docker-images/php71-fpm-centos68-phalcon-withext at master · 52fhy/docker-images
https://github.com/52fhy/docker-images/tree/master/php71-fpm-centos68-phalcon-withext

基于Docker部署PHP7开发环境

原文地址:https://www.cnblogs.com/52fhy/p/8413029.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved