docker compose 搭建NGINX和PHP

直接上docker-compose.yml

version: '3.3'
services:
  nginx:
    build:
      context: ./nginx
      dockerfile: Dockerfile
    volumes:
        - ./nginx/nginx.conf:/etc/nginx/nginx.conf
        - ./nginx/conf.d:/etc/nginx/conf.d
        - ./nginx/html:/usr/share/nginx/html
        - ./nginx/log:/var/log/nginx
    restart: on-failure
    depends_on:
      - php-fpm
    ports:
      - '80:80'
    links:
      - php-fpm

  php-fpm:
    build:
      context: ./php
      dockerfile: Dockerfile
    volumes:
      - ./nginx/html:/var/www/html
    restart: on-failure
    ports:
      - '9000:9000'

上面是基礎版,但是已經可以運行了,剩下的就靠自己了。

注意點
上面若有參數不明白,建議去官網學習一下,地址

把NGINX的配置文件、日誌、項目運行地址,指向本地目錄,
建議配合Git管理使用,很爽。

配合PHP的時候,需要注意的是,容器互聯
links 連接其他容器名,

在nginx.conf的server文件裏,需要注意:

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

上面兩處注意點,
1,fastcgi_pass php-fpm:9000 ,此處直接寫容器名,不建議寫ip,
每次容器重啓,ip可能會變,但容器名不會變。
2,fastcgi_param *** /var/www/html 此處這個地址,是PHP容器中的默認訪問地址!一般是這個地址。。不確定的話可以去PHP容器中查看配置項就知道了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章