使用docker部署php服务的详细步骤
前言
前期准备,服务器需要先安装好docker、docker-compose,文章内容不涉及如何安装docker的相关内容。
制作的内容,使用nginx+php的新基础镜像部署php服务,然后使用openresty做反向代理。
nginx+php的新基础镜像制作过程,可以参考之前的文章,地址如下:nginx+php的新基础镜像制作全过程_nginx_ (jb51.net)
一、安装openresty
1、创建openresty相关目录,执行如下命令。
mkdir -p /docker/openresty/{conf.d,logs,html,cert}
cd /docker/openresty/
2、编写yaml文件,内容如下;version替换成自己的docker-compose版本。
vim docker-compose.yml
version: '2.2.2'
services:
openresty:
image: openresty/openresty
restart: unless-stopped
ports:
- "80:80"
- "443:443"
container_name: openresty
volumes:
- "./nginx.conf:/usr/local/openresty/nginx/conf/nginx.conf"
- "./conf.d:/usr/local/openresty/nginx/conf/conf.d"
- "./html:/usr/local/openresty/nginx/html"
- "./logs:/usr/local/openresty/nginx/logs"
- "./cert:/usr/local/openresty/nginx/cert"
networks:
- mynet
networks:
mynet:
name: mynet
driver: bridge
3、编写nginx配置,内容如下。
vim nginx.conf
worker_processes 1;
error_log logs/error.log;
events {
worker_connections 1024;
}
http {
server {
listen 80;
location / {
default_type text/html;
content_by_lua_block {
ngx.say("hello, ayzen!$
")
}
}
}
}
4、启动openresty,执行如下命令。
docker-compose up -d
5、检查服务是否正常运行,执行如下命令。
docker-compose ps -a
返回如下内容,说明服务已正常启动,正在运行。

6、检查请求是否正常,执行如下命令。
curl http://127.0.0.1
请求正常,会返回nginx配置的内容,比如下面这样。

7、至此openresty已部署完成,并且可以正常运行响应请求。
二、部署php服务
1、创建test1项目相关目录,执行如下命令。
mkdir -p /docker/test1/html cd /docker/test1/
2、编写index.php文件,内容如下。
vim html/index.php3、编写yaml文件,内容如下;version替换成自己的docker-compose版本。
vim docker-compose.yml version: '2.2.2' services: web: image: ayzen/nginx-php8.3.3 ports: - "8081:80" container_name: test1 command: ["/start.sh"] volumes: - "./html:/usr/local/nginx/html" networks: - mynet networks: mynet: name: mynet driver: bridge4、启动test1项目,执行如下命令。
docker-compose up -d5、检查项目是否正常运行,执行如下命令。
docker-compose ps -a返回如下内容,说明项目已正常启动,正在运行。
6、检查项目请求是否可以正常响应,执行如下命令。
curl http://127.0.0.1:8081/index.php请求正常会返回如下内容。
7、至此test1项目已正常部署完成。
三、项目配置对外提供服务
1、前面的test1项目虽然可以正常提供服务了,但是也只限制与内网当中;如果需要对外提供服务需要加上openresty配合。
2、修改nginx配置增加域名请求,在http模块增加server内容如下。
server { listen 80; server_name test1.ayzen.cn; location / { proxy_pass http://test1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; } }3、重启openresty,执行如下命令。
docker-compose down && docker-compose up -d4、域名解析,将test1.ayzen.cn指向服务器IP;此刻没有域名的可以通过修改hosts实现,执行如下命令。
vim /etc/hosts #增加一行内容如下 127.0.0.1 test1.ayzen.cn5、验证请求是否可以正常代理到test1容器,执行如下命令。
curl http://test1.ayzen.cn/index.php如果请求正常返回如下内容,说明配置已生效。
6、至此,代理配置已完成,test1可以正常对外提供服务了。
总结
如何使用docker部署php服务,简单来说只需要三个步骤。
1、使用docker运行openresty容器;
2、部署php服务;
3、配置域名;
因为演示的原因,php项目只有一个index.php文件。在使用过程中可以替换成真正的项目代码。
栏 目:其它服务器
下一篇:docker容器运行成功但无法访问的原因分析及解决方案(以Tomcat为例亲测有效)
本文标题:使用docker部署php服务的详细步骤
本文地址:https://zz.feitang.co/server/28495.html
您可能感兴趣的文章
- 12-31Shell脚本中获取进程ID的方法
- 12-31Shell脚本美化登录界面装饰图(含农历)
- 12-31Shell 编程:Bash空格的那点事
- 12-31Shell中获取脚本所在目录绝对路径的方法
- 12-31Shell脚本实现乱序排列文件内容的多种方法(洗牌问题)
- 12-31shell 编程中空格的使用方法
- 12-31Shell脚本实现ftok函数
- 12-31getcwd cannot access parent directories错误解决方法
- 12-31Shell脚本实现递归删除空文件夹
- 12-31Shell脚本实现获取网页快照并生成缩略图









