English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

تثبيت PHP باستخدام Docker

Install PHP image

Method 1, docker pull php

Search Docker Hub php images on

You can view other versions of php by Sort by, the default is the latest version php:latest

In addition, we can also use the docker search php command to view available versions:

w3codebox@w3codebox:~/php-fpm$ 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

w3codebox@w3codebox:~/php-fpm$ docker pull php:5.6-fpm

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

w3codebox@w3codebox:~/php-fpm$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php                                                         5.6-fpm                                                         025041cd3aa5                                                                 6 أيام مضت                                                                 456.3 MB

Nginx + PHP  部署

Nginx 部署可以查看:تثبيت Nginx باستخدام Docker,一些 Nginx 的配置参考这篇文章。

启动  PHP:

$ docker run --name  myphp-fpm -v ~/nginx/www:/www  -d php:5.6-fpm

命令说明:

  • --name  myphp-fpm : 将容器命名为 myphp-fpm。

  • -v ~/nginx/www:/www : 将主机中项目的目录 www 挂载到容器的 /www

创建  ~/nginx/conf/conf.d 目录:

mkdir ~/nginx/conf/conf.d

在该目录下添加 ~/nginx/conf/conf.d/w3codebox-test-php.conf 文件,内容如下:

server {
    listen       80;
    server_name  localhost;
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm index.php;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
    location ~ \.php$ {
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /www/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

配置文件说明:

  • php:9000: 表示 php-fpm 服务的 URL,下面我们会具体说明。

  • /www/: 是 myphp-fpm 中 php 文件的存储路径,映射到本地的 ~/nginx/www 目录。

启动 nginx:

docker run --name w3codebox-php-nginx -p 8083:80 -d \
    -v ~/nginx/www:/usr/share/nginx/html:ro \
    -v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro \
    --link myphp-fpm:php \
    nginx
  • -p 8083:80، التوجيه إلى nginx ، وسيتم توجيه 80 إلى الماكينة المحلية 8083.

  • ~/nginx/www، وهو مجلد التخزين للملفات html المحلية، /usr/share/nginx/html هو مجلد التخزين للملفات html داخل القمم.

  • ~/nginx/conf/conf.d، وهو مجلد التخزين للملفات المعدلة للمستخدمين المحليين، /etc/nginx/conf.d هو مجلد التخزين للملفات المعدلة للمستخدمين المحليين داخل القمم.

  • --link myphp-fpm:php، ووضع myphp-fpm شبكة nginx، وتعديل nginx في /etc/hosts، وأضف اسم النطاق php التفاصيل إلى 127.0.0.1، مما يسمح لنغكس بالوصول إلى php-fpm عبر php:9000.

الآن سنقوم بإنشاء ملف index.php في مجلد ~/nginx/www، وسيكون الكود كالتالي:

<?php
إطلاق phpinfo();
؟>

فتح في المتصفح http://127.0.0.1:8083/index.php، وسيظهر كالتالي: