当前位置:   article > 正文

docker 启动 php-fpm和nginx 的两种连接方式_docker php fpm nginx

docker php fpm nginx

第一种方式 使用IP

启动PHP

docker run --name php -v /nas/k8s_doc/nas/www:/usr/share/nginx/html -p 9000:9000 -d   php:7.4-fpm-alpine 
73e69f48970a7251ec6cd119c2c9dade712622e9bef9b6bb6a43d564e3a1bcc6
  • 1
  • 2

执行:
docker inspect php
获取到addres:

[root@master1 nas]# docker inspect php| grep "IPAddress"
            "IPAddress": "172.17.0.3",
                    "IPAddress": "172.17.0.3",
  • 1
  • 2
  • 3

修改default中的fastcgi_pass中的IP为这个IP:
172.17.0.3

default.conf

server {
    listen       80;
    listen  [::]:80;
    server_name  localhost;

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

    location / {
        root   /usr/share/nginx/html;
        index  index.php index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    
    location ~ \.php$ {
       root           html;
       fastcgi_pass   172.17.0.3:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
       fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
       include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45

启动NGINX

docker run --name nginx -v /nas/k8s_doc/nas/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /nas/k8s_doc/nas/www:/usr/share/nginx/html -v /nas/k8s_doc/nas/nginx/logs:/var/log/nginx -d -p 8080:80 nginx:stable-alpine
  • 1

这种方式需要手动修改IP,方法二,采用DOCKER network 来自动解析名称:

方法二:docker network 自动解析名称

# 查看网络

[root@master1 nas]# docker network ls
NETWORK ID          NAME                DRIVER              SCOPE
eaae0152cb16        bridge              bridge              local
c664bcc9d002        host                host                local
b44256c6abd1        none                null                local
  • 1
  • 2
  • 3
  • 4
  • 5

创建自定义网络:

docker network create my-net

上述命令中加上:–network my-net

修改default.conf fastcgi_pass 中的IP为 php:

  location ~ \.php$ {
       root           html;
       fastcgi_pass   php:9000;
       fastcgi_index  index.php;
       fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
       fastcgi_param  SCRIPT_NAME      $fastcgi_script_name;
       include        fastcgi_params;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

删除后重新启动

[root@master1 nas]# docker rm -f nginx
nginx
[root@master1 nas]# docker rm -f php

docker run --name nginx -v /nas/k8s_doc/nas/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /nas/k8s_doc/nas/www:/usr/share/nginx/html -v /nas/k8s_doc/nas/nginx/logs:/var/log/nginx -d -p 8080:80 --network my-net  nginx:stable-alpine

docker run --name nginx -v /nas/k8s_doc/nas/nginx/conf.d/default.conf:/etc/nginx/conf.d/default.conf -v /nas/k8s_doc/nas/www:/usr/share/nginx/html -v /nas/k8s_doc/nas/nginx/logs:/var/log/nginx -d -p 8080:80 --network my-net  nginx:stable-alpine
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

echo “<?php phpinfo():?>” > /nas/k8s_doc/nas/www/info.php

打开浏览器:
http://192.168.132.50:8080/info.php
192.168.132.50 为docker 主机IP
在这里插入图片描述

结语

此文仅用于测试掌握细节,正式环境建议用docker-composer,或者k8s来部署,更加方便。
请看我的另一篇文字:
docker compose 部署 NGINX + PHP

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/715516
推荐阅读
相关标签
  

闽ICP备14008679号