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;
    #}
}

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:

8、启动docker-compose
docker-compose up -d
9、使用自定义域名需要修改windown的hosts
C:\Windows\System32\drivers\etc\hosts
docker-compose部署nginx+php_第1张图片
10、访问www.nwd.com/index.php
docker-compose部署nginx+php_第2张图片
访问不到页面使用命令查看日志报错:docker logs 容器id

你可能感兴趣的:(随笔,php,nginx,docker)