实现方式
通过更改两个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的虚拟网卡。
services:
nginx:
container_name: nginx
image: nginx:latest
restart: always
environment:
- TZ=Asia/Shanghai
ports:
- 80:80
- 443:443
volumes:
- /website/html/:/usr/share/nginx/html/
- /website/conf/:/etc/nginx/conf.d/
- /website/log/:/var/log/nginx/
- /website/doc_html/:/usr/share/nginx/doc_html/
- /website/lnmp/nginx.conf:/etc/nginx/nginx.conf
php7.4:
container_name: php7.4
image: php:7.4-fpm
volumes:
- /website/doc_html/:/usr/share/nginx/doc_html/
- /website/php_conf/:/usr/local/etc/php
network_mode: "container:nginx"
working_dir: /usr/share/nginx/html
restart: always
depends_on:
- nginx
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
{/collapse-item}
{collapse-item label="nginx配置文件"}
server {
if (!-e $request_filename) {
rewrite ^(.*)$ /index.php$1 last;
}
listen 443 ssl;
server_name zhangmingrui.cool www.zhangmingrui.cool;
server_tokens off;
keepalive_timeout 5;
root /usr/share/nginx/html;
index index.php index.html;
ssl_certificate /etc/nginx/conf.d/zhangmingrui.cool_bundle.pem;
ssl_certificate_key /etc/nginx/conf.d/zhangmingrui.cool.key;
ssl_session_timeout 5m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:HIGH:!aNULL:!MD5:!RC4:!DHE;
ssl_prefer_server_ciphers on;
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;
}
{/collapse-item}
评论 (0)