使用docker和nginx部署php网站

disho6za  于 2022-10-30  发布在  PHP
关注(0)|答案(1)|浏览(174)

我试图让一个网站的工作,使用php。首先,我试图让它的工作与一个基本的indexiderphp文件。
我没有把端口放在nginx部分,因为traefik auto通过它的反向代理功能来处理这个问题。我有其他网站(博客)运行它,只是有一个带有php的nginx网站,似乎给我带来了问题。

version: '3'

services:

    example-website:
        container_name: example-website
        build: ./Dockerfile
        image: 'nginx:alpine'
        restart: unless-stopped
        links:
          - php
        networks:
          - proxy
          - default
        labels:
          - "traefik.enable=true"
          - "traefik.docker.network=proxy"
          - "traefik.http.routers.example-secure.entrypoints=websecure"
          - "traefik.http.routers.example-secure.rule=Host(`example.com`)"
        volumes:
          - /var/docker/test/html:/var/www/html

    php:
        image: php:7.1.11-fpm-alpine
        expose:
        - 9000
        volumes:
        - /var/docker/test/html:/var/www/html
        networks:
          - proxy
          - default

networks:
  proxy:
    external: true

以下是我的默认.conf文件:

server {

     listen 80;
     root /var/www/html;
     index index.html index.php;

     charset utf-8;

     location / {
      try_files $uri $uri/ /index.php?$query_string;
     }

     location = /favicon.ico { access_log off; log_not_found off; }
     location = /robots.txt { access_log off; log_not_found off; }

     access_log off;
     error_log /var/log/nginx/error.log error;

     sendfile off;
 client_max_body_size 100m;

     location ~ .php$ {
      fastcgi_split_path_info ^(.+.php)(/.+)$;
      fastcgi_pass php:9000;
      fastcgi_index index.php;
      include fastcgi_params;
      fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
      fastcgi_intercept_errors off;
      fastcgi_buffer_size 16k;
      fastcgi_buffers 4 16k;
    }

     location ~ /.ht {
      deny all;
     }
    }

下面是index.php

<!DOCTYPE html>  
     <head>  
      <title>Hello World!</title>
     </head>  

     <body>  
      <h1>Hello World!</h1>
      <p><?php echo 'We are running PHP, version: ' . phpversion(); ?></p>
     </body>

停靠文件为:

FROM nginx:latest
COPY ./conf.d/default.conf /etc/nginx/conf.d/default.conf

目前部署时,我只看到:

Welcome to nginx!

If you see this page, the nginx web server is successfully installed and working. Further configuration is required.

For online documentation and support please refer to nginx.org.
Commercial support is available at nginx.com.

Thank you for using nginx.
svdrlsy4

svdrlsy41#

我建议做以下几点:

  • 检查容器日志中是否有任何错误
  • 登录到container以验证所有内容都按您期望的方式设置,例如,确保/etc/nginx/conf.d/default.conf已被您的设置覆盖
  • 检查每个容器中的nginx和php日志
  • 您在Docker-compose文件中不需要image: 'nginx:alpine',因为您在Dockerfile中有FROM nginx:latest
  • 包括显式的Docker-Composer的nginx部分中的端口部分,
  • nginx.conf中,我总是在位置块中使用/location ~ \.php$ {

相关问题