Docker 安装 PHP 并配合 Nginx 运行 phpinfo

搜索安装 PHP 镜像

docker pull php

查找Docker Hub上的php镜像

$ docker search php
NAME                      DESCRIPTION                                     STARS     OFFICIAL   AUTOMATED
php                       While designed for web development, the PH...   1232      [OK]       
richarvey/nginx-php-fpm   Container running Nginx + PHP-FPM capable ...   207                  [OK]
phpmyadmin/phpmyadmin     A web interface for MySQL and MariaDB.          123                  [OK]
eboraas/apache-php        PHP5 on Apache (with SSL support), built o...   69                   [OK]
php-zendserver            Zend Server - the integrated PHP applicati...   69        [OK]       
million12/nginx-php       Nginx + PHP-FPM 5.5, 5.6, 7.0 (NG), CentOS...   67                   [OK]
webdevops/php-nginx       Nginx with PHP-FPM                              39                   [OK]
webdevops/php-apache      Apache with PHP-FPM (based on webdevops/php)    14                   [OK]
phpunit/phpunit           PHPUnit is a programmer-oriented testing f...   14                   [OK]
tetraweb/php              PHP 5.3, 5.4, 5.5, 5.6, 7.0 for CI and run...   12                   [OK]
webdevops/php             PHP (FPM and CLI) service container             10                   [OK]
...

这里我们拉取官方的镜像,标签为5.6-fpm

$ docker pull bitnami/php-fpm

等待下载完成后,我们就可以在本地镜像列表里查到REPOSITORY为php,标签为5.6-fpm的镜像。

$ docker images
REPOSITORY                 TAG                 IMAGE ID            CREATED             SIZE
 bitnami/php-fpm            latest              84abb84939ff        About an hour ago   246MB
 php                        latest              f0357c41bff5        10 days ago         367MB
 nginx                      latest              719cd2e3ed04        2 weeks ago         109MB
 docker4w/nsenter-dockerd   latest              2f1c802f322f        8 months ago        187kB

Nginx + PHP 部署

Nginx 部署可以查看:Docker 安装 Nginx,一些 Nginx 的配置参考这篇文章。

启动PHP容器

$ docker run -d --name jphp-fpm -v D:\wwwroot\POWER_IDC:/data/wwwroot/poweridc  bitnami/php-fpm

命令说明:

  • –name jphp-fpm : 将容器命名为 jphp-fpm。
  • -v D:\wwwroot\POWER_IDC:/data/wwwroot/poweridc 将主机中项目的目录 POWER_IDC 挂载到容器的data/wwwroot/poweridc

查看php容器IP

docker inspect --format '{{ .NetworkSettings.IPAddress }}' jphp-fpm

配置Nginx的虚拟主机文件

在该目录下修改 D:\DockerConfig\nginx\conf.d\poweridc.conf 文件,内容如下:

server {
    listen       80;
    server_name  poweridc;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /data/wwwroot/poweridc;
        index  index.html index.htm;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        #root           html;
        fastcgi_pass   jphp-fpm:9000;#下面用了--link参数,hosts里会自动解析ip
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /data/wwwroot/poweridc/$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

配置文件说明:

  • 172.17.0.3:9000: 表示 php-fpm 服务的容器地址,上面已经说明。

启动Nginx

docker run -d -p 8081:80 --name jnginx -v D:\wwwroot\POWER_IDC:/data/wwwroot/poweridc -v D:\DockerConfig\nginx\conf.d\poweridc.conf:/etc/nginx/conf.d/poweridc.conf -v D:\DockerConfig\nginx\logs:/etc/nginx/logs --link jphp-fpm:bitnami/php-fpm nginx
  • -p 8081:80: 端口映射,把 nginx 中的 80 映射到本地的 8081 端口。
  • D:\wwwroot\POWER_IDC: 是本地 html 文件的存储目录,/data/wwwroot/poweridc 是容器内 html 文件的存储目录。
  • D:\DockerConfig\nginx\conf.d\poweridc.conf: 是本地 nginx 配置文件的存储目录,/etc/nginx/conf.d/poweridc.conf 是容器内 nginx 配置文件的存储目录。

运行php文件

接下来我们在 D:\wwwroot\POWER_IDC 目录下创建 info.php,代码如下:

<?php phpinfo();

浏览器打开 http://poweridc:8081/info.php,显示如下:

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注