1. 程式人生 > 其它 >Docker Compose安裝Nginx和PHP7環境

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


瀏覽器訪問測試