首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >502码头、NPM、PHPFPM和Symfony的不良网关

502码头、NPM、PHPFPM和Symfony的不良网关
EN

Stack Overflow用户
提问于 2016-11-13 03:42:37
回答 1查看 2.7K关注 0票数 3

由于我住在德国,我现在可以说“早上好”了。现在是04:18:15,我现在需要休息。但也许你能帮我解决这个问题。

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

我在浏览器中得到一条502坏网关消息

这是我有的东西

我有三张照片。这些被放置在

代码语言:javascript
复制
 /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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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.info.gov.hk.conf

代码语言:javascript
复制
listen = 127.0.0.1:9000

symfony/Dockerfile

代码语言:javascript
复制
FROM debian:jessie
VOLUME /var/www/myproject

docker-compose.yml

代码语言:javascript
复制
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中获得本地机器上的新日志条目。

代码语言:javascript
复制
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/"

在我执行完之后,向您显示输出可能会有帮助。

船坞-拼装-建造

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-13 18:31:15

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

这些是我的文件

fpmimage/Dockerfile

代码语言:javascript
复制
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.info.gov.hk.conf

代码语言:javascript
复制
listen = 127.0.0.1:9000

nginx/Dockerfile

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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/会话
  2. chown www-data:www-data /var/www/myproject/var/会话
  3. chown www-data:www-data /var/www/myproject/var/cache
  4. chown www-data:www-data /var/www/myproject/var/log
  5. chmod 775 /var/www/myproject/var/session
  6. chmod 775 /var/www/myproject/var/cache
  7. chmod 775 /var/www/myproject/var/log

由于我在config.yml中使用了一些配置,在此之后什么也没有起作用,所以我忘记清除缓存文件夹。所以当我清除了我的/var/www/myproject/app/cache文件夹之后,它成功了,我吓坏了。

我希望这有帮助,我没有忘记我的一步,让我的系统运行。我想知道您是否在我的配置中看到了任何改进。我希望还有很多其他方法可以让这样的系统运行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40570341

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档