Docker部署PHP+Nginx+MySQL

目录

安装 PHP 镜像

安装 Nginx 镜像

 Nginx + PHP部署


安装 PHP 镜像

用 docker search php 命令来查看可用版本

[root@ergeth23set3asr56erty45 ~]# docker search php
NAME                       DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
php                        While designed for web development, the PHP …   5875                [OK]
phpmyadmin/phpmyadmin      A web interface for MySQL and MariaDB.          1123                                    [OK]
adminer                    Database management in a single PHP file.       543                 [OK]
phpmyadmin                 phpMyAdmin - A web interface for MySQL and M…   204                 [OK]
webdevops/php-nginx        Nginx with PHP-FPM                              193                                     [OK]
php-zendserver             Zend Server - the integrated PHP application…   191                 [OK]
webdevops/php-apache-dev   PHP with Apache for Development (eg. with xd…   141                                     [OK]
webdevops/php-apache       Apache with PHP-FPM (based on webdevops/php)    116                                     [OK]
bitnami/php-fpm            Bitnami PHP-FPM Docker Image                    112                                     [OK]
phpunit/phpunit            PHPUnit is a programmer-oriented testing fra…   82                                      [OK]
nazarpc/phpmyadmin         phpMyAdmin as Docker container, based on off…   61                                      [OK]
phpipam/phpipam-www        phpIPAM is an open-source web IP address man…   50
thecodingmachine/php       General-purpose ultra-configurable PHP images   35                                      [OK]
circleci/php               CircleCI images for PHP                         33
chialab/php                Adding common PHP extensions to some of the …   27                                      [OK]
devilbox/php-fpm           PHP-FPM Docker images based on original PHP …   22
bitnami/phpmyadmin         Bitnami Docker Image for phpMyAdmin             22                                      [OK]
bitnami/phpbb              Bitnami Docker Image for phpBB                  19                                      [OK]
phpdockerio/php7-fpm       PHP 7 FPM base container for PHPDocker.io.      18                                      [OK]
phpdockerio/php73-fpm      PHP 7.3 FPM base container for PHPDocker.io.    18
appsvc/php                 Azure App Service php dockerfiles               16                                      [OK]
phpipam/phpipam-cron       phpIPAM is an open-source web IP address man…   14
arm32v7/php                While designed for web development, the PHP …   13
graze/php-alpine           Smallish php7 alpine image with some common …   12                                      [OK]
phpdockerio/php7-cli       PHP 7 CLI base container image for PHPDocker…   2                                       [OK]

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

[root@ergeth23set3asr56erty45 ~]# docker pull php:7.4-fpm

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

[root@ergeth23set3asr56erty45 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php                 7.4-fpm             41b17b0f90e6        9 days ago          405MB

安装 Nginx 镜像

我们还是用 docker search nginx 命令来查看可用版本

[root@ergeth23set3asr56erty45 ~]# docker search nginx
NAME                               DESCRIPTION                                     STARS               OFFICIAL            AUTOMATED
nginx                              Official build of Nginx.                        14741               [OK]
jwilder/nginx-proxy                Automated Nginx reverse proxy for docker con…   2013                                    [OK]
richarvey/nginx-php-fpm            Container running Nginx + PHP-FPM capable of…   813                                     [OK]
jc21/nginx-proxy-manager           Docker container for managing Nginx proxy ho…   179
linuxserver/nginx                  An Nginx container, brought to you by LinuxS…   142
tiangolo/nginx-rtmp                Docker image with Nginx using the nginx-rtmp…   122                                     [OK]
jlesage/nginx-proxy-manager        Docker container for Nginx Proxy Manager        105                                     [OK]
bitnami/nginx                      Bitnami nginx Docker Image                      96                                      [OK]
alfg/nginx-rtmp                    NGINX, nginx-rtmp-module and FFmpeg from sou…   91                                      [OK]
jasonrivers/nginx-rtmp             Docker images to host RTMP streams using NGI…   89                                      [OK]
nginxdemos/hello                   NGINX webserver that serves a simple page co…   67                                      [OK]
privatebin/nginx-fpm-alpine        PrivateBin running on an Nginx, php-fpm & Al…   53                                      [OK]
nginx/nginx-ingress                NGINX Ingress Controller for Kubernetes         50
nginxinc/nginx-unprivileged        Unprivileged NGINX Dockerfiles                  32
staticfloat/nginx-certbot          Opinionated setup for automatic TLS certs lo…   20                                      [OK]
schmunk42/nginx-redirect           A very simple container to redirect HTTP tra…   19                                      [OK]
nginx/nginx-prometheus-exporter    NGINX Prometheus Exporter                       16
centos/nginx-112-centos7           Platform for running nginx 1.12 or building …   15
centos/nginx-18-centos7            Platform for running nginx 1.8 or building n…   13
bitwarden/nginx                    The Bitwarden nginx web server acting as a r…   12
flashspys/nginx-static             Super Lightweight Nginx Image                   10                                      [OK]
mailu/nginx                        Mailu nginx frontend                            8                                       [OK]
bitnami/nginx-ingress-controller   Bitnami Docker Image for NGINX Ingress Contr…   8                                       [OK]
ansibleplaybookbundle/nginx-apb    An APB to deploy NGINX                          2                                       [OK]
wodby/nginx                        Generic nginx                                   1                                       [OK]

这里我们拉取最新版的 Nginx 镜像

[root@ergeth23set3asr56erty45 ~]# docker pull nginx:latest

查看本地镜像

[root@ergeth23set3asr56erty45 ~]# docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
php                 7.4-fpm             41b17b0f90e6        9 days ago          405MB
nginx               latest              daee903b4e43        5 months ago        133MB

从上图中nginx TAG标签 可以看到我们已经安装了最新版本(latest)的 nginx 镜像。

 Nginx + PHP部署

创建php容器,设置端口映射、目录映射

[root@ergeth23set3asr56erty45 ~]#  docker run --name php-fpm -id -v ~/nginx/www:/usr/share/nginx/html php:7.4-fpm  

命令说明:

  •  -d: 后台运行容器,并返回容器ID;
  • --name php-fpm : 将容器命名为 php-fpm
  • -v ~/nginx/www:/usr/share/nginx/html  : 将主机中项目的目录 www 挂载到容器的 /usr/share/nginx/html

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

 [root@ergeth23set3asr56erty45 ~]# mkdir ~/nginx/conf/conf.d

在该目录下添加 ~/nginx/conf/conf.d/runoob-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   /usr/share/nginx/html/$fastcgi_script_name;
        include        fastcgi_params;
    }
}

配置文件说明:

  • php:9000: 表示 php-fpm 服务的 URL,下面我们会具体说明。
  • /www/: 是 myphp-fpm 中 php 文件的存储路径,映射到本地的 ~/nginx/www 目录。

启动 nginx:

[root@ergeth23set3asr56erty45 ~]# docker run --name runoob-php-nginx -p 80:80 -d -v ~/nginx/www:/usr/share/nginx/html:ro  -v ~/nginx/conf/conf.d:/etc/nginx/conf.d:ro  nginx
  • -p 80:80: 端口映射,把 nginx 中的 80 映射到本地的 80 端口 ,格式为:主机(宿主)端口:容器端口, -p 80[主机端口]:80[ nginx容器端口]
  • ~/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,显示如下:

猜你喜欢

转载自blog.csdn.net/beenles/article/details/115894099