配置需要 nginx和php环境

我们先拉取分支

1
2
3
4
5
# php环境分支
docker pull php:8.1.0RC1-fpm-alpine3.14

# nginx 环境分支
docker pull nginx

启动php容器

1
2
#启动 php 并且挂载文件夹
docker run -d -v /opt/php/www:/var/www/html -p 9000:9000 --name php 你的镜像id

查看我们php容器启动的状态

查看PHPfpm端口9000是否启动(这步很重要,没启动的话需要重装php镜像)

1
netstat -tunlp | grep 9000

netstat netstat -tunlp 用于显示 tcp,udp 的端口和进程等相关情况 netstat 查看端口占用语法格式 netstat -tunlp | grep 端口号

-t (tcp) 仅显示tcp相关选项
-u (udp)仅显示udp相关选项
-n 拒绝显示别名,能显示数字的全部转化为数字
-l 仅列出在Listen(监听)的服务状态
-p 显示建立相关链接的程序名

启动Nginx容器

  1. 启动之前我们需要配置一下配置文件,用来转发请求到php服务器 将文件防止在 /opt/nginx/conf 目录下即可

    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
        server {

    # 设置允许跨域请求
    add_header 'Access-Control-Allow-Origin' '*';

    listen 80;
    server_name localhost;

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

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
    root /usr/share/nginx/html;
    }


    location ~ [^/]\.php(/|$) {
    fastcgi_pass php:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
    include fastcgi_params;
    }


    }

启动 nginx 容器

1
docker run --name nginx -p 80:80 -d -v /opt/nginx/www:/usr/share/nginx/html:ro -v /opt/nginx/conf:/etc/nginx/conf.d:ro --link php nginx

启动 nginx 容器 映射80端口,挂载容器目录到本地指定的目录 关联php的网络

  1. 接下来我们在 /opt/php/www 目录下创建 index.php,代码如下

    1
    2
    3
    <?php
    echo phpinfo();
    ?>

最后我们访问一下我们的 index.php

你的ip地址 + index.php

注意如果你要搭建live2d api

注意nginx /usr/share/nginx/html根目录跟php /var/www/html文件目录都要有源文件