我正在尝试将docker用于php5.6和ngnix,但配置中存在问题 [英] I am trying to use docker for php5.6 with ngnix but there is a issue in configuration

查看:191
本文介绍了我正在尝试将docker用于php5.6和ngnix,但配置中存在问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要在本地计算机上设置php5.6。以下是docker-compose.yml文件

 版本:'3'

网络:
laravel:

服务:
nginx:
映像:nginx:稳定高山
container_name:nginx
端口:
- 8000:80
卷:
-./src:/var/www
-./nginx/default.conf:/etc/nginx/conf.d/default.conf
depend_on:
-php
网络:
-laravel


php:
图片:gotechnies / php-5.6-alpine
container_name:php
卷:
-./src:/var/www
端口:
- 9000:9000
网络:
-laravel

ngnix配置文件

 服务器{
听80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root / var / www / public;

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

位置〜\.php $ {
try_files $ uri = 404;
fastcgi_split_path_info ^(。+ \.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
包括fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
fastcgi_param PATH_INFO $ fastcgi_path_info;
}
}

在运行docker-compose up -d命令后,以下是





但是当我尝试访问



对于 nginx / default.conf ,请使用以下命令:

 服务器{
听80;
index index.php index.html;
server_name localhost;
error_log /var/log/nginx/error.log;
access_log /var/log/nginx/access.log;
root / var / www / html;

位置〜\.php $ {
try_files $ uri = 404;
fastcgi_split_path_info ^(。+ \.php)(/.+)$;
fastcgi_pass php:9000;
fastcgi_index index.php;
包括fastcgi_params;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
fastcgi_param PATH_INFO $ fastcgi_path_info;
}
}

对于 src / index.php (测试以确保PHP正常运行)

 < ;?回声phpinfo(); ?> 

对于您的 docker-compose.yml ,我删除了很多内容您将不需要:

 版本: 3 

服务:
nginx :
图片:nginx:最新
端口:
- 8080:80
数量:
-./src/:/var/www/html
-./nginx/default.conf:/etc/nginx/conf.d/default.conf
depend_on:
-php

php:
图片:mikolatero / php5.6-fpm-alpine
卷:
-./src/:/var/www/html

执行 docker-compose up
导航到 http:// localhost:8080 / index.php ,您应该受到欢迎使用PHP信息页面:



< img src = https://i.stack.imgur.com/JTjA5.png alt =在此处输入图片描述>



发生了什么变化?



在这种情况下,我选择了最新的NGINX并找到了适用于PHP5.6-FPM的良好映像并将其用于堆栈。



对于已装入的卷,我将目录移到了与Docker Compose文件相同的上下文中。没必要,但从笔记本电脑运行时可能更便于携带。您安装的Web源可能是/应该是Web存储库的位置。我还在NGINX映像 / var / www / html

中使用了众所周知的Web文件位置

PHP5.6-FPM与Web资源安装在同一目录中,因此PHP可用于该目录中的文件。



最后,我摆脱了 networks ,因为除非您有特殊原因,否则没有必要,因为这些映像将使用默认的Docker网络。


Hello I need to setup php5.6 on my local machine. Following are the docker-compose.yml file

  version: '3'

networks:
  laravel:

services:
  nginx:
    image: nginx:stable-alpine
    container_name: nginx
    ports:
      - "8000:80"
    volumes:
      - ./src:/var/www
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php
    networks:
      - laravel


  php:
    image: gotechnies/php-5.6-alpine
    container_name: php
    volumes:
      - ./src:/var/www
    ports:
      - "9000:9000"
    networks:
      - laravel

ngnix configuration file

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;

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

    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;
    }
}

after running docker-compose up -d command following is the output.

but when i am trying to access http://localhost:8000 i am unable to render page.

解决方案

To run PHP5.6 with NGINX you will need to do the following:

Directory layout. All web files go in your local src/ directory

For nginx/default.conf use the following:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html;

    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;
    }
}

For src/index.php (test to make sure PHP is working)

<? echo phpinfo(); ?>

For your docker-compose.yml I have removed a lot of things that you will not need:

version: "3"

services:
  nginx:
    image: nginx:latest
    ports:
        - "8080:80"
    volumes:
      - ./src/:/var/www/html
      - ./nginx/default.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - php

  php:
    image: mikolatero/php5.6-fpm-alpine
    volumes: 
      - ./src/:/var/www/html

Execute docker-compose up. Navigate to http://localhost:8080/index.php and you should be greeted with the PHP info page:

What Changed?

In this case, I opted for the latest NGINX and located a good image for PHP5.6-FPM and used those for the stack.

For the mounted volumes, I moved the directories into the same context as the Docker Compose file. Not necessary, but maybe more portable when running from a laptop. Your mounted web source may/should be the location of your web repo. I also used the well-know location for the web files in the NGINX image /var/www/html

The PHP5.6-FPM is mounted to the same directory as the web source so PHP is available to the files in that directory.

Lastly, I got rid of the networks as, unless you have a specific reason, it is not necessary as these images will use the default Docker network.

这篇关于我正在尝试将docker用于php5.6和ngnix,但配置中存在问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆