1. 先安装docker 和docker-composer
1.1安装docker 直接使用快捷一键安装
$ curl -fsSL get.docker.com -o get-docker.sh
$ sudo sh get-docker.sh --mirror Aliyun
1.2等待一分钟docker安装完成启动docker
$ sudo systemctl enable docker
$ sudo systemctl start docker
1.3 测试docker 安装 和镜像加速
直接参考文章安装和调试 CentOS 安装 Docker | Docker 从入门到实践
2.安装php 运行环境
2.1 选用最简单 的拥有 php-fpm 和 nginx 的 镜像 webdevops/php-nginx webdevops/php-nginx — Dockerfile Documentation documentation 直接使用 docker pull 拉取最新镜像
docker pull webdevops/php-nginx
2.2 拉完了之后docker images 查看镜像文件是否成功
docker images
显示如下就说明pull 下来了
2.3 运行下webdevops/php-nginx的镜像文件
docker run -itd --name php-ngins-thinkphp6 -p 89:80 -v /home/guyi/www/thinkphp6/:/app/ webdevops/php-nginx
-itd 交互终端方式打开具体参数解读可阅读该篇文章 docker run参数详解_docker run 参数详解_日日行不惧千万里的博客-CSDN博客
--name 给容器取一个名字
-p 89 :就是虚拟机可以访问的端口(后面 ip:89 就可以访问) 80是容器里面的80端口
-v /home/guyi/www/thinkphp6/ 虚拟机目录映射的目录(指向放项目的目录即可) /app/ 虚拟机 的容器路径。 还可增加 配置文件目录 修改配置文件的时候就无须进入容器修改: -v nginx 的 配置目录/conf.d : /opt/docker/etc/nginx/conf.d 其他挂载配置路径看下表
容器内php 配置文件路径
File/DirectoryDescription/opt/docker/etc/php/php.webdevops.iniPHP settings from WebDevOps image/opt/docker/etc/php/php.ini php.ini for custom settings/opt/docker/etc/php/fpm/php-fpm.confPHP-FPM main configuration file/opt/docker/etc/php/fpm/pool.d/application.confApplication PHP-FPM pool configuration file/opt/docker/etc/supervisor.d/php-fpm.confSupervisord configuration file for PHP-FPM
容器内nginx 配置路径
File/DirectoryDescription/opt/docker/etc/nginx/conf.d Main global configuration directory (automatically included files) /opt/docker/etc/nginx/conf.d/10-php.confPHP cgi configuration/opt/docker/etc/nginx/ssl SSL configuration directory for certifications and keys /opt/docker/etc/nginx/ssl/server.crt Example SSL certification (*.vm)/opt/docker/etc/nginx/ssl/server.csr Example SSL certification request (*.vm)/opt/docker/etc/nginx/ssl/server.key Example SSL key (.vm)/opt/docker/etc/nginx/vhost.common.d Vhost configuration directory (automatically included files) /opt/docker/etc/nginx/vhost.common.d/10-location-root.confRedirect requests to DOCUMENT_INDEX/opt/docker/etc/nginx/vhost.common.d/10-php.confPHP cgi configuration for vhost/opt/docker/etc/nginx/global.confGlobal nginx configuration/opt/docker/etc/nginx/main.confMain Nginx configuration/opt/docker/etc/nginx/php.confDeprecated PHP configuration/opt/docker/etc/nginx/vhost.common.confDeprecated vhost common include/opt/docker/etc/nginx/vhost.confVhost configuration/opt/docker/etc/nginx/vhost.ssl.confVhost SSL configuration/opt/docker/etc/supervisor.d/nginx.confSupervisord configuration file for Nginx
webdevops/php-nginx 镜像名字
2.4 运行后 访问虚拟机ip:89 访问192.168.199.1:89 或者 直接curl ip:89 出现centos 的欢迎页面即可 。到此环境搭建好了。
3. 安装 thinkphp6 框架 可选择 直接下载源码放到 run 挂载的目录下
或者 cd /home/guyi/www/thinkphp6 下 直接用官网的composer 安装 tp6框架
composer create-project topthink/think thinkphp6
4.项目搭建好后需要在nginx 配置文件中修改入口目录
4.1 挂载过 /opt/docker/etc/nginx/vhost.conf 直接在虚拟机对应目录修改即可
4.2运行时run 没有 -v 挂载的直接 进入容器修改
docker ps //查看虚拟机的容器id 或者名字
docker exec -it 78b4726227a1 /bin/bash //进入容器的命令 78b4726227a1 容器的id
vim /opt/docker/etc/nginx/vhost.conf //修改vhost.conf
server { listen 80 default_server;
server_name _ *.vm docker;
root "/app/public"; index index.php; # 隐藏 index.php 以及使得Nginx支持pathinfo模式 if (!-e $request_filename) { rewrite ^/index.php(.*)$ /index.php?s=$1 last; rewrite ^(.*)$ /index.php?s=$1 last; }
include /opt/docker/etc/nginx/vhost.common.d/*.conf; }
############## # SSL ##############
server { listen 443 default_server;
server_name _ *.vm docker;
root "/app/public"; index index.php;
include /opt/docker/etc/nginx/vhost.common.d/*.conf; include /opt/docker/etc/nginx/vhost.ssl.conf; }
进入后 按 i 进入插入模式 将配置文件放入 按esc 输入 :wq!
然后 exit 退出容器 重启下nigix docker restart 78b4726227a1
重启后访问 ip:89 出现tp6欢迎页面 至此 tp6搭建完成
5.安装swoole 最简单的方式实现
5.1 拉取swoole 镜像 版本可自行配置
docker pull phpswoole/swoole:4.6.2-php7.3
5.2 测试镜像环境:
docker run --rm phpswoole/swoole:4.6.2-php7.3 "php -m"
docker run --rm phpswoole/swoole:4.6.2-php7.3 "php --ri swoole"
docker run --rm phpswoole/swoole:4.6.2-php7.3 "composer --version
5.3 建一个websocket 启动方法 更多方法查看git或者官方文档:
swoole-src: Coroutine-based concurrency library for PHP - Gitee.comj
创建一个server.php 放到你指定的swoole的虚拟机目录
on('open',function(swoole_websocket_server $server,$request){ echo "server: handshake success with fd{$request->fd}\n"; }); $server->on('message',function(swoole_websocket_server $server,$frame){ echo "receive from {$frame->fd}:{$frame->data}"; echo "opcode:{$frame->opcode}"; echo "fin: {$frame->finish}\n"; // $server->push($frame->fd,"this is a server"); foreach ($server->connections as $fd) { // 需要先判断是否是正确的websocket连接,否则有可能会push失败 if ($server->isEstablished($fd)) { $server->push($fd, $frame->data); } } }); $server->on('close',function($ser,$fd){ echo "client {$fd} closed\n"; }); $server->start();
5.4 启动swoole容器
docker run --rm -p 9502:9502 --name swoole -v /usr/local/swoole/:/var/www phpswoole/swoole:4.6.2-php7.3
/usr/local/swoole/ 就是你刚刚创建的 server.php 的虚拟机目录
5.5测试访问:
curl http://127.0.0.1:9502
容器启动后会尝试执行 php server.php 命令启动服务,所以无需手动进入容器执行,出现问题请进入容器目录 执行 php server.php 命令; 到此swoole 运行成功了。
网上copy 了段弹幕代码:
精彩内容
发表评论