Docker,NPM,PHPFPM和Symfony上的502 Bad Gateway

时间:2016-11-13 03:42:38

标签: php symfony nginx docker docker-compose

由于我住在德国,我可以说"早上好"现在。它04:18:15我现在需要睡一觉。但也许你可以帮我解决这个问题。

这是我使用docker的第一步,我无法通过我的webbrowser(调用http://myproject.dev:8080/)到达本地symfony。

我在浏览器中收到502 Bad Gateway消息

这是我的

我有三张照片。这些都放在

 /home/chucky/dockerimages/

- nginx/
   - Dockerfile
   - myproject.nginx.conf
- fpmimage/
   - Dockerfile
   - symfony.pool.conf
- symfony/
   - Dockerfile

我的Symfony安装(从symfony安装程序获取的默认symfony)可以在/ var / www / symfony下找到

在这个文件夹里面有一个文件:docker-compose.yml

现在我们来看文件内容:

的nginx / Dockerfile

FROM debian:jessie
RUN apt-get update && apt-get install -y nginx
ADD myproject.nginx.conf /etc/nginx/sites-available/myproject
RUN ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/myproject
RUN rm /etc/nginx/sites-enabled/default
RUN echo "upstream php-upstream { server phpfpm:9000; }" > /etc/nginx/conf.d/upstream.conf
RUN usermod -u 1000 www-data
CMD ["nginx", "-g", "daemon off;"]
RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log
EXPOSE 80
EXPOSE 443

的nginx / myproject.nginx.conf

server {
    server_name myproject.dev www.myproject.dev;
    root /var/www/myproject;

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;


    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }
    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(app_dev|config)\.php(/|$) {
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_pass phpfpm:9000;
        #fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        #fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }

    # PROD
    location ~ ^/app\.php(/|$) {
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_pass phpfpm:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    include fastcgi_params;
       # When you are using symlinks to link the document root to the
       # current version of your application, you should pass the real
       # application path instead of the path to the symlink to PHP
       # FPM.
       # Otherwise, PHP's OPcache may not properly detect changes to
       # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
       # for more information).
       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       fastcgi_param DOCUMENT_ROOT $realpath_root;
       # Prevents URIs that include the front controller. This will 404:
       # http://domain.tld/app.php/some-path
       # Remove the internal directive to allow URIs like this
       internal;
   }

   # return 404 for all other php files not matching the front controller
   # this prevents access to other php files you don't want to be accessible.
   location ~ \.php$ {
     return 404;
   }

}

fpmimage / Dockerfile

FROM debian:jessie
RUN apt-get update && apt-get install -y php5-common php5-cli php5-fpm php5-mcrypt php5-mysql php5-apcu php5-gd php5-imagick php5-curl php5-intl
RUN usermod -u 1000 www-data
CMD ["php5-fpm", "-F"]
EXPOSE 9000

fpmimage / symfony.pool.conf

listen = 127.0.0.1:9000

的symfony / Dockerfile

FROM debian:jessie
VOLUME /var/www/myproject

搬运工-compose.yml

version: '2'

services:
   symfony:
      build: /home/chucky/dockerimages/symfony
      tty: true

   phpfpm:
      build: /home/chucky/dockerimages/fpmimage
      tty: true
      volumes_from:
         - symfony
      ports:
         - "9000:9000"
      depends_on:
         - symfony

   nginx:
      build: /home/chucky/dockerimages/nginx
      volumes_from:
         - symfony
      volumes:
         - /var/log/nginx:/var/log/nginx
      ports:
         - "8080:80"
      depends_on:
         - phpfpm
         - symfony

当我访问http://127.0.0.1:8080/http://myproject.dev:8080/时 我在本地机器上的/var/log/nginx/project_error.log中获取了新的日志条目

2016/11/13 10:08:43 [error] 6#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: myproject.dev, request: "GET / HTTP/1.1", upstream: "fastcgi://172.18.0.3:9000", host: "127.0.0.1:8080"

2016/11/13 10:08:43 [error] 6#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 172.18.0.1, server: myproject.dev, request: "GET /favicon.ico HTTP/1.1", upstream: "fastcgi://172.18.0.3:9000", host: "127.0.0.1:8080", referrer: "http://127.0.0.1:8080/"

执行

后显示输出可能会有所帮助

docker-compose up --build

Building symfony
Step 1 : FROM debian:jessie
 ---> 73e72bf822ca
Step 2 : VOLUME /var/www/myproject
 ---> Using cache
 ---> 0f508ee968e9
Successfully built 0f508ee968e9
Building phpfpm
Step 1 : FROM debian:jessie
 ---> 73e72bf822ca
Step 2 : RUN apt-get update && apt-get install -y php5-common php5-cli php5-fpm php5-mcrypt php5-mysql php5-apcu php5-gd php5-imagick php5-curl php5-intl
 ---> Using cache
 ---> aa5990f0e852
Step 3 : RUN usermod -u 1000 www-data
 ---> Using cache
 ---> daf793938034
Step 4 : CMD php5-fpm -F
 ---> Using cache
 ---> 370c65c14d29
Step 5 : EXPOSE 9000
 ---> Using cache
 ---> 8d18bd852576
Successfully built 8d18bd852576
Building nginx
Step 1 : FROM debian:jessie
 ---> 73e72bf822ca
Step 2 : RUN apt-get update && apt-get install -y nginx
 ---> Using cache
 ---> 6efdb80d580f
Step 3 : ADD myproject.nginx.conf /etc/nginx/sites-available/myproject
 ---> Using cache
 ---> 166da8351d0f
Step 4 : RUN ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/myproject
 ---> Using cache
 ---> f9664f6d4dc7
Step 5 : RUN rm /etc/nginx/sites-enabled/default
 ---> Using cache
 ---> 18de9d72a2f5
Step 6 : RUN echo "upstream php { server phpfpm:9001; }" > /etc/nginx/conf.d/upstream.conf
 ---> Running in 657abb36b3bb
 ---> b8dfcf6f5668
Removing intermediate container 657abb36b3bb
Step 7 : RUN usermod -u 1000 www-data
 ---> Running in 55a8dce2f492
 ---> bca558fcf413
Removing intermediate container 55a8dce2f492
Step 8 : CMD nginx -g daemon off;
 ---> Running in 400b5f76a3bb
 ---> 6751644b3548
Removing intermediate container 400b5f76a3bb
Step 9 : RUN ln -sf /dev/stdout /var/log/nginx/access.log
 ---> Running in 796f023c797e
 ---> 72bc07b1330e
Removing intermediate container 796f023c797e
Step 10 : RUN ln -sf /dev/stderr /var/log/nginx/error.log
 ---> Running in 269b0fec15aa
 ---> 62d1674d9b5a
Removing intermediate container 269b0fec15aa
Step 11 : EXPOSE 80
 ---> Running in 348d5e2e6061
 ---> 5373fddc7ce6
Removing intermediate container 348d5e2e6061
Step 12 : EXPOSE 443
 ---> Running in b6bbf8623b4b
 ---> fa6b92ad1d09
Removing intermediate container b6bbf8623b4b
Successfully built fa6b92ad1d09
Starting myproject_symfony_1
Starting myproject_phpfpm_1
Recreating myproject_nginx_1
Attaching to myproject_symfony_1, myproject_phpfpm_1, myproject_nginx_1
phpfpm_1   | 2016 03:16:45] NOTICE: fpm is running, pid 1
phpfpm_1   | [13-Nov-2016 03:16:45] NOTICE: ready to handle connections
phpfpm_1   | [13-Nov-2016 03:16:45] NOTICE: systemd monitor interval set to 10000ms

1 个答案:

答案 0 :(得分:1)

同时,经过多次调试后我才开始运行。

所以这些是我的文件

fpmimage / Dockerfile

FROM debian:jessie

RUN apt-get update && apt-get install -y php5-common php5-cli php5-fpm php5-mcrypt php5-mysql php5-apcu php5-gd php5-imagick php5-curl php5-intl

RUN sed -i 's/listen = \/var\/run\/php5-fpm.sock/listen = 0.0.0.0:9000/g' /etc/php5/fpm/pool.d/www.conf

RUN usermod -u 1000 www-data

CMD ["php5-fpm", "-F"]

EXPOSE 9000

fpmimage / symfony.pool.conf

listen = 127.0.0.1:9000

的nginx / Dockerfile

FROM debian:jessie

RUN apt-get update && apt-get install -y nginx

ADD myproject.nginx.conf /etc/nginx/sites-available/myproject

RUN ln -s /etc/nginx/sites-available/myproject /etc/nginx/sites-enabled/myproject
RUN rm /etc/nginx/sites-enabled/default

RUN echo "upstream php { server phpfpm:9000; }" > /etc/nginx/conf.d/upstream.conf

RUN usermod -u 1000 www-data

CMD ["nginx", "-g", "daemon off;"]

RUN ln -sf /dev/stdout /var/log/nginx/access.log
RUN ln -sf /dev/stderr /var/log/nginx/error.log

EXPOSE 80
EXPOSE 443

的nginx / myproject.nginx.conf

server {
    server_name myproject.dev www.myproject.dev;
    root /var/www/myproject/web;

    error_log /var/log/nginx/project_error.log;
    access_log /var/log/nginx/project_access.log;


    location / {
        # try to serve file directly, fallback to app.php
        try_files $uri /app.php$is_args$args;
    }
    # DEV
    # This rule should only be placed on your development environment
    # In production, don't include this and don't deploy app_dev.php or config.php
    location ~ ^/(app_dev|config)\.php(/|$) {
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_pass phpfpm:9000;
        #fastcgi_pass php-upstream;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        # When you are using symlinks to link the document root to the
        # current version of your application, you should pass the real
        # application path instead of the path to the symlink to PHP
        # FPM.
        # Otherwise, PHP's OPcache may not properly detect changes to
        # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
        # for more information).
        #fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fastcgi_param DOCUMENT_ROOT $realpath_root;
    }
    # PROD
    location ~ ^/app\.php(/|$) {
        #fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_pass phpfpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
       # When you are using symlinks to link the document root to the
       # current version of your application, you should pass the real
       # application path instead of the path to the symlink to PHP
       # FPM.
       # Otherwise, PHP's OPcache may not properly detect changes to
       # your PHP files (see https://github.com/zendtech/ZendOptimizerPlus/issues/126
       # for more information).
       fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
       fastcgi_param DOCUMENT_ROOT $realpath_root;
       # Prevents URIs that include the front controller. This will 404:
       # http://domain.tld/app.php/some-path
       # Remove the internal directive to allow URIs like this
       internal;
   }

   # return 404 for all other php files not matching the front controller
   # this prevents access to other php files you don't want to be accessible.
   location ~ \.php$ {
     return 404;
   }

}

的symfony / Dockerfile

FROM debian:jessie

VOLUME /var/www/myproject/app/cache
VOLUME /var/www/myproject/var/sessions

RUN chown www-data:www-data /var/www/myproject/app/cache
RUN chown www-data:www-data /var/www/myproject/var/sessions

/var/www/myproject/docker-compose.yml

version: '2'

services:
   symfony:
      build: /home/chucky/dockerimages/symfony
      tty: true
      volumes:
         - /var/www/myproject:/var/www/myproject
         - /var/www/myproject/app/cache:/var/www/myproject/app/cache
         - /var/www/myproject/var/sessions:/var/www/myproject/var/sessions

   phpfpm:
      build: /home/chucky/dockerimages/fpmimage
      tty: true
      volumes_from:
         - symfony
      ports:
         - "9000:9000"
      depends_on:
         - symfony

   nginx:
      build: /home/chucky/dockerimages/nginx
      volumes_from:
         - symfony
      volumes:
         - /var/log/nginx:/var/log/nginx
      ports:
         - "8080:80"
      depends_on:
         - phpfpm
         - symfony

我认为这都是必要的文件。但我必须做一些调整才能让我的symfony项目运行。我遇到了诸如“会话存储无法创建目录”之类的问题。所以我试图将“framework.session.save_path”的路径修改为/app/config.yml中的其他内容。但解决方案比这更简单。我必须注意为framework.session.save_path定义的文件夹存在。

在symfony默认情况下,这是“%kernel.root_dir%/ .. / var / sessions /%kernel.environment%”。所以我为这个文件夹做了一些chmod和chown。 (还有缓存和日志文件夹)

  1. 创建文件夹/ var / www / myproject / var / sessions
  2. chown www-data:www-data / var / www / myproject / var / sessions
  3. chown www-data:www-data / var / www / myproject / var / cache
  4. chown www-data:www-data / var / www / myproject / var / logs
  5. chmod 775 / var / www / myproject / var / sessions
  6. chmod 775 / var / www / myproject / var / cache
  7. chmod 775 / var / www / myproject / var / logs
  8. 由于我在config.yml中使用了一些配置并且在此之后没有任何工作,我忘了清除缓存文件夹。所以在我清除了我的/ var / www / myproject / app / cache文件夹后,它工作了,我吓坏了。

    我希望这有所帮助,我没有忘记我采取的步骤,以使我的系统运行。我想知道您的配置中是否有任何改进。我希望还有很多其他方法可以让这样的系统运行起来。