Docker 搭建 LNMP + Wordpress(详细步骤)

一、项目模拟

1. 项目环境

公司在实际的生产环境中,需要使用 Docker 技术在一台主机上创建 LNMP 服务并运行 Wordpress 网站平台。然后对此服务进行相关的性能调优和管理工作。
所有安装包下载:

wget http://101.34.22.188/lnmp_wordpress/mysql-boost-5.7.20.tar.gz
wget http://101.34.22.188/lnmp_wordpress/nginx-1.12.0.tar.gz
wget http://101.34.22.188/lnmp_wordpress/php-7.1.10.tar.bz2
wget http://101.34.22.188/lnmp_wordpress/wordpress-4.9.4-zh_CN.tar.gz
> 或者
wget -r -np http://101.34.22.188/lnmp_wordpress/

2. 服务器环境

容器 操作系统 IP地址 主要软件
nginx CentOS 7.9 x86_64 172.111.0.10 Docker-Nginx
mysql Centos 7.9 x86_64 172.111.0.20 Docker-Mysq
php Centos 7.9 x86_64 172.111.0.30 Docker-php
  1. 任务需求
  • 使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台
  • 限制 Nginx 容器最多使用 500MB 的内存和 1G 的 Swap
  • 限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s
  • 将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地

二、Linux 系统基础镜像

[root@docker ~]# systemctl disable --now firewalld
[root@docker ~]# setenforce 0
setenforce: SELinux is disabled
[root@docker ~]# docker pull centos:7
#从公有仓库中下载 centos7 作为系统基础镜像
[root@docker ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
centos       7         eeb6ee3f44bd   4 weeks ago   204MB

三、Nginx

1. 建立工作目录

[root@docker ~]# mkdir /opt/nginx
[root@docker ~]# cd /opt/nginx
[root@docker nginx]# rz -E
rz waiting to receive.
#上传 nginx 安装包 nginx-1.12.0.tar.gz
[root@docker nginx]# rz -E
rz waiting to receive.
#上传 wordpress 服务包 wordpress-4.9.4-zh_CN.tar.gz

2. 编写 Dockerfile 脚本

[root@docker nginx]# vim Dockerfile
 
FROM centos:7
MAINTAINER this is nginx image <lnmp>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin nginx
ADD nginx-1.12.0.tar.gz /usr/local/src/
WORKDIR /usr/local/src/nginx-1.12.0
RUN ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module;make -j 4 && make install
ENV PATH /usr/local/nginx/sbin:$PATH
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html
RUN chmod 777 -R /usr/local/nginx/html/
EXPOSE 80
VOLUME [ "/usr/local/nginx/html/" ]
CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]

3. 准备 nginx.conf 配置文件

[root@docker nginx]# ls
Dockerfile  nginx-1.12.0.tar.gz  nginx.conf  wordpress-4.9.4-zh_CN.tar.gz
[root@docker nginx]# egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"
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;
        charset utf-8;
        location / {
   
            root   html;
            index  index.html index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
   
            root   html;
        }
        location ~ \.php$ {
   
            root           html;
            fastcgi_pass   172.111.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}

4. 生成镜像

[root@docker nginx]# docker build -t nginx:lnmp .
[root@docker nginx]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
nginx        lnmp      35a6404fcfa1   5 seconds ago   522MB
centos       7         eeb6ee3f44bd   4 weeks ago     204MB

5. 创建自定义网络

[root@docker nginx]# docker network create --subnet=172.111.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork
0cbe1bd0bd782bf1c8e69916d99427970196de22deb312f970e01030406d9b45
[root@docker nginx]# docker network ls
NETWORK ID     NAME        DRIVER    SCOPE
dd7a55d01f86   bridge      bridge    local
63ddf1e359e9   host        host      local
0cbe1bd0bd78   mynetwork   bridge    local
a4b66a8a6cd2   none        null      <
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值