Docker项目部署lnmp+wordpress

Posted 水木,年華

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Docker项目部署lnmp+wordpress相关的知识,希望对你有一定的参考价值。

一.项目环境

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

1.1 环境描述

 主机	         操作系统	     IP地址	      主要软件
Docker	CentOS 7.3 x86_64	 192.168.80.4	Docker 19.03

1.2 项目需求

(1)使用Docker构建LNMP环境并运行Wordpress网站平台。
(2)限制nginx容器最多使用500Mb的内存和1G的Swap。
(3)限制mysql容器写 /dev/sda 的速率为 10 MB/s。
(4)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地。

二.部署

2.1 安装docker源

[root@docker ~]# yum install -y yum-utils device-mapper-persistent-data lvm2 #安装所需的包
[root@docker ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo  #添加阿里云的源

2.2 安装docker

[root@docker ~]# yum install -y docker-ce  #安装社区版docker
[root@docker ~]# systemctl enable docker   #开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@docker ~]# systemctl start  docker   #启动

2.3 创建nginx-php 、mysq目录

[root@docker ~]# mkdir docker && cd docker

创建创建nginx-php 、mysql目录

mkdir nignx mysql

2.4 Docker部署nginx-php

①上传nginx-1.12.2.tar.gz、wordpress-4.9.4-zh_CN.tar、libmcrypt-2.5.7.tar.gz、nginx.conf、wp-config.php到nginx/目录,解压

[root@docker nginx]#tar zxvf wordpress-4.9.4-zh_CN.tar
[root@docker nginx]#tar zxvf nginx-1.12.2.tar.gz
[root@docker nginx]#tar zxvf libmcrypt-2.5.7.tar.gz
[root@docker nginx]#tar zxvf php-5.5.38.tar.gz


②创建nginx-dockfile

[root@docker docker]# cd nginx 
[root@docker nginx]# vim  Dockerfile  
FROM docker.io/centos:7
RUN yum -y update
RUN yum -y install gcc gcc-c++ openssl-devel openssl autoconf cmake autoconf zlib zlib-devel libtool pcre pcre-devel wget net-tools make
RUN groupadd  -g 900 nginx && useradd nginx -g nginx -s /sbin/nologin
ADD nginx-1.12.2 nginx-1.12.2
RUN cd /nginx-1.12.2/ && make && make install
RUN ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
RUN sed -i '1afastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;' /usr/local/nginx/conf/fastcgi_params
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress /usr/local/nginx/html/wordpress
ADD wp-config.php /usr/local/nginx/html/wordpress
RUN yum -y install gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel  openssl automake make autoconf libtool zlib-devel make pcre-devel wget net-tools
ADD libmcrypt-2.5.7 libmcrypt-2.5.7
RUN cd libmcrypt-2.5.7/&& ./configure --prefix=/usr/local/libmcrypt && make && make install
ADD php-5.5.38 php-5.5.38
RUN  cd php-5.5.38/ && ./configure --prefix=/usr/local/php5.5 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install
RUN  cd php-5.5.38 && cp php.ini-production /etc/php.ini
RUN  cd /php-5.5.38 && cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
RUN  chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm && chkconfig php-fpm on
RUN  cp /usr/local/php5.5/etc/php-fpm.conf.default  /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's*;pid = run/php-fpm.pid*pid = run/php-fpm.pid*g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/user = nobody/user = nginx/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/group = nobody/group = nginx/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.max_children = 5/pm.max_children = 50/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.start_servers = 2/pm.start_servers = 5/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 5/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 30/g' /usr/local/php5.5/etc/php-fpm.conf
EXPOSE 9000
EXPOSE 80

③部署nginx-php

[root@docker nginx]# docker build -t "centos:nginx-php" . #构建nginx-php镜像
#需要几分钟
Successfully built 07a4acbb016b
Successfully tagged centos:nginx-php
[root@docker nginx]#  docker images   #列出镜像
REPOSITORY   TAG         IMAGE ID       CREATED          SIZE
centos       nginx-php   07a4acbb016b   57 seconds ago   1.52GB
centos       7           8652b9f0cb4c   9 months ago     204MB
[root@docker nginx]# docker run -dit -p 80:80 -m 500m --memory-swap 1G 07a4acbb016b /bin/bash  #容器能使用的内存和交换分区大小
cebe2bd3cbd0924c221c9eb423054ebafb04541c245df90c63b4b0bf92dccee7
[root@docker nginx]# docker ps        #查看当前运行容器
CONTAINER ID   IMAGE          COMMAND       CREATED              STATUS              PORTS                                         NAMES
cebe2bd3cbd0   07a4acbb016b   "/bin/bash"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   dazzling_buck
[root@docker nginx]# docker exec -itcebe2bd3cbd0   /bin/bash  #进入容器
[root@073de131844d /]# nginx  #开启服务
[root@cebe2bd3cbd0 /]# /etc/init.d/php-fpm start              #启动php
Starting php-fpm  done
[root@cebe2bd3cbd0 /]# netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      32/nginx: master pr 
[root@cebe2bd3cbd0 /]# netstat -antp | grep php  
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      50/php-fpm: master  
[root@073de131844d /]# exit 
exit

2.5 Docker部署mysql

①创建mysql-Dockerfile

[root@docker mysql]# vim /root/docker/mysql/Dockerfile
FROM docker.io/centos:7
RUN yum -y install gcc gcc-c++ make autoconf make cmake wget
RUN groupadd mysql; useradd -r -M -u 3306 -s /sbin/nologin -g mysql mysql
RUN mkdir /usr/local/mysql; mkdir /data/mysql -pv
RUN yum install gcc gcc-c++ ncurses-devel bison bison-devel -y
RUN wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.29.tar.gz
RUN tar xf mysql-5.6.29.tar.gz -C /usr/local/src/
WORKDIR /usr/local/src/mysql-5.6.29
RUN cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DEXTRA_CHARSETS=all -DDEFAULT_COLLATION=utf8_general_ci -DWITH-MYSQLD-LDFLAGS=-all-static -DWITH-CLIENT-LD-FLAGS=-all-static -DWITH_DEBUG=0 && gmake && gmake install
RUN chown -R root:mysql /usr/local/mysql/ && chown -R mysql:mysql /data/mysql
RUN chmod 755 /usr/local/src/mysql-5.6.29/scripts/mysql_install_db.sh
RUN /usr/local/src/mysql-5.6.29/scripts/mysql_install_db.sh --basedir=/usr/local/mysql --datadir=/data/mysql --no-defaults --user=mysql
RUN cp /usr/local/src/mysql-5.6.29/support-files/my-default.cnf  /etc/my.cnf
RUN cp /usr/local/src/mysql-5.6.29/support-files/mysql.server  /etc/init.d/mysqld
RUN chmod 775 /etc/init.d/mysqld && /etc/init.d/mysqld start
RUN echo -e '#!/bin/bash\\nexport PATH=$PATH:/usr/local/mysql/bin' >/etc/profile.d/mysql.sh
RUN source /etc/profile
EXPOSE 3306
[root@docker mysql]# docker build -t "centos:mysql-5.6" .   //构建镜像
Successfully built e18ad22fb1cf
Successfully tagged centos:mysql-5.6
[root@docker mysql]# docker run -dit -p 3306:3306 --device-write-bps /dev/sda:10M e18ad22fb1cf /bin/bash                                    //限制Mysql容器写 /dev/sda 的速率为 10 MB/s
178a24f5758825fa5ec538a55706640de7d08af23a5b16cd2e85b1238ccacd31
[root@docker mysql]# docker ps                           //列出当前容器
CONTAINER ID   IMAGE          COMMAND       CREATED              STATUS              PORTS                                         NAMES
178a24f57588   e18ad22fb1cf   "/bin/bash"   About a minute ago   Up About a minute   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp     determined_lamport
cebe2bd3cbd0   07a4acbb016b   "/bin/bash"   39 minutes ago       Up 39 minutes       0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   dazzling_buck
[root@docker mysql]# docker exec -it 178a24f57588 /bin/bash  //进入容器
[root@178a24f57588 mysql-5.6.29]# /etc/init.d/mysqld start   //启动mysql
Starting MySQL............ SUCCESS! 

②修改密码、进入数据库、授权
修改密码,第一行默认密码(无)第二、第三行新密码
mysqladmin -u root -p password
(第一行直接敲回车)
123456
123456

授权

Confirm new password: 
[root@178a24f57588 mysql-5.6.29]# mysql -uroot -p123456        //进入数据库
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \\g.
Your MySQL connection id is 2
Server version: 5.6.29 Source distribution

Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\\h' for help. Type '\\c' to clear the current input statement.

mysql> create database wordpress default charset utf8 COLLATE utf8_general_ci;  //创建wordpress表
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on wordpress.*  to 'wordpress'@'%' identified by '123456' with grant option;                                                            //授权
Query OK, 0 rows affected (0.00 sec)

mysql> flush privileges;                                                     //刷新权限生效
Query OK, 0 rows affected (0.00 sec)                                         

mysql> exit                                                                  //退出数据库
Bye
[root@178a24f57588 mysql-5.6.29]# exit                                       //退出容器
exit
[root@docker_lnmp wp-admin]# docker inspect --format='{{.NetworkSettings.IPAddress}}' 178a24f57588
172.17.0.3
###可查看容器内应用的ip,如果出现数据库连接错误,进去Nginx容器修改/usr/local/nginx/html/wordpress/wp-config.php中的IP地址
#[root@docker_lnmp wp-admin]# docker exec -it cebe2bd3cbd0 /bin/bash 
#[root@cebe2bd3cbd0 /]# vi /usr/local/nginx/html/wordpress/wp-config.php

2.6 测试访问网页wordpress

http://192.168.80.4/wordpress/index.php

三.备份恢复

①将所有容器快照

[root@docker_lnmp nginx]# docker images           //查看镜像
REPOSITORY   TAG         IMAGE ID       CREATED        SIZE
centos       mysql-5.6   e18ad22fb1cf   2 hours ago    5.24GB
centos       nginx-php   07a4acbb016b   3 hours ago    1.52GB
centos       7           8652b9f0cb4c   9 months ago   204MB
[root@docker_lnmp nginx]# docker ps             //查看容器
CONTAINER ID   IMAGE          COMMAND       CREATED       STATUS       PORTS                                         NAMES
178a24f57588   e18ad22fb1cf   "/bin/bash"   2 hours ago   Up 2 hours   0.0.0.0:3306->3306/tcp, :::3306->3306/tcp     determined_lamport
cebe2bd3cbd0   07a4acbb016b   "/bin/bash"   3 hours ago   Up 3 hours   0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   dazzling_buck



②将镜像保存成tar包


③删除容器和镜像


④恢复

以上是关于Docker项目部署lnmp+wordpress的主要内容,如果未能解决你的问题,请参考以下文章

Dockerfile构建LNMP分离环境部署wordpress

docker部署lnmp-wordpress

Dockerfile构建LNMP分离环境部署wordpress

如何在lnmp上部署Thinkphp

LNMP架构环境之PHP+Mariadb环境项目:部署博客wordpress项目

使用docker lnmp镜像包部署搭建laravel项目