CentOS 7 Docker方式安裝 PHP,Mysql,phpmyadmin 過程記錄

20190408 三個月前學習 docker,打算在實際環境部署一次 PHP + Mysql,可是此次轉到了 CentOS,沒走幾步就卡殼了!只好在 CentOS7 下從頭學習一次

實戰和學習老是有許多差異的!多走幾輪,總會用得上的!

環境: 一、 visualbox 虛擬機 CentOS 7.6 (IP:192.168.1.184) 只安裝了nginx (本次不作 Docker nginx 容器) 二、 visualbox 虛擬機 CentOS 7.6(IP:192.168.1.188) 安裝 nginx,PHP,mysql,phpmyadminphp

** 爲了對照學習,用於 docker 的服務器上,本地安裝 nginx以外,再也不安裝 PHP,mysql,phpmyadmin ** 過程當中主要問題出如今 nginx 的 php 配置 ,mysql 的數據卷html

一、安裝 Docker

  1. 官網 https://docs.docker.com/install/ 找到 linux --Centosmysql

  2. 安裝必要的倉庫 sudo yum install -y yum-utils
    device-mapper-persistent-data
    lvm2linux

  3. 設置使用穩定版倉庫 sudo yum-config-manager
    --add-repo
    https://download.docker.com/linux/centos/docker-ce.reponginx

  4. 安裝 DOCKER CE sudo yum install docker-ce docker-ce-cli containerd.iosql

  5. 列舉版本,能夠選擇特定版本 yum list docker-ce --showduplicates | sort -r ...... docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable * base: mirrors.aliyun.comdocker

    使用方法: sudo yum install docker-ce-<VERSION_STRING> docker-ce-cli-<VERSION_STRING> containerd.ioubuntu

  6. 啓動 docker sudo systemctl start docker sudo systemctl enable dockervim

  7. 確認 docker -v Docker version 18.09.4, build d14af54266centos

  8. 測試一下 sudo docker run hello-world Hello from Docker! This message shows that your installation appears to be working correctly. ......

  9. 查看鏡像 sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE hello-world latest fce289e99eb9 2 months ago 1.84kB

二、安裝 PHP

sudo docker pull php ** latest 版本是 7.2 的,本次只處理 php5.6 sudo docker pull php:5.6-fpm

sudo docker images REPOSITORY TAG IMAGE ID CREATED SIZE php latest 90102e6d83ad 47 hours ago 367MB php 5.6-fpm 3458979c7744 2 months ago 344MB hello-world latest fce289e99eb9 2 months ago 1.84kB

sudo docker run -p 9000:9000 --name myphp56 -v /usr/share/nginx/html:/var/www/html -d php:5.6-fpm

參數說明:

--name  myphp56:將容器命名爲 myphp56
-v /usr/share/nginx/html:/var/www/html : 將宿主機的網站目錄(/usr/share/nginx/html)掛載到容器的網站目錄(/var/www/html)

-p 9000:9000:將容器myphp72的 9000 端口映射到宿主機的 9000 端口			
	*** php 缺省端口是 9000 ,若是宿主機也安裝過php,可能會衝突,能夠改到 9001

三、測試 nginx + php

** 本次重點測試 docker 。因此, nginx 的配置直接修改 nginx.conf ,端口方式訪問或域名方式網站另行配置

  1. 修改 nginx 配置,加入 php 配置 cd /etc/nginx sudo vim nginx.conf 加入 php 文件的 location

location ~ .php$ { # include fastcgi.conf; # this is on local #root /usr/share/nginx/html; # thsi is on docker,not local /var/www/html root /var/www/html;

fastcgi_split_path_info ^(.+\.php)(/.+)$;
            #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
            fastcgi_pass 127.0.0.1:9000;
            fastcgi_param SCRIPT_FILENAME
            $document_root$fastcgi_script_name;
            fastcgi_param PATH_INFO $uri;
            fastcgi_param SCRIPT_NAME $fastcgi_script_name;
            fastcgi_index index.php;
            include fastcgi_params;
            }

** centos 7 缺省的 nginx WEB 目錄不在 /var/www/html ,而是 /usr/share/nginx/html ** 以上 root /var/www/html; 指的是 宿主機的 /var/www/html 映射到 docker 容器傷的 WEB 目錄

  1. 創建一個 index.php 文件,用於測試 cd /var/www/html sudo vim index.php

    <?php phpinfo(); ?>

  2. 啓動 docker php 容器 docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 15 minutes 0.0.0.0:9000->9000/tcp myphp56

    docker start myphp56

    docker ps -s CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES SIZE dd0a3155e7ee php:5.6-fpm "docker-php-entrypoi…" 3 days ago Up 14 minutes 0.0.0.0:9000->9000/tcp myphp56 8B (virtual 344MB)

  3. 測試一下 curl 127.0.0.1/index.php curl 192.168.1.184/index.php 瀏覽器下打開網址:http://192.168.1.184/index.php 在這裏插入圖片描述

四、Docker PHP 錯誤處理

  1. 頁面暫時找不到 The page you are looking for is temporarily unavailable. Please try again later.

    docker ps -s 檢查一下, docker myphp56 沒有 start

  2. 找不到文件 curl 127.0.0.1/index.php File not found.

    檢查 nginx.conf php 模塊中 root 定義,容易犯錯誤的是:將宿主機的 WEB 目錄 和 docker 容器的目錄搞混! 以前在 ubuntu 沒有遇到這個問題,那裏宿主機和 docker 的WEB 目錄都是 /var/www/html 如下是個人 php 配置部分 location ~ .php$ { # include fastcgi.conf; # this is on local # root /usr/share/nginx/html; # thsi is on docker,not local /var/www/html root /var/www/html;

    fastcgi_split_path_info ^(.+\.php)(/.+)$;
             #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
             fastcgi_pass 127.0.0.1:9000;
             fastcgi_param SCRIPT_FILENAME
             $document_root$fastcgi_script_name;
             fastcgi_param PATH_INFO $uri;
             fastcgi_param SCRIPT_NAME $fastcgi_script_name;
             fastcgi_index index.php;
             include fastcgi_params;
    
             }
  3. 瀏覽器打開變成下載

    1).重點檢查  fastcgi_pass 的配置
     ** 我這裏安裝的 Centos 沒有 php-fpm.sock,docker 裏面也沒有,因此只能使用端口方式
    
        #fastcgi_pass unix:/var/run/php-fpm/php-fpm.sock; 
        fastcgi_pass 127.0.0.1:9000;
    
      2). Centos 7.6 的防火牆規則須要開啓 Docker 映射的 9000 端口
      若是宿主機的 firewalld 是開啓的,最好一塊兒開放 80, 443, 9000等端口,不然,本地 curl 多是對的,在局域網瀏覽器也打不開!
    
     sudo firewall-cmd --zone=public --add-port=80/tcp --permanent
     sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
     sudo firewall-cmd --zone=public --add-port=9000/tcp --permanent
    
     sudo systemctl restart firewalld.service

五、安裝 Docker mysql

docker search mysql

sudo docker pull mysql ** latest 版本是 mysql 8.0,本次處理 mysql 5.7

sudo docker pull mysql:5.6 sudo docker pull mysql:5.7

sudo docker ps REPOSITORY TAG IMAGE ID CREATED SIZE mysql 5.6 bc788a6a2745 12 days ago 256MB mysql 5.7 98455b9624a9 12 days ago 372MB mysql latest 7bb2586065cd 12 days ago 477MB phpmyadmin/phpmyadmin latest c6ba363e7c9b 2 months ago 166MB php 5.6-fpm 3458979c7744 2 months ago 344MB

先使用 latest 試試 sudo docker run --name mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql

docker inspect mysql ...... "Env": [ "MYSQL_ROOT_PASSWORD=123456", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", "GOSU_VERSION=1.7", "MYSQL_MAJOR=8.0", "MYSQL_VERSION=8.0.15-1debian9" ], ......

再啓動 mysql 5.7 sudo docker run --name mysql57 -v mysql57:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7

參數說明:
--name mysql57 給容器取名爲 mysql57
-v mysql57:/var/lib/mysql 掛在一個volume(取名爲mysql57)到容器的/var/lib/mysql
**給volume(數據卷)取名便於管理
** mysql 缺省的數據都是放在 /var/lib/mysql 的

-e MYSQL_ROOT_PASSWORD=123456 設置mysql環境的root密碼
-p 3306:3306 宿主機的 3306 端口(前一個3306)對應到docker 的 3306 端口(後面的 3306)

出現錯誤!

docker: Error response from daemon: Conflict. The container name "/mysql57" is already in use by container "7650608f3e1fc6f85614f059c9049f9259993cfcc4863cb566d504311d00df33". You have to remove (or rename) that container to be able to reuse that name.
	See 'docker run --help'.

** 端口衝突了! 簡單處理,直接刪除 mysql 8.0 容器

docker ps -s

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                               NAMES               SIZE
56605931a504        mysql               "docker-entrypoint.s…"   6 minutes ago       Up 6 minutes        0.0.0.0:3306->3306/tcp, 33060/tcp   mysql               7B (virtual 477MB)
dd0a3155e7ee        php:5.6-fpm         "docker-php-entrypoi…"   3 days ago          Up 26 minutes       0.0.0.0:9000->9000/tcp              myphp56             8B (virtual 344MB)

docker stop mysql

docker rm mysql

再次啓動 mysql 5.7 docker start mysql57 ** 雖然剛纔由於衝突啓動失敗,可是 mysql57 容器已經創建,因此,直接 start 便可

查看 mysql 容器信息 sudo docker inspect mysql57

...
"Mounts": [
            {
                "Type": "volume",
                "Name": "mysql",
                "Source": "/var/lib/docker/volumes/mysql/_data",
                "Destination": "/var/lib/mysql",
                "Driver": "local",
                "Mode": "z",
                "RW": true,
                "Propagation": ""
            }
...
"Env": [
                "MYSQL_ROOT_PASSWORD=123456",
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "GOSU_VERSION=1.7",
                "MYSQL_MAJOR=5.7",
                "MYSQL_VERSION=5.7.25-1debian9"
            ],
...
"Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.3",
...

六、 鏈接 mysql57 容器測試

  1. 宿主機上遠程鏈接 (剛纔inspect 到的容器 IP= 172.17.0.3)

    mysql -h172.17.0.3 -uroot -p123456

    Welcome to the MariaDB monitor.  Commands end with ; or \g.
     Your MySQL connection id is 3
     Server version: 5.7.25 MySQL Community Server (GPL)
    
     Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
    
     Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
    
     MySQL [(none)]> select version();
     +-----------+
     | version() |
     +-----------+
     | 5.7.25    |
     +-----------+
     1 row in set (0.00 sec)
  2. 進入容器查看 docker exec -it mysql57 bash

root@e80eaaa17d57:/# mysql -V

mysql  Ver 14.14 Distrib 5.7.25, for Linux (x86_64) using  EditLine wrapper

容器內本地鏈接 mysql mysql -uroot -p123456 ... mysql> show databases;

+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

七、 安裝 Docker phpMyAdmin

查找 phpmyadmin 鏡像 sudo docker search phpmyadmin

docker pull phpmyadmin/phpmyadmin

** 提早運行前面安裝的 Docker php56 和 mysql 57

sudo docker run --name phpmyadmin --link mysql57:db -p 9998:80 -d phpmyadmin/phpmyadmin 參數說明:

--name phpmyadmin 容器取名 phpmyadmin
--link mysql57:db 鏈接到 容器mysql ,給這個 link 一個別名爲db
-p 9998:80 宿主機9998 端口對應到 phpmyadmin的 80 端口

sudo docker ps -s

CONTAINER ID        IMAGE                   COMMAND                  CREATED             STATUS              PORTS                               NAMES               SIZE
0f5599cb72a6        phpmyadmin/phpmyadmin   "/run.sh supervisord…"   15 seconds ago      Up 14 seconds       9000/tcp, 0.0.0.0:9998->80/tcp      phpmyadmin          33.7MB (virtual 200MB)
e80eaaa17d57        mysql:5.7               "docker-entrypoint.s…"   21 minutes ago      Up 21 minutes       0.0.0.0:3306->3306/tcp, 33060/tcp   mysql57             157B (virtual 372MB)
dd0a3155e7ee        php:5.6-fpm             "docker-php-entrypoi…"   3 days ago          Up 2 hours          0.0.0.0:9000->9000/tcp              myphp56             8B (virtual 344MB)

查看 phpmyadmin 容器信息 docker inspect phpmyadmin

...
 "Ports": {
                "80/tcp": [
                    {
                        "HostIp": "0.0.0.0",
                        "HostPort": "9998"
                    }
                ],
                "9000/tcp": null
            },
...
"Env": [
                "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
                "PHPIZE_DEPS=autoconf \t\tdpkg-dev dpkg \t\tfile \t\tg++ \t\tgcc \t\tlibc-dev \t\tmake \t\tpkgconf \t\tre2c",
                "PHP_INI_DIR=/usr/local/etc/php",
                "PHP_EXTRA_CONFIGURE_ARGS=--enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi",
                "PHP_CFLAGS=-fstack-protector-strong -fpic -fpie -O2",
                "PHP_CPPFLAGS=-fstack-protector-strong -fpic -fpie -O2",
                "PHP_LDFLAGS=-Wl,-O1 -Wl,--hash-style=both -pie",
                "GPG_KEYS=1729F83938DA44E27BA0F4D3DBDB397470D12172 B1B44D8F021E4E2D6021E995DC9FF8D3EE5AF27F",
                "PHP_VERSION=7.2.14",
                "PHP_URL=https://secure.php.net/get/php-7.2.14.tar.xz/from/this/mirror",
                "PHP_ASC_URL=https://secure.php.net/get/php-7.2.14.tar.xz.asc/from/this/mirror",
                "PHP_SHA256=ee3f1cc102b073578a3c53ba4420a76da3d9f0c981c02b1664ae741ca65af84f",
                "PHP_MD5=",
                "VERSION=4.8.5",
                "URL=https://files.phpmyadmin.net/phpMyAdmin/4.8.5/phpMyAdmin-4.8.5-all-languages.tar.xz"
            ],
            
                    "Gateway": "172.17.0.1",
                    "IPAddress": "172.17.0.4",
 ...

8 、測試 Docker phpMyAdmin

docker port phpmyadmin 80/tcp -> 0.0.0.0:9998

curl 127.0.0.1:9998 curl 192.168.1.184:9998

http://192.168.1.184:9998/

登陸查看 mysql

因爲本次學習的虛擬機上,沒有安裝 php 和 mysql ,因此,以上信息必定是 Docker 容器的

相關文章
相關標籤/搜索