一、环境准备
- 1.一台安装好docker的宿主机,安装参考上一篇
- 2.准备wordpress代码包,并规划好目录再解压
下载地址:
链接:https://pan.baidu.com/s/1kZFhC_h2mTDQ5K37z2jzRQ
提取码:8puw
二、部署docker内环境
- 1.先pull一个centos:6.9的镜像并运行
[root@docker01 ~]#docker pull centos:6.9
[root@docker01 ~]# docker run -it -p 80:80 -p 3306:3306 centos:6.9 /bin/bash
搭建wordpress需要的服务,nginx, mysql, php-fpm,因为要使用nginx与mysql所以做好端口映射80,3306
- 2.先配置yum源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo #容器为centos6.9,所以使用6的源
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo
- 3.安装需要使用的服务
yum install -y nginx mysql-server php-fpm - 4.创建代码存放目录
在容器内创建存放代码的目录
mkdir /data
上传代码并解压到指定位置
unzip -d /data/ wordpress-5.0.3-zh_CN.zip
- 5.修改nginx主配置文件vim /etc/nginx/nginx.conf
[root@fbd124eb2143 nginx]# cat nginx.conf
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root /data; #站点目录
index index.php index.html index.htm;
}
location ~ \.php$ {
root /data;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /data$fastcgi_script_name;
include fastcgi_params;
}
}
}
检查语法
[root@fbd124eb2143 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
启动服务
service nginx start
[root@fbd124eb2143 nginx]# service nginx start
Starting nginx: [ OK ]
- 5.启动mysql
service mysqld start - 6.创建wordpress数据库
[root@fbd124eb2143 ~]# mysql -uroot
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| test |
| wordpress |
+--------------------+
4 rows in set (0.00 sec)
创建并授权用户
mysql> grant all on wordpress.* to wordpress@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> grant all on wordpress.* to wordpress@'localhost' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
删除无用用户
mysql> select user,host from mysql.user;
+-----------+--------------+
| user | host |
+-----------+--------------+
| wordpress | 10.0.0.% |
| root | 127.0.0.1 |
| | fbd124eb2143 |
| root | fbd124eb2143 |
| | localhost |
| root | localhost |
| wordpress | localhost |
+-----------+--------------+
7 rows in set (0.00 sec)
mysql> drop user ''@'localhost' ;
Query OK, 0 rows affected (0.01 sec)
mysql> drop user ''@'fbd124eb2143' ;
Query OK, 0 rows affected (0.01 sec)
mysql> select user,host from mysql.user;
+-----------+--------------+
| user | host |
+-----------+--------------+
| wordpress | 10.0.0.% |
| root | 127.0.0.1 |
| root | fbd124eb2143 |
| root | localhost |
| wordpress | localhost |
+-----------+--------------+
5 rows in set (0.00 sec)
更新一下权限信息
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
mysql> Bye
- 7.启动php-fpm
service php-fpm start - 8.访问本机ip及wordpress目录,发现提示缺少扩展包
image.png
- 9.按提示安装php-mysql扩展包并重启
yum install php-mysql -y
rpm -qa|grep -i php
[root@1c52188827fe wordpress]# rpm -qa|grep -i php
php-fpm-5.3.3-49.el6.x86_64
php-pdo-5.3.3-49.el6.x86_64
php-common-5.3.3-49.el6.x86_64
php-mysql-5.3.3-49.el6.x86_64
service php-fpm restart
- 10.重新访问本机ip及wordpress目录并按要求配置操作,最终显示页面
image.png
- 11.在容器内写入启动脚本
cat /init.sh
#!/bin/bash
service mysqld start
service php-fpm start
nginx -g 'daemon off;'
- 12.把容器提交为镜像
[root@docker ~]# docker commit 1c52188827fe centos:6.9_wordpress
sha256:76e675b795f0c3a105fc20aba0c1e04749b678cbfae483fbc55df17c43e54f38
[root@docker ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 6.9_wordpress 76e675b795f0 13 seconds ago 559MB
centos6.9_ssh v1 3b3df71e3b73 5 days ago 195MB
nginx latest f68d6e55e065 10 days ago 109MB
centos 6.8 82f3b5f3c58f 3 months ago 195MB
centos 6.9 2199b8eb8390 3 months ago 195MB
- 12.对刚提交的镜像进行测试
先停掉容器
运行刚刚提交的镜像并配合脚本开启
网友评论