• docker-compose部署nginx+php


    1、首先拉取nginx及php镜像
    docker pull nginx:1.21.6
    docker pull php:7.4.28-fpm
    2、创建本地目录
    mkdir /home/nginx-php
    3、运行容器拷贝文件
    运行nginx拷贝
    docker run -d --name nginx nginx:1.21.6
    cd /home/nginx-php
    docker cp nginx:/etc/nginx .
    docker cp nginx:/var/log .
    docker rm -f nginx
    运行php拷贝
    docker run -d --name php php:7.4.28-fpm
    cd /home/nginx-php
    docker cp phptest:/usr/local/etc/php .
    docker rm -f php
    4、修改php配置
    cd /home/nginx-php/php #进入php目录
    mv php.ini-development php.ini #重命名php.ini-development为php.ini
    sed -i “s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/g” php.ini
    5、修改nginx配置文件
    cd /home/nginx-php/nginx/conf.
    cp default.conf www.nwd.com.conf (这边随便定义一个网站名称,到时候在windown服务器添加一下hosts)
    vi www.nwd.com.conf

    server {
        listen       80;
        server_name www.nwd.com;   #修改为自定义的域名
    
        #access_log  /var/log/nginx/host.access.log  main;
        location / {
            root /home/www;    网站根目录
            index  index.html index.htm index.php;  添加index.php
        }
    
        #error_page  404              /404.html;
    
        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   /usr/share/nginx/html;
        }
    
        ###添加如下的代码
        location ~ \.php$ {
            root    /home/www;    ##网站根目录
            fastcgi_buffer_size       128k;
            fastcgi_buffers           4 256k;
            fastcgi_busy_buffers_size 256k;
            fastcgi_pass   php:9000;    ##写你docker-compose定义的名字加端口,不要写127.0.0.1
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    
        # deny access to .htaccess files, if Apache's document root
        # concurs with nginx's one
        #
        #location ~ /\.ht {
        #    deny  all;
        #}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    6、创建网站目录
    mkdir -p /home/nginx-php/www
    echo ‘’ > index.html
    echo ‘’ > index.php
    7、创建docker-compose文件
    vi /home/nginx-php/docker-compose.yaml

    version: "3"
    services: 
      nginx:
        image: nginx:1.21.6
        container_name: "web-nginx"
        restart: always
        ports:
          - "80:80"
          - "443:443"
        depends_on:
          - "php"
        volumes:
          - "/home/nginx-php/nginx:/etc/nginx"
          - "/home/nginx-php/log:/var/log"
          - "/home/nginx-php/www:/home/www"
        networks:
          - web-network
      php:
        image: php:7.4.28-fpm
        container_name: "web-php"
        restart: always
        ports:
          - "9000:9000"
        volumes:
          - "/home/nginx-php/www:/home/www"
          - "/home/nginx-php/php:/usr/local/etc/php"
        networks:
          - web-network
    networks:
      web-network:
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    8、启动docker-compose
    docker-compose up -d
    9、使用自定义域名需要修改windown的hosts
    C:\Windows\System32\drivers\etc\hosts
    在这里插入图片描述
    10、访问www.nwd.com/index.php
    在这里插入图片描述
    访问不到页面使用命令查看日志报错:docker logs 容器id

  • 相关阅读:
    electron实战之Electron+Vue+Vite+ElementPlus操作本地配置文件
    LCD屏硬件调光的几种方式
    把Mybatis Generator生成的代码加上想要的注释
    java游戏制作-拼图游戏
    自然语言处理的多行业应用
    leetcode 004. 寻找两个正序数组的中位数 (c++超详细解法)
    数据结构 - 数组 - 青岛大学(王卓)
    List<Map<String, String>>数据行转列
    三数之和(python)
    记一次服务器异常掉电,导致HBase Master is initializing 问题处理
  • 原文地址:https://blog.csdn.net/qq_43303980/article/details/126401134