使用docker部署php服务的详细步骤
作者:ayzen1988
前言
前期准备,服务器需要先安装好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: bridge3、编写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("<p>hello, ayzen!$</p>")
            }
        }
    }
}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.php <?php echo "hello ayzen!this is test1!\r\n";
3、编写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 -d
5、检查项目是否正常运行,执行如下命令。
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 -d
4、域名解析,将test1.ayzen.cn指向服务器IP;此刻没有域名的可以通过修改hosts实现,执行如下命令。
vim /etc/hosts #增加一行内容如下 127.0.0.1 test1.ayzen.cn
5、验证请求是否可以正常代理到test1容器,执行如下命令。
curl http://test1.ayzen.cn/index.php
如果请求正常返回如下内容,说明配置已生效。

6、至此,代理配置已完成,test1可以正常对外提供服务了。
总结
如何使用docker部署php服务,简单来说只需要三个步骤。
1、使用docker运行openresty容器;
2、部署php服务;
3、配置域名;
因为演示的原因,php项目只有一个index.php文件。在使用过程中可以替换成真正的项目代码。
到此这篇关于使用docker部署php服务的详细步骤的文章就介绍到这了,更多相关docker部署php服务内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
