使用Docker Compose安装Nginx和PHP7环境

DevOps 2020年06月05日

本篇笔记记录了使用Docker Compose安装Nginx和PHP7环境的过程

下载并安装docker-compose

sudo curl -L "https://github.com/docker/compose/releases/download/1.26.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

设置权限

sudo chmod +x /usr/local/bin/docker-compose

查看版本

docker-compose --version

创建工作目录

mkdir -p /docker/compose-nginx-fpm
cd /docker/compose-nginx-fpm

创建Nginx配置目录

mkdir -p nginx/conf.d

创建配置

vim nginx/conf.d/default.conf

配置如下

server {
    listen  80 default_server;
    server_name  localhost;

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

    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

创建应用根目录

mkdir -p wwwroot

创建php测试文件

vim wwwroot/index.php

代码如下

<?php
phpinfo();

创建docker-compose配置文件

vim docker-compose.yml

配置如下

version: "3"
services:
  nginx:
    image: nginx:1.19.0
    ports:
      - "80:80"
      - "443:443"
    volumes:
      - ./wwwroot:/usr/share/nginx/html:rw
      - ./nginx/conf.d:/etc/nginx/conf.d/:ro
    networks:
      - net-app

  php:
    image: php:7.2.29-fpm
    ports:
      - "9000:9000"
    volumes:
      - ./wwwroot:/usr/share/nginx/html:rw
    networks:
      - net-app

networks:
  net-app:

运行docker-compose

docker-compose up -d


浏览器访问测试