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 | 172.20.0.10 | Docker-Nginx |
| mysql | CentOS 7 | 172.20.0.10 | Docker-Mysql |
| php | CentOS 7 | 172.20.0.10 | Docker-php |
3 任务需求
① 使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台
② 限制 Nginx 容器最多使用 500MB 的内存和 1G 的 Swap
③ 限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s
④将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地
二 Linux 系统基础镜像
三 Nginx
1 建立工作目录
[root@mcb-11 nginx]# mkdir /opt/lnmp
[root@mcb-11 lnmp]# mkdir nginx
[root@mcb-11 lnmp]# cd nginx/
[root@mcb-11 nginx]# rz -E
rz waiting to receive.
[root@mcb-11 nginx]# rz -E
rz waiting to receive.
[root@mcb-11 nginx]# ls
Dockerfile
nginx-1.12.0.tar.gz
nginx.conf
wordpress-4.9.4-zh_CN.tar.gz
[root@mcb-11 nginx]# mkdir html
[root@mcb-11 nginx]# cd html
[root@mcb-11 html]# pwd
/opt/lnmp/nginx/html
[root@mcb-11 html]# tar zxvf ../wordpress-4.9.4-zh_CN.tar.gz -C /opt/lnmp/nginx/
[root@mcb-11 html]# tar zxvf ../wordpress-4.9.4-zh_CN.tar.gz -C /opt/lnmp/nginx/html
[root@mcb-11 html]# ls
wordpress
[root@mcb-11 html]# cd wordpress/
[root@mcb-11 wordpress]# ls
index.php wp-admin wp-content wp-load.php wp-signup.php
license.txt wp-blog-header.php wp-cron.php wp-login.php wp-trackback.php
readme.html wp-comments-post.php wp-includes wp-mail.php xmlrpc.php
wp-activate.php wp-config-sample.php wp-links-opml.php wp-settings.php
[root@mcb-11 wordpress]# cd ..
[root@mcb-11 nginx]# docker build -t nginx:lnmp .
=> => naming to docker.io/library/nginx:lnmp
[root@mcb-11 nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx lnmp 2451d2dc694b 4 minutes ago 569MB
[root@mcb-11 nginx]# docker run -d --name nginx -p 80:80 -v /opt/lnmp/nginx/html:/usr/local/nginx/html --network mynetwork --ip 172.18.0.10 nginx:lnmp
c6ce7c1695075704354d6299a3a4c09ce8948f3632aacbcf91f829a4a886ba59
[root@mcb-11 nginx]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
c6ce7c169507 nginx:lnmp "/usr/local/nginx/sb..." 6 seconds ago Up 5 seconds 0.0.0.0:80->80/tcp, :::80->80/tcp, 443/tcp nginx
[root@mcb-11 nginx]# docker exec -it nginx /bin/bash
[root@c6ce7c169507 nginx-1.12.0]# ls
CHANGES LICENSE README conf contrib man src
CHANGES.ru Makefile auto configure html objs
[root@c6ce7c169507 nginx-1.12.0]# pwd
/usr/local/src/nginx-1.12.0
[root@c6ce7c169507 local]# ls
bin etc games include lib lib64 libexec nginx sbin share src
[root@c6ce7c169507 local]# cd nginx/
[root@c6ce7c169507 nginx]# ls
client_body_temp conf fastcgi_temp html logs proxy_temp sbin scgi_temp uwsgi_temp
[root@c6ce7c169507 nginx]# cd html/
[root@c6ce7c169507 html]# ls
wordpress
2 编写 Dockerfile 脚本
FROM centos:7
MAINTAINER this is nginx image <hmj>
RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make
RUN 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 -j4 && 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
EXPOSE 443
ENTRYPOINT [ "/usr/local/nginx/sbin/nginx", "-g", "daemon off;" ]
3 准备 nginx.conf 配置文件
4 生成镜像
5 创建自定义网络
6 启动镜像容器
docker run -d --name nginx -p 80:80 -v /opt/lnmp/nginx/html:/usr/local/nginx/html --network mynetwork --ip 172.18.0.10 nginx:lnmp
7 验证 nginx
四 Mysql
1 建立工作目录
2 编写 Dockerfile
[root@docker mysql]# vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <hmj>
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make
ADD mysql-boost-5.7.20.tar.gz /usr/local/src/
WORKDIR /usr/local/src/mysql-5.7.20/
RUN cmake \
-DCMAKE_INSTALL_PREFIX=/usr/local/mysql \
-DMYSQL_UNIX_ADDR=/usr/local/mysql/mysql.sock \
-DSYSCONFDIR=/etc \
-DSYSTEMD_PID_DIR=/usr/local/mysql \
-DDEFAULT_CHARSET=utf8 \
-DDEFAULT_COLLATION=utf8_general_ci \
-DWITH_INNOBASE_STORAGE_ENGINE=1 \
-DWITH_ARCHIVE_STORAGE_ENGINE=1 \
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1 && make -j4&& make install
ADD my.cnf /etc/
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql
RUN chown mysql:mysql /etc/my.cnf
RUN chown -R mysql:mysql /usr/local/mysql/
RUN /usr/local/mysql/bin/mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
EXPOSE 3306
CMD ["/usr/local/mysql/bin/mysqld"]
3 准备 my.cnf 文件
[root@docker mysql]# vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysqld]
user = mysql
basedir=/usr/local/mysql
datadir=/usr/local/mysql/data
port = 3306
character-set-server=utf8
pid-file = /usr/local/mysql/mysqld.pid
socket=/usr/local/mysql/mysql.sock
bind-address = 0.0.0.0
skip-name-resolve
max_connections=2048
default-storage-engine=INNODB
max_allowed_packet=16M
server-id = 1
sql_mode=NO_ENGINE_SUBSTITUTION,STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_AUTO_VALUE_ON_ZERO,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,PIPES_AS_CONCAT,ANSI_QUOTES
4 生成镜像
[root@docker mysql]# docker build -t mysql:lnmp .
[root@docker mysql]# docker images
5 启动镜像容器
docker run --name mysql -d --privileged --network mynetwork --ip 172.18.0.13 mysql:lnmp
6 验证 mysql
五 Php
1 建立工作目录
2 编写 Dockerfile 脚本
FROM centos:7
MAINTAINER this is php image <hmj>
RUN yum -y install gd \
libjpeg libjpeg-devel \
libpng libpng-devel \
freetype freetype-devel \
libxml2 libxml2-devel \
zlib zlib-devel \
curl curl-devel \
openssl openssl-devel \
gcc gcc-c++ make pcre-devel
RUN useradd -M -s /sbin/nologin nginx
ADD php-7.1.10.tar.bz2 /usr/local/src/
WORKDIR /usr/local/src/php-7.1.10
RUN ./configure \
--prefix=/usr/local/php \
--with-mysql-sock=/usr/local/mysql/mysql.sock \
--with-mysqli \
--with-zlib \
--with-curl \
--with-gd \
--with-jpeg-dir \
--with-png-dir \
--with-freetype-dir \
--with-openssl \
--enable-fpm \
--enable-mbstring \
--enable-xml \
--enable-session \
--enable-ftp \
--enable-pdo \
--enable-tokenizer \
--enable-zip && make && make install
ENV PATH /usr/local/php/bin:/usr/local/php/sbin:$PATH
ADD php.ini /usr/local/php/lib/
ADD php-fpm.conf /usr/local/php/etc/
ADD www.conf /usr/local/php/etc/php-fpm.d/
EXPOSE 9000
ENTRYPOINT [ "/usr/local/php/sbin/php-fpm", "-F" ]
3 准备 php.ini、php-fpm.conf、www.conf 配置文件
3.1 php.ini
该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置
3.2 php-fpm.conf
该配置文件位于 /usr/local/php/etc/php-fpm.conf.default
[root@docker php]# vim php-fpm.conf
#17行,删除注释符号";"
pid = run/php-fpm.pid
[root@docker php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
3.3 www.conf
该配置文件位于 usr/local/php/etc/php-fpm.d/www.conf.default
[root@docker php]# vim www.conf
#23、24行,修改用户和组
user = nginx
group = nginx
#36行,修改监听IP和端口为容器IP:9000端口
listen = 172.20.0.30:9000
[root@docker php]# egrep -v "^;" www.conf | egrep -v "^$"
4 生成镜像
[root@mcb-11 php]# docker build -t php:lnmp .
[root@mcb-11 php]# docker images
5 启动镜像容器
6 验证 php
六 启动 wordpress 服务
1 mysql 授权
进入 mysql 容器
[root@433a9d5652b4 mysql-5.7.20]# mysql -u root -p
Enter password: #回车
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5
Server version: 5.7.20 Source distribution
Copyright (c) 2000, 2017, 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;
Query OK, 1 row affected (0.00 sec)
mysql> show databashs;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databashs' at line 1
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
| wordpress |
+--------------------+
5 rows in set (0.00 sec)
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> grant all privileges on *.* to 'root'@'%' identified by '123';
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
创建一个新的配置文件 wp-config.php
[root@433a9d5652b4 mysql-5.7.20]# exit
exit
[root@mcb-11 lnmp]# cd nginx/html/wordpress/
[root@mcb-11 wordpress]# cp wp-config-sample.php wp-config.php
[root@mcb-11 wordpress]# vim wp-co
wp-comments-post.php wp-config.php wp-config-sample.php wp-content/
[root@mcb-11 wordpress]# vim wp-config.php
define('DB_NAME', 'wordpress');
define('DB_USER', 'wordpress');
define('DB_PASSWORD', '123456');
# 修改WordPress数据库信息
2. 浏览器访问测试