使用Docker搭建LAMP环境,上线wordpress
1,环境
系统版本:CentOS Linux release 7.5.1804
docker版本:Docker version 1.13.1
主机IP地址:192.168.116.128
前提条件:
关闭selinux
已经安装好docker
2,拉取mysql和PHP镜像
[root@localhost ~]# docker pull mysql
[root@localhost ~]# docker pull php:7.2-apache
3,创建网络
[root@localhost ~]# docker network create lamp
2126534bba917d449091c868584d71528b16f3c60d91088af8d82b3fc0c9ac6c
查看网络
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
dc97fa388a53 bridge bridge local
1e70fdb4b1ca host host local
2126534bba91 lamp bridge local
c40bc4ba2760 none null local
4,创建生产mysql和php容器的脚本
[root@localhost ~]# vim 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
5,启动mysql和PHP容器
[root@localhost ~]# bash docker_lamp.sh mysql
cd91996a5842a82a3368598f8d38f76e4a8879bb93c2ed77bfa8e7fd3085c377
[root@localhost ~]# bash docker_lamp.sh httpd_php
c456a5114903bd20cbc3484d4232ca58e57af4c92ea0464df01f2b49f869946c
[root@localhost ~]#
查看容器
[root@localhost ~]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6e459971afa0 php:7.2-apache "docker-php-entryp..." 3 seconds ago Up 2 seconds 0.0.0.0:80->80/tcp httpd-php
7ba5e9464247 mysql:latest "docker-entrypoint..." About a minute ago Up About a minute 0.0.0.0:3306->3306/tcp, 33060/tcp mysql
6,写一个php的首页文件来进行测试
echo "<?php phpinfo(); ?>" > /data/docker/www/index.php
通过浏览器访问http://192.168.116.128
7,修改mysql的密码加密方式为mysql_native_password
[root@localhost ~]# vim /data/docker/mysql/conf/docker_mysql.cnf
[mysqld]
default-authentication-plugin=mysql_native_password
8,数据库的操作
登录数据库容器,创建、配置wordpress数据库
[root@localhost ~]# docker exec -it mysql /bin/bash
root@7ba5e9464247:/# mysql -uroot -pTest1234
mysql> create database wordpress;
mysql> create user wps@localhost identified by '123456';
mysql> grant all privileges on wordpress.* to wps@localhost;
mysql> create user wps@172.18.0.1 identified by '123456';
mysql> grant all privileges on wordpress.* to wps@172.18.0.1;
mysql> alter user wps@172.18.0.1 identified with mysql_native_password by '123456';
mysql> flush privileges;
mysql> exit
root@7ba5e9464247:/# exit
9,下载wordpress博客
[root@localhost ~]# wget https://cn.wordpress.org/wordpress-4.9.4-zh_CN.tar.gz
[root@localhost ~]# tar -xzvf wordpress-4.9.4-zh_CN.tar.gz -C /data/docker/www/
10,配置wordpress博客
1,修改wp-config-sample.php配置
[root@localhost ~]# cd /data/docker/www/wordpress/
[root@localhost wordpress]# mv wp-config-sample.php wp-config.php
[root@localhost wordpress]# vim wp-config.php
/** WordPress数据库的名称 */
define('DB_NAME', 'wordpress');
/** MySQL数据库用户名 */
define('DB_USER', 'wps');
/** MySQL数据库密码 */
define('DB_PASSWORD', '123456');
/** MySQL主机 */
define('DB_HOST', '172.18.0.1');
/** 创建数据表时默认的文字编码 */
define('DB_CHARSET', 'utf8');
/** 数据库整理类型。如不确定请勿更改 */
define('DB_COLLATE', '');
2,mysql容器里安装mysqli扩展
[root@localhost ~]# docker exec -it httpd-php /bin/bash
root@6e459971afa0:/var/www/html# apt-get update
root@6e459971afa0:/var/www/html# apt-get install libpng-dev
root@6e459971afa0:/var/www/html# cd /usr/local/bin/
root@6e459971afa0:/usr/local/bin# ./docker-php-ext-install gd mysqli
root@6e459971afa0:/usr/local/bin# ./docker-php-ext-enable gd mysqli
root@6e459971afa0:/usr/local/bin# exit
[root@localhost ~]# docker restart httpd-php
3,修改wp-db.php文件,将mysql_connect替换为mysqli_connect
[root@localhost ~]# cd /data/docker/www/wordpress/
[root@localhost wordpress]# sed -i "s/mysql_connect/mysqli_connect/g" wp-includes/wp-db.php
11,网页配置wordpress
关闭防火墙
浏览器访问http://192.168.116.128/wordpress
按前面的步骤填写完连接mysql的配置信息后,看到如下页面,表示mysql连接成功。
需要手工创建wp-config.php文件,将以上信息贴入wp-config.php文件中
vim /data/docker/www/wordpress/wp-config.php
然后点击“现在安装”,出现以下页面
输入完以上信息后,点击“安装WordPress”
至此,docker搭建lamp上线wordpress博客系统就搭建完成了。