docker搭建php-fpm+nginx

docker container run --rm -d --name php-fpm -v $(pwd)/html:/var/www/html php:7.4.27-fpm-alpine

docker container run --rm -d --name nginx --link php-fpm:my-php-fpm -p 8080:80 nginx:1.20.2-alpine
    • --rm 容器停止时自动删除
    • -d 在后台运行容器
    • --name 为容器指定名称为php-fpm
    • -v 绑定挂载卷,将当前目录下的html文件夹绑定到容器中的/var/www/html路径
    • --link 添加到另一个容器的链接
  • -p 将容器的80端口暴露到主机的8080端口

nginx配置default.conf

location ~ \.php$ {
    root           /var/www/html;
    fastcgi_pass   my-php-fpm:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    include        fastcgi_params;
}

docker-compose.yml

$ more docker-compose.yml 
version: "3.8"

networks:
    mynet:

services:
    php-fpm:
        image: php:7.4.27-fpm-alpine
        networks:
            - mynet
        volumes:
            - type: bind
              source: ./html
              target: /var/www/html
    nginx:
        image: nginx:1.20.2-alpine
        networks:
            - mynet
        volumes:
            - type: bind
              source: ./default.conf
              target: /etc/nginx/conf.d/default.conf
        ports:
            - 80:80
评论
:broken_heart: :confounded: :flushed: :frowning: :grinning: :heart: :kissing_heart: :mask: :pensive: :rage: :relaxed: :scream: :smile: :smirk: :sob: :stuck_out_tongue_closed_eyes: :stuck_out_tongue_winking_eye: :wink: