目录



一、docker部署lnmp

1.1、项目环境

(1)公司在实际的生产环境中,需要使用Docker 技术在一台主机上创建LNMP服务并运行Wordpress网站平台。然后对此服务进行相关的性能调优和管理工作。
(2)使用Docker构建LNMP环境并运行Wordpress网站平台。
(3)限制Nginx容器最多使用500Mb的内存和1G的Swap。
(4)限制Mysql容器写 /dev/sda:10M 的速率为 10 MB/s。
(5)将所有容器进行快照,然后将Docker镜像打包成tar包备份到本地。

主机:192.168.80.12
docker容器1:172.80.0.10/16 nginx
docker容器2:172.80.0.11/16 mysql
docker容器3:172.80.0.12/16 php

systemctl stop firewalld
setenforce 0

docker network create --subnet=172.80.0.0/16 --opt "com.docker.network.bridge.name"="docker1" newnetwork #自定义网络


1.2、部署nginx(容器IP为:172.80.0.10)

mkdir /opt/nginx/
cd /opt/nginx/ #工作目录

上传 nginx-1.12.0.tar.gz、wordpress-4.9.4-zn_CN.tar.gz 到 /opt/nginx/ 目录中

mkdir /opt/nginx/html
tar zxvf wordpress-4.9.4-zn_CN.tar.gz -C /opt/nginx/html

vim Dockerfile

FROM centos:7
MAINTAINER this is nginx image <LK>
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 -j 4 && make install
ENV PATH $PATH:/usr/local/nginx/sbin
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress-4.9.4-zn_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;"]

docker build -t nginx:lnmp
docker run -d --name nginx -p 80:80 -v /opt/nginx/html:/usr/local/nginx/html --net newnetwork --ip 172.80.0.10 nginx:lnmp


docker部署lnmp_html

docker部署lnmp_docker_02

docker部署lnmp_mysql_03

1.3、部署mysql(容器IP为172.80.0.11)

mkdir /opt/mysqld
cd /opt/mysqld

上传 mysql-5.7.17.tar.gz boost_1_59_0.tar.gz到 /opt/mysqld 目录中

vim Dockerfile
FROM centos:7
MAINTAINER this is mysql image <LK>
RUN yum -y install gcc gcc-c++ ncurses-devel bison cmake make
ADD mysql-5.7.17.tar.gz /opt/
ADD boost_1_59_0.tar.gz /usr/local/boost/
WORKDIR /opt/mysql-5.7.17/
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_EXTRA_CHARSETS=all \
-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=/usr/local/boost \
-DWITH_SYSTEMD=1
RUN make -j 4 && make install
RUN useradd -M -s /sbin/nologin mysql
ADD my.cnf /etc/
RUN chown -R mysql:mysql /usr/local/mysql/
RUN chown mysql:mysql /etc/my.cnf
ENV PATH /usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data
RUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
EXPOSE 3306
RUN systemctl enable mysqld
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]

vim /etc/mysqld/my.cnf
[client]
port = 3306
socket = /usr/local/mysql/mysql.sock

[mysql]
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
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

docker run --name=mysql -d --privileged -v /usr/local/mysql --net mynetwork --ip 172.80.0.11 mysql:lnmp


docker部署lnmp_nginx_04

docker部署lnmp_html_05

1.4、部署php(容器IP 为 172.18.0.12)

mkdir /opt/php
cd /opt/php

上传 php-7.1.10.tar.bz2 到 /opt/php 目录中

vim Dockerfile
FROM centos:7
MAINTAINER this is mysql php <LK>
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 -j4 && 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" ]

docker build -t php:lnmp .
docker run --name=php -d -p 9000:9000 --volumes-from mysql_server --volumes-from nginx --net mynetwork --ip 172.18.0.12 php:lnmp


docker部署lnmp_php_06

docker部署lnmp_docker_07

#进入数据库容器授权
mysql
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456'
grant all privileges on *.* to 'root'@'%' identified by 'abc123';
flush privileges;

#浏览器访问
http://192.168.80.15/wordpress/index.php


docker部署lnmp_nginx_08

1.5、注意事项

nginx配置文件

nginx.conf
配置 Nginx 支持 PHP 解析


docker部署lnmp_docker_09

php配置文件

php.ini
调整主配置文件


docker部署lnmp_html_10

docker部署lnmp_nginx_11

php-fpm.conf
调整进程服务配置文件


docker部署lnmp_docker_12

www.conf
调整扩展配置文件


docker部署lnmp_html_13