2016-11-13 103 views
3

由于我住在德国,我现在可以说“早上好”。它的04:18:15我现在需要一些睡眠。但也许你可以帮助我。502 Docker,NPM,PHPFPM和Symfony上的错误网关

这些都是我与码头工人第一步,我不能通过我的浏览器上(调用http://myproject.dev:8080/)到达本地的symfony。

我得到一个502错误网关的消息在我的浏览器

这里是我有

我有三个图像。这些被放置在

/home/chucky/dockerimages/ 

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

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

这个文件夹里发现躺着一个文件:码头工人,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/" 

这可能有助于您展示i的输出执行之后

搬运工-构成向上--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 
+0

if fpm listen to the port 9000,why nginx uses 9001? – Federkun

+0

我想我以正确的方式迈出了一步。我添加到我的fpmimage/Dockerfile下面的行 RUN sed -i's/listen = \/var \/run \ /php5-fpm.sock/listen = 0.0.0.0:9000/g'/ etc/php5/fpm/pool.d/www.conf 说实话,我不是100%知道它做了什么。它是否正确,它是否用0.0.0.0:9000定义替换了php5-fpm.sock定义? –

+0

如果您的项目位于/ var/www/myproject,并且您的nginx服务器规则适用于app.php,那么您的root不应该指向'/ var/www/myproject/web'吗? – mickadoo

回答

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 

/无功/网络/ myproject的/搬运工-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. 创建的文件夹/无功/网络/ myproject的在/ var /会话
  2. CHOWN WWW的数据:WWW的数据/无功/网络/ myproject的在/ var /会话
  3. CHOWN WWW的数据:WWW的数据/无功/网络/ myproject的在/ var /缓存
  4. CHOWN WWW的数据:WWW的数据/无功/网络/ myproject的在/ var /日志
  5. 搭配chmod 775 /无功/网络/ myproject/var/sessions
  6. chmod 775/var/www/myproject/var/cache
  7. 搭配chmod 775的/ var/WWW/myproject的在/ var /日志

因为我在config.yml并没有什么一些配置发挥各地在这之后的工作,我忘了清除缓存文件夹。所以,当我清除我的/ var/www/myproject/app/cache文件夹时,它工作,我吓坏了。

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