nginx根据php监听端口号不同分配不同版版的php-fpm服务
标签搜索

nginx根据php监听端口号不同分配不同版版的php-fpm服务

mrui
2025-03-03 / 0 评论 / 58 阅读 / 正在检测是否收录...

实现方式

通过更改两个php-fpm服务监听的端口号来实现php-fpm的同时运行。

docker容器下更改php-fpm端口的方式

#先进入php-fpm容器查找php-fpm的配置文件
docker exec -it  php8.4 bash
find / -maxdepth 8 -name '*.conf' | xargs grep 9000
/opt/bitnami/php/etc/php-fpm.d/www.conf:listen = 9000
#由于使用的docker容器不同,php-fpm的配置文件所在的目录也可能有差异,因此使用find命令来进行查找。当然,也可以尝试使用whereis命令来查找。
root@9f78325f4f95:/app# whereis php-fpm
php-fpm: /opt/bitnami/php/sbin/php-fpm
#这样就可以知道php-fpm所在的文件夹位置,然后进入这个文件夹逐层寻找即可。
#先查看下这个配置文件的内容,使用cat或者grep查看(容器内more、less、vi、vim等命令不可用)
root@9f78325f4f95:/app# grep -a5 9000 /opt/bitnami/php/etc/php-fpm.d/www.conf
;                            a specific port;
;   'port'                 - to listen on a TCP socket to all addresses
;                            (IPv6 and IPv4-mapped) on a specific port;
;   '/path/to/unix/socket' - to listen on a unix socket.
; Note: This value is mandatory.
listen = 9000

; Set listen(2) backlog.
; Default Value: 511 (-1 on Linux, FreeBSD and OpenBSD)
;listen.backlog = 511
#通过查看上下5行的内容,发现这是一个标准的php配置文件。
#将www.conf文件从容器内拷贝到本机。
docker cp 361618e74380:/opt/bitnami/php/etc/php-fpm.d/www.conf   /website/php-fpm_conf/
#使用sed命令将端口改为8000
sed -i /^listen/s/9/8/ /website/php-fpm_conf/php-fpm.d/www.conf

修改nginx配置文件,将php-fpm端口改为8000

vim nginx.conf
    location ~ \.php(\/.*)*$ {
        root         /usr/share/nginx/html;
        fastcgi_pass   127.0.0.1:8000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  PATH_INFO $fastcgi_path_info;
        include        fastcgi_params;
        client_max_body_size 20m;
        fastcgi_connect_timeout 30s;
        fastcgi_send_timeout 30s;
        fastcgi_read_timeout 30s;
        fastcgi_intercept_errors on;

修改docker compose的yaml文件

主要是将修改的www.conf文件所在文件夹挂载到容器中。

……
php8.2:
    container_name: php8.2
    image: docker.1ms.run/bitnami/php-fpm:latest
    volumes:
      - /website/html/:/usr/share/nginx/html
      - /website/php_conf/:/usr/local/etc/php
      - /website/php-fpm_conf:/opt/bitnami/php/etc/php-fpm.d/
    network_mode: "container:nginx"
    working_dir: /usr/share/nginx/html
    restart: always
    depends_on:
      - nginx
……

备注:为了nginx方便与php通信,这里让php使用了nginx的虚拟网卡。

0

评论 (0)

取消