• 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

  • 相关阅读:
    学习和巩固mysql的经典练习题目
    过等保费用包含哪些?大概多少钱?
    使用Java语言做几个小小练习题吧
    【微信小程序】零基础学 | 小程序语法
    Spring及Spring boot 第四章-第二节 Spring声明式事务管理 @Transaction AOP实现
    【英语:王者进阶_高级别学术阅读】K1.快速摸清段落主题
    非零基础自学Java (老师:韩顺平) 第8章 面向对象编程(中级部分) 8.11 面向对象编程 - 多态
    【零基础学Python】Day5 Python基本数据类型之List
    中国历史朝代
    Android音频子系统(十一)------耳机返听(耳返)原理实现
  • 原文地址:https://blog.csdn.net/qq_43303980/article/details/126401134