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

安装 PHP Docker

Installer l'image PHP

Méthode un、docker pull php

Recherche Docker Hub 镜像上的 php:

Il est possible de voir d'autres versions de php via le filtre 'Trier par',par défaut c'est la dernière version php:latest

En outre,nous peut également utiliser la commande docker search php pour visualiser les versions disponibles:

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]
...

Ici, nous tirons l'image officielle, tag5.6-fpm

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

Après le téléchargement, nous pouvons trouver dans la liste des images locales avec REPOSITORY php, tag5.6-l'image fpm.

w3codebox@w3codebox:~/php-fpm$ docker images
REPOSITORY  TAG  IMAGE ID  CRÉÉ  TAILLE
php                 5.6-fpm  025041cd3aa5        6 jours ago          456.3 MB

Nginx + Déploiement PHP

Déploiement Nginx peut être consulté :安装 Nginx Docker,des configurations Nginx peuvent être consultées dans cet article.

Démarrer  PHP :

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

Explication des commandes :

  • --nom  myphp-fpm : Nommer le conteneur myphp-fpm。

  • -v ~/nginx/www:/www : Mounter le répertoire du projet www sur le conteneur de l'hôte /www

Créer  ~/nginx/conf/dossier conf.d :

mkdir ~/nginx/conf/conf.d

Ajouter dans ce répertoire : ~/nginx/conf/conf.d/w3codebox-test-php.conf Le fichier, contenu suivant :

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

Explication du fichier de configuration :

  • php:9000: signifie php-L'URL du service fpm, nous en parlerons en détail plus tard.

  • /www/: c'est myphp-fpm chemin de stockage des fichiers php en chinois simplifié, mappé sur le ~ local./nginx/dossier www.

Démarrer nginx :

docker run --nom3codebox-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:是本地 nginx 配置文件的存储目录,/etc/nginx/conf.d 是容器内 nginx 配置文件的存储目录。

  • --link myphp-fpm:php:把 myphp-fpm 的网络并入 nginx,并通过修改 nginx 的 /etc/hosts,把域名 php 映射成 127.0.0.1,让 nginx 通过 php:9000 访问 php-fpm。

接下来我们在 ~/nginx/在 www 目录下创建 index.php,代码如下:

<?php
echo phpinfo();
?>

浏览器打开 http://127.0.0.1:8083/index.php,显示如下: