docker搭建php开发环境

docker搭建php开发环境

浏览次数:
信息来源: 用户投稿
更新日期: 2026-05-11
文章简介

一.创建带有swoole-redis-pdo_mysql-gd扩展的dockerimagevimdockerfile2.在dockerfile文件写入 runapt-getupdateap

2025阿里云双十一服务器活动

一.创建带有swoole-redis-pdo_mysql-gd扩展的dockerimage

vimdockerfile

2.在dockerfile文件写入

runapt-getupdate&&apt-getinstall-y\

&&docker-php-ext-install-j$(nproc)iconv\

&&docker-php-ext-configuregd--with-freetype-dir=/usr/include/--with-jpeg-dir=/usr/include/\

&&docker-php-ext-install-j$(nproc)gd\

&&docker-php-ext-configurepdo_mysql\

&&docker-php-ext-installpdo_mysql\

&&peclinstallredis-4.3.0\

&&docker-php-ext-enableredisswoole

3.创建自定义的php镜像,主要不要漏了最后的'.',是指定当前目录构建镜像

dockerbuild-tmyphp4.

运行指令,由于网络问题等,需要等比较长的时间,成功后会出现类似下面的代码

...buildprocesscompletedsuccessfullyinstalling'/usr/local/include/php/ext/swoole/config.h'installing'/usr/local/lib/php/extensions/no-debug-non-zts-20160303/swoole.so'installok:channel://pecl.php.net/swoole-4.3.1configurationoption"php_ini"isnotsettophp.inilocationyoushouldadd"extension=swoole.so"tophp.iniremovingintermediatecontainerad1420f7554f--->2f2f332d73cesuccessfullybuilt2f2f332d73cesuccessfullytaggedmyphp4:latest

至此docker的自定义myphp4image创建成功!

二.创建docker-compose.yml文件

vimdocker-compose.yml

写入下面代码

-/var/www/html:/usr/share/nginx/html

-/var/www/html:/usr/share/nginx/html

image:"redis:4.0"

运行指令

docker-composeup-d

成功可以看到

warning:thedockerengineyou'reusingisrunninginswarmmode.

composedoesnotuseswarmmodetodeployservicestomultiplenodesinaswarm.allcontainerswillbescheduledonthecurrentnode.

todeployyourapplicationacrosstheswarm,use`dockerstackdeploy`.

creatingnetwork"pnsmr_default"withthedefaultdrivercreatingpnsmr_php-fpm_1...donecreatingpnsmr_redis_1...donecreatingpnsmr_mysql_1...donecreatingpnsmr_nginx_1...done

至此,已开启nginxmysqlredisphp服务

三.修改各服务配置文件

1.浏览器输入127.0.0.1:9998#此处应输入你的服务器ip地址,可以看到下图

docker搭建php开发环境

dockerinspect-f'{{.name}}-{{range.networksettings.networks}}{{.ipaddress}}{{end}}'$(dockerps-aq)

此指令可以查看所有用docker-compose开启的容器的ip,结果类似下图,可以用对应的ip地址进行内部通讯

3.复制nginx容器的配置文件出来,并修改替换,使nginx能解析php

dockercppnsmr_nginx_1:/etc/nginx/conf.d/default.confnginx.conf

vimnginx.conf

修改为下列代码

#access_log/var/log/nginx/host.access.logmain;

root/usr/share/nginx/html;

indexindex.htmlindex.htm;

#redirectservererrorpagestothestaticpage/50x.html

error_page500502503504/50x.html;

root/usr/share/nginx/html;

#proxythephpscriptstoapachelisteningon127.0.0.1:80

#proxy_passhttp://127.0.0.1;

#passthephpscriptstofastcgiserverlisteningon127.0.0.1:9000

fastcgi_pass172.24.0.3:9000;#此处需要填写你的php容器的docker内部通讯ip

fastcgi_paramscript_filename/usr/share/nginx/html/$fastcgi_script_name;

#denyaccessto.htaccessfiles,ifapache'sdocumentroot

dockercpnginx.confpnsmr_nginx_1:/etc/nginx/conf.d/default.conf#将修改好的配置文件拷贝到容器里

dockercontainerstoppnsmr_nginx_1

dockercontainerstartpnsmr_nginx_1#重启nginx容器使配置文件生效

vim/var/www/html/index.php#在服务器本地目录新建index.php文件,输入<?phpphpinfo();并保存

vim/var/www/html/index.html用于测试redis是否配置成功

$redis->connect("172.24.0.4",6379);

$redis->set('test','thisisatestforredis');

echo$redis->get('test');

访问127.0.0.1:9998/redis.php,redis已生效

dockerexec-itpnsmr_mysql_1bash

进入mysql并更改root用户密码

vim/var/www/html/mysql.php

$pdo=newpdo('mysql:host=172.24.0.2;dbname=mysql;port=3306','root','root123');

var_dump($pdo);

访问127.0.0.1:9998/mysql.php,mysql已生效

以上就是怎么使用docker搭建php+nginx+swoole+mysql+redis环境的详细内容,更多请关注主机测评网其它相关文章!

标签:
什么是VMware(什么是vmware虚拟机)
« 上一篇
返回列表
下一篇 »

如本文对您有帮助,就请抽根烟吧!