首先安装docker
[root@localhost ~] docker version
Client: Docker Engine - Community
Version: 19.03.8
API version: 1.39 (downgraded from 1.40)
Go version: go1.12.17
Git commit: afacb8b
Built: Wed Mar 11 01:27:04 2020
OS/Arch: linux/amd64
Experimental: false
ip
ip 192.168.245.173
0.关闭防火墙和setenforce 0
systemctl stop firewalld
setenforce 0
1.docker安装mysql,和php
[root@localhost ~] docker pull mysql
[root@localhost ~] docker pull php:7.2-apache
2.创建网络
[root@localhost ~] docker network create lamp
22ba3b0cb1d2bb63b07f5e539db6fa6ca422caecf546480ed2278b651c613db9
查看网络
[root@localhost ~] docker network ls
NETWORK ID NAME DRIVER SCOPE
628e30069300 bridge bridge local
f590b214545f host host local
22ba3b0cb1d2 lamp bridge local
06f692ae94ab none null local
3.创建生成mysql和php容器的脚本
[root@localhost ~] vim docker_lamp.sh ^C
[root@localhost ~] cat docker_lamp.sh
#!/bin/bash
function mysql()
{
docker run --name mysql --net lamp -p 3306:3306 \
-v /data/docker/mysql/data:/var/lib/mysql \
-v /data/docker/mysql/conf:/etc/mysql/conf.d \
-v /data/docker/mysql/logs:/logs \
-e MYSQL_ROOT_PASSWORD=Test1234 \
-d mysql:latest --character-set-server=utf8
}
function httpd_php()
{
docker run --name httpd-php --net lamp -p 80:80 \
-v /data/docker/httpd/conf:/etc/apache2/sites-enabled \
-v /data/docker/www:/var/www/html \
-v /data/docker/httpd/logs:/var/log/apache2 \
-d php:7.2-apache
}
$1
4.启动mysql和php容器
[root@localhost ~] sh docker_lamp.sh mysql
acd4a3f3952d254c5fff8a53afe25f2ec8c2267e15565a8d273d26c72830121b
[root@localhost ~] sh docker_lamp.sh httpd_php
9ae9335f22f0fc712e512a31afffb2b8fd5652da1c96a14b246262316cdd647a
注释:如果不行用 bash docker_lamp.sh mysql 启动试一试
5.查看容器
[root@localhost ~] docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
9ae9335f22f0 php:7.2-apache "docker-php-entrypoi…" 3 minutes ago Up 3 minutes 0.0.0.0:80->80/tcp httpd-php
acd4a3f3952d mysql:latest "docker-entrypoint.s…" 3 minutes ago Up 3 minutes 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
2af772b5223c linode/lamp "/bin/bash" 44 minutes ago Up 44 minutes 0.0.0.0:79->80/tcp, 0.0.0.0:3305->3306/tcp my_lamp
6.书写测试页面
为目录赋权
[root@localhost ~] useradd dkuser#如果没有就创建
[root@localhost ~] chown -R dkuser:dkuser /data
写测试页面
[root@localhost ~] echo "<?php phpinfo();?>" > /data/docker/www/index.php
浏览器访问地址 192.168.245.173,出现 PHP 的页面就说明是OK的。
7、修改mysql密码加密方式为mysql_native_password
[root@localhost ~] vim /data/docker/mysql/conf/docker_mysql.conf
[root@localhost ~] cat /data/docker/mysql/conf/docker_mysql.conf
[mysqld]
default-authentication-plugin=mysql_native_password
8.数据库操作
登陆mysql容器,创建、配置wordpress数据库
[root@localhost ~] docker exec -it mysql /bin/bash
root@acd4a3f3952d:/ chown -R mysql:mysql /var/lib/mysql#注意要授权
root@acd4a3f3952d:/ mysql -uroot -pTest1234
mysql> create database wordpress;
Query OK, 1 row affected (0.01 sec)
mysql> create user wps@localhost identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all privileges on wordpress.* to wps@localhost;
Query OK, 0 rows affected (0.00 sec)
mysql> create user wps@172.18.0.1 identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> grant all privileges on wordpress.* to wps@172.18.0.1;
Query OK, 0 rows affected (0.01 sec)
mysql> alter user wps@172.18.0.1 identified with mysql_native_password by '123456';
Query OK, 0 rows affected (0.00 sec)
9.配置wordpress博客系统
[root@localhost ~] wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
#解压到 /data/docker/www
[root@localhost ~] tar zxf wordpress-4.9.4-zh_CN.tar.gz -C /data/docker/www
9.2修改wordpress配置
[root@localhost ~] cd /data/docker/www/wordpress/
[root@localhost wordpress] vim wp-config-sample.php#配置文件
// ** MySQL 设置 - 具体信息来自您正在使用的主机 ** //
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'wps');
/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');
/** MySQL主机 */
define('DB_HOST', '192.168.245.173');
/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');
/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');
/**#@+
9.3备份配置文件
[root@localhost wordpress] mv wp-config-sample.php wp-config.php
9.4、修复问题
从PHP5.0开始不推荐使用mysql_connect()函数,从PHP7.0开始就废弃了该函数,替代的函数是mysqli_connect(),下面是解决办法:
进入php容器安装mysqli扩展
[root@localhost wordpress] docker exec -it httpd-php /bin/bash
root@9ae9335f22f0:/var/www/html apt-get update
root@9ae9335f22f0:/var/www/html apt-get install libpng-dev
root@9ae9335f22f0:/usr/local/bin cd /usr/local/bin/
root@9ae9335f22f0:/usr/local/bin ./docker-php-ext-install gd mysqli
root@9ae9335f22f0:/usr/local/bin ./docker-php-ext-enable gd mysql
退出容器,重启容器
[root@localhost wordpress] docker restart httpd-php
httpd-php
替换方法
[root@localhost wordpress] cd /data/docker/www/wordpress/
[root@localhost wordpress] sed -i "s/mysql_connect/mysqli_connect/g" wp-includes/wp-db.ph
9.5、打开wordpress页面进行配置
打开页面 http://192.168.121.121/wordpress —> 在出现的页面点击 “现在就开始” —> 填写下面所示的数据库信息:
数据库名 wordpress
用户名 wps
密码 123456
数据库主机 192.168.245.173
表前缀 wp_
—> 点击 “安装WordPress” —> 点击 “登陆” —> 使用账户和密码登陆博客,之后就可以进入博客页面,进而对博客进行管理了