Docker配置PHP开发环境教程
前言
笔者用的是mac开发,但是mac自带的php功能安装十分不方便,并且和线上的linux开发环境不一致。在没有用docker之前一直用vagrant配置的centos的php开发环境,但是自从有了docker之后,就不再用vagrant了。
配置自己的php镜像
首先在自己的任意一个目录下创建如下三个文件
run.sh
#!/bin/bash /usr/sbin/php-fpm7.0 /usr/sbin/nginx tailf/etc/apt/sources.list
sources.list
#debcdrom:[Ubuntu16.04LTS_XenialXerus_-Releaseamd64(20160420.1)]/xenialmainrestricted deb-srchttp://archive.ubuntu.com/ubuntuxenialmainrestricted#Addedbysoftware-properties debhttp://mirrors.aliyun.com/ubuntu/xenialmainrestricted deb-srchttp://mirrors.aliyun.com/ubuntu/xenialmainrestrictedmultiverseuniverse#Addedbysoftware-properties debhttp://mirrors.aliyun.com/ubuntu/xenial-updatesmainrestricted deb-srchttp://mirrors.aliyun.com/ubuntu/xenial-updatesmainrestrictedmultiverseuniverse#Addedbysoftware-properties debhttp://mirrors.aliyun.com/ubuntu/xenialuniverse debhttp://mirrors.aliyun.com/ubuntu/xenial-updatesuniverse debhttp://mirrors.aliyun.com/ubuntu/xenialmultiverse debhttp://mirrors.aliyun.com/ubuntu/xenial-updatesmultiverse debhttp://mirrors.aliyun.com/ubuntu/xenial-backportsmainrestricteduniversemultiverse deb-srchttp://mirrors.aliyun.com/ubuntu/xenial-backportsmainrestricteduniversemultiverse#Addedbysoftware-properties debhttp://archive.canonical.com/ubuntuxenialpartner deb-srchttp://archive.canonical.com/ubuntuxenialpartner debhttp://mirrors.aliyun.com/ubuntu/xenial-securitymainrestricted deb-srchttp://mirrors.aliyun.com/ubuntu/xenial-securitymainrestrictedmultiverseuniverse#Addedbysoftware-properties debhttp://mirrors.aliyun.com/ubuntu/xenial-securityuniverse debhttp://mirrors.aliyun.com/ubuntu/xenial-securitymultiverse
Dockerfile
FROMubuntu:16.04 #=========================== #配置虚拟主机 #-vdefault:/etc/nginx/sites-enabled/default #配置程序目录 #-vweb:/var/www/html #配置映射端口 #-p8008:80 #=========================== MAINTAINERchengtao"751753158@qq.com" ADDsources.list/etc/apt/sources.list ADDrun.sh/root/run.sh RUNchmod+x/root/run.sh RUNapt-getupdate RUNapt-getinstall-yphp-fpmphp-mysqlnginx RUNsed-i's/;date.timezone=/date.timezone=Asia\/Shanghai/'/etc/php/7.0/fpm/php.ini RUNmkdir-p/run/php/ EXPOSE80 CMD["/bin/bash","/root/run.sh"]
执行命令
dockerbuild-td1studio:php-base:0.1.
配置phpmysql开发环境
mkdir-p~/projects/php-app cd~/projects/php-app mkdirmysql mkdirwww
www/index.php
<?php phpinfo();
nginx.conf
server{ listen80default_server; root/var/www/html; indexindex.htmlindex.htmindex.php; location/{ try_files$uri$uri/=404; } location~\.php${ includesnippets/fastcgi-php.conf; fastcgi_passunix:/run/php/php7.0-fpm.sock; } }
docker-compose.yml
version:'2' services: mysql: image:mysql:5.6 volumes: -./mysql/:/var/lib/mysql/ ports: -"3307:3306" environment: -MYSQL_ROOT_PASSWORD=123456 php-app: image:d1studio/php-base:0.1 ports: -"8009:80" volumes: -./nginx.conf:/etc/nginx/sites-enabled/default -./www/:/var/www/html/ links: -mysql
开启php的测试项目
#开启 docker-composeup #关闭 docker-composedown
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。