使用PHP和NGINX不起作用的Docker-Compose

发布于 2025-02-12 05:38:38 字数 1838 浏览 3 评论 0 原文

我在Docker-Compose中具有非常简单的配置,其中 php:7-fpm nginx 我想用来托管简单的PHP网站。但是我遇到的问题在生产中可用。

有人可以告诉我我做错了什么吗?

这是docker-compose.prod.yml:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ../company/site:/code

      - ./site.prod.conf:/etc/nginx/conf.d/default.conf

  php:
    image: php:7-fpm
    volumes:
      - ../company/site:/code

这是site.prod.conf文件:

server {
    listen 80;
    index index.php index.html;
    server_name example.com;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    location ~ \.php$ {
            try_files $uri =404;
            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_param PATH_INFO $fastcgi_path_info;
        }
}

我可以编写,日志似乎很好,当我运行Docker PS:

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
c268a9cf4716   php:7-fpm      "docker-php-entrypoi…"   27 minutes ago   Up 16 seconds   9000/tcp                                example_code-php-1
beaaec39209b   nginx:latest   "/docker-entrypoint.…"   27 minutes ago   Up 16 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   example_code-web-1

然后检查端口时,我认为这看起来很好:

netstat -tulpn | grep :80
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      204195/docker-proxy 
tcp6       0      0 :::8080                 :::*                    LISTEN      204207/docker-proxy 

I have a very simple config in docker-compose with php:7-fpm and nginx that I want to use to host simple php websites. But I am having issues getting it available in production.

Can someone please tell me what I did wrong?

Here is docker-compose.prod.yml:

version: '3.8'
services:
  web:
    image: nginx:latest
    ports:
      - "8080:80"
    volumes:
      - ../company/site:/code

      - ./site.prod.conf:/etc/nginx/conf.d/default.conf

  php:
    image: php:7-fpm
    volumes:
      - ../company/site:/code

Here is the site.prod.conf file:

server {
    listen 80;
    index index.php index.html;
    server_name example.com;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    location ~ \.php$ {
            try_files $uri =404;
            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_param PATH_INFO $fastcgi_path_info;
        }
}

I can compose up and the logs appear to be fine and when I run docker ps:

docker ps
CONTAINER ID   IMAGE          COMMAND                  CREATED          STATUS          PORTS                                   NAMES
c268a9cf4716   php:7-fpm      "docker-php-entrypoi…"   27 minutes ago   Up 16 seconds   9000/tcp                                example_code-php-1
beaaec39209b   nginx:latest   "/docker-entrypoint.…"   27 minutes ago   Up 16 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   example_code-web-1

Then checking the ports, I think this looks fine:

netstat -tulpn | grep :80
tcp        0      0 0.0.0.0:8080            0.0.0.0:*               LISTEN      204195/docker-proxy 
tcp6       0      0 :::8080                 :::*                    LISTEN      204207/docker-proxy 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事↓在人 2025-02-19 05:38:38
  1. 您需要将PHP容器的TCP端口9000公开,以使其他容器能够使用它(请参阅 Docker-Compose端口与公开):
  php:
    image: php:7-fpm
    expose:
      - "9000"
    ...
  1. 您真的想要您的网站在TCP端口8080上可用,而不是标准端口80?如果不是,请将“ 8080:80”更改为 “ 80:80”
  2. 除了PHP处理程序外,请使用默认位置(尽管您的网站也应该是可行的,但不将其添加到您的nginx配置是一个不好的做法):
location / {}
  1. You need to expose TCP port 9000 of the PHP container to made other containers able to use it (see What is the difference between docker-compose ports vs expose):
  php:
    image: php:7-fpm
    expose:
      - "9000"
    ...
  1. Do you really want your sites to be available on TCP port 8080, not the standard port 80? If not, change "8080:80" to "80:80".
  2. Besides the PHP handler, use a default location (although your site should be workable even without it, it is a bad practice to not add it to your nginx config):
location / {}
离线来电— 2025-02-19 05:38:38

您必须检查日志以找出错误。 https://docs.docker.com/engine/engine/eengine/reference/commandline/logs/logs/logs/ < / a>

这些问题可能发生:

  1. PHP模块缺少
  2. 用户 /权限不正确。 WWW-DATA是否在您的NGINX和PHP-FPM配置中定义?
  3. 使用HTTPS和端口443代替HTTP和端口80。HTTP可以被您的浏览器阻止。您可以使用Let's Encrypt Docker Image定义免费的SSL证书。
  4. 自2019年1月10日以来,PHP 7.0是EOL(结束或生命)。请使用PHP 8.0或PHP 8.1。 https://endoflife.date/php
  5. 请勿使用tag nginx:最新生产。更新容器时,您可能会遇到严重的问题,因为将下载最后一个版本。
  6. 不要在生产上登录目录。请在您的Dockerfile中使用副本。
  7. 在这里检查服务器上的防火墙

是Docker Docker最佳实践: https:// docs。 docker.com/develop/dev-best-practices/

https://docks.docker.com/devevelovely.com/devevelovelvelvement-develop-images-images-images-images/dockerfile_best-practerices /

这里,我建议此docker-compose.prod.yml

version: '3.8'
services:
  web:
    image: nginx:1.21
    depends_on:
      - my-php-container-name
    container_name: my-nginx-container-name
    working_dir: /code
    ports:
        - '80:80'
        - '443:443'
    volumes:
      - ../nurock/hidden_creste:/code
      - ./site.prod.conf:/etc/nginx/conf.d/default.conf
    restart: always

  php:
    build: php-fpm
    container_name: my-php-container-name
    working_dir: /code
    volumes:
      - ../nurock/hidden_creste:/code
    restart: always

在 与此Docker-compose.prod.yml文件相同的目录,创建一个php-fpm目录: mkdir php-fpm (或Docker-Compose中 build> build> build> build> buold>的目录架构。 prod.yml文件。)

在php-fpm目录中,请添加此dockerfile,称为 dockerfile

FROM php:8.1-fpm
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
WORKDIR "/code"
RUN apt-get update && apt-get install -y --no-install-recommends \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libicu-dev
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd pdo_mysql bcmath mysqli intl

当然,添加php扩展名您需要您的项目。在这里,您有一个示例如何安装GD,PDO_MYSQL,BCMatch,MySQLI,INTL。但是还有其他扩展名称为curl,xml,xdebug,mcrypt,memcache等...

in您的nginx配置,应使用端口443定义https的配置。还请更新此行 fastcgi_pass php:9000; 。用容器名称替换 php 。当然,容器名称必须是唯一的。

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass my-php-container-name:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }

然后,建立您的设置

docker-compose -f docker-compose.prod.yml build && docker-compose -f docker-compose.prod.yml up

You must check the logs to find out the error. https://docs.docker.com/engine/reference/commandline/logs/

These issues can happen :

  1. A php module is missing
  2. user / permission are not correct. Is www-data defined in your nginx and php-fpm config ?
  3. Use HTTPS and port 443 instead of HTTP and port 80. HTTP may be blocked by your browser. You can define a free SSL certificate with Let's Encrypt Docker image.
  4. PHP 7.0 is EOL (end or life) since January 10, 2019. Please use PHP 8.0 or PHP 8.1. https://endoflife.date/php
  5. Do not use use tag nginx:latest on production. You may have serious issues when you update your container, because last version will be downloaded.
  6. Do not mount directory on production. Please use COPY in your Dockerfile.
  7. Check the firewall on your server

Here is Docker Docker best practices : https://docs.docker.com/develop/dev-best-practices/

https://docs.docker.com/develop/develop-images/dockerfile_best-practices/

Here, I suggest this docker-compose.prod.yml

version: '3.8'
services:
  web:
    image: nginx:1.21
    depends_on:
      - my-php-container-name
    container_name: my-nginx-container-name
    working_dir: /code
    ports:
        - '80:80'
        - '443:443'
    volumes:
      - ../nurock/hidden_creste:/code
      - ./site.prod.conf:/etc/nginx/conf.d/default.conf
    restart: always

  php:
    build: php-fpm
    container_name: my-php-container-name
    working_dir: /code
    volumes:
      - ../nurock/hidden_creste:/code
    restart: always

In the same directory as this docker-compose.prod.yml file, create a php-fpm directory: mkdir php-fpm (or directory architecture written under build in docker-compose.prod.yml file.)

In php-fpm directory, please add this Dockerfile called Dockerfile

FROM php:8.1-fpm
COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer
WORKDIR "/code"
RUN apt-get update && apt-get install -y --no-install-recommends \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libicu-dev
    && docker-php-ext-configure gd --with-freetype --with-jpeg \
    && docker-php-ext-install -j$(nproc) gd pdo_mysql bcmath mysqli intl

Of course, add the PHP extensions that you need for your project. Here you have an example how to install gd, pdo_mysql, bcmatch, mysqli, intl. But there are others extension as curl, xml, xdebug, mcrypt, memcache, etc... https://github.com/mlocati/docker-php-extension-installer

In your nginx configuration, you should define config for HTTPS with port 443. Please also update this line fastcgi_pass php:9000;. Replace php by the container name. Of course, container name must be unique.

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass my-php-container-name:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $fastcgi_path_info;
        }

Then, build your set-up

docker-compose -f docker-compose.prod.yml build && docker-compose -f docker-compose.prod.yml up
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文