通过Docker Compose 部署LNMP架构

在这里插入图片描述

Docker compose 配置

docker compose 配置文件 docker-lnmp.yml

version: '3'
services:
  db:
    image: mysql:8
    volumes:
      - db_data:/var/lib/mysql
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: root
      MYSQL_DATABASE: test
      MYSQL_USER: test
      MYSQL_PASSWORD: test

  nginx:
    image: nginx:latest
    ports:
      - "80:80"
    volumes:
      - ./app:/usr/share/nginx/html
      - ./nginx.conf:/etc/nginx/nginx.conf
    restart: always

  php:
    image: php:7.4-fpm
    volumes:
      - ./app:/var/www/html
    restart: always

volumes:
  db_data:

这个 docker-lnmp.yml文件中,定义了三个服务:db(MySQL)、nginx和php(PHP-FPM)。定义了一个卷db_data,用于存储MySQL的数据。

其中 ./app是指docker-compose.yml这个文件目录下的app文件夹,这是你Web应用程序目录,把你的php文件放这。

Nginx 配置

还要创建一个Nginx配置文件nginx.conf,配置Nginx代理请求到PHP-FPM服务:

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    upstream php {
        server php:9000;
    }

    server {
        listen       80;
        server_name  localhost;

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

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

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        location ~ \.php$ {
            include       fastcgi_params;
            fastcgi_pass  php;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
}

在你的docker-lnmp.yml文件目录执行这个命令启动服务,会自动解析yml文件,拉取镜像并启动容器,一步完成。

docker-compose up -d

访问http://serverIP:80测试。

在这里插入图片描述

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值