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

Install PHP with Docker

Install PHP image

Method one, 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]
...

Here we pull the official image, tag as5.6-fpm

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

After the download is complete, we can find the REPOSITORY as php, the tag as5.6-The fpm image.

w3codebox@w3codebox:~/php-fpm docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php                 5.6-fpm 025041cd3aa5        6 days ago          456.3 MB

Nginx + PHP deployment

Nginx deployment can be viewed at:Install Nginx with Docker, some Nginx configurations can be referred to in this article.

Start PHP:

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

Command description:

  • --name myphp-fpm : Name the container myphp-fpm.

  • -v ~/nginx/www:/www : Mount the project directory www in the host to the container /www

Create ~/nginx/conf/conf.d directory:

mkdir ~/nginx/conf/conf.d

Add it to the directory ~/nginx/conf/conf.d/w3codebox-test-php.conf The file, content as follows:

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

Configuration file description:

  • php:9000: indicates php-The URL of the fpm service, we will explain it specifically below.

  • /www/: means myphp-fpm The storage path of the php files, mapped to the local ~/nginx/www directory.

Start 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:80Port Mapping, map nginx in 80 maps to the local 8083 port.

  • ~/nginx/www: is the storage directory for the local html files,/usr/share/nginx/html is the storage directory for the container's html files.

  • ~/nginx/conf/conf.d: is the storage directory for the local nginx configuration files,/etc/nginx/conf.d is the storage directory for the container's nginx configuration files.

  • --link myphp-fpm:phpto myphp-fpm to merge the network into nginxAnd modify nginx to /etc/hosts, and map the domain php to map 127.0.0.1Let nginx pass php through9000 to access php-fpm.

Next, we will be in ~/nginx/Create index.php under the www directory, as follows:

<?php
echo phpinfo();
?>

Open in browser http://127.0.0.1:8083/index.phpDisplay as follows: