Docker学习(3)—搭建PHP环境(安装wordpress为例)

1、需要一个php容器,一个mysql容器,一个nginx容器
下载nginx、php:7.2-fpm、mysql镜像:
docker pull nginx
docker pull php:7.2-fpm
docker pull mysql

注:docker官方提供的php镜像是没有fpm的

2、建立相应目录
mkdir -p /appserver/code
mkdir -p /appserver/mysql
mkdir -p /appserver/nginx
mkdir -p /appserver/logs
cd /tmp
wget https://cn.wordpress.org/latest-zh_CN.tar.gz
tar -zxvf latest-zh_CN.tar.gz -C /appserver/code/
chown root:root -R /appserver/code/

3、启动mysql容器
docker run -itd -v /appserver/mysql:/var/lib/mysql -p 33066:3306 --name=mysql -e MYSQL_ROOT_PASSWORD=123456 mysql

4、创建数据库
docker exec -it mysql /bin/bash
mysql -u root -p
create database wordpress;
create user 'wp'@'%' identified by '123456';
grant all on wordpress.* to 'wp'@'%';
alter user 'wp'@'%' identified with mysql_native_password by '123456'; (mysql8远程连接需要将加密方式从caching_sha2_password改为mysql_native_password)
flush privileges;

5、启动php容器
由于php:7.2-fpm的工作目录是/var/www/html,我们就以这个目录作为代码路径
docker run -itd --link mysql:mysql -v /appserver/code/wordpress:/var/www/html --name=php php:7.2-fpm

6、添加php容器mysql支持
增加pdo_mysql和mysqli模块:
docker exec -it php /bin/bash
docker-php-ext-install pdo_mysql mysqli

重启容器:
docker restart php

7、修改wp-db.php文件,将mysql_connect替换为mysqli_connect
cd /appserver/code/wordpress/
sed -i "s/mysql_connect/mysqli_connect/g" wp-includes/wp-db.php

注:
否则会报错Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /var/www/html/wp-includes/wp-db.php
原因是php7.2已经将mysql_connect()删除

8、建立nginx配置文件
vi /appserver/nginx/default.conf

server {
    listen       80;
    server_name  localhost;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        root   /var/www/html;
        index  index.html index.htm index.php;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /var/www/html;
    }

    # proxy the PHP scripts to Apache listening on 127.0.0.1:80
    #
    #location ~ \.php$ {
    #    proxy_pass   http://127.0.0.1;
    #}

    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        root           /var/www/html;
        #将127.0.0.1改为php
        fastcgi_pass   php:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #    deny  all;
    #}
}

9、启动nginx容器
docker run -itd -p 80:80 -v /appserver/nginx/default.conf:/etc/nginx/conf.d/default.conf -v /appserver/logs/:/var/log/nginx/ -v /appserver/code/wordpress:/var/www/html --link php:php --name=nginx nginx
说明:
这里挂载了3个地址
1)nginx配置文件
2)nginx日志路径
3)php代码路径

10、安装wordpress
访问http://IP地址
按照提示输入数据库名,用户名,密码。数据库主机填容器的名称(mysql)
由于没有写入权限需要手工建立wp-config.php配置文件

11、其他
1)mysql远程连接命令
mysql -h 服务器IP地址 -P 端口 -u wp -p
2)为什么远程连接要将加密方式改为旧的
客户端不支持:Authentication plugin 'caching_sha2_password' cannot be loaded: /usr/lib64/mysql/plugin/caching_sha2_password.so: cannot open shared object file: No such file or directory
3)查看用户加密方式的sql语句
select host, user, authentication_string, plugin from mysql.user;

参考资料:
https://blog.51cto.com/andyxu/2177116
https://blog.csdn.net/wd2014610/article/details/89023562