Docker创建 LNMP 服务+Wordpress 网站平台

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地址主要软件
nginxCentOS 7172.20.0.10Docker-Nginx
mysqlCentOS 7172.20.0.20Docker-Mysql
phpCentOS 7172.20.0.30Docker-php

3.任务需求

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

二.Linux 系统基础镜像

规划三台操作系统的内存及大小4+4G

提前关闭防火墙

#关闭防火墙
systemctl stop firewalld.service
setenforce 0

提前安装好Docker容器,具体安装步骤可以查看以下:

https://blog.csdn.net/Katie_ff/article/details/132304161?spm=1001.2014.3001.5501

下载完之后提前下载centos7的镜像

docker pull centos:7

三.docker构建Nginx

1.建立工作目录上传安装包

mkdir /opt/nginx
cd /opt/nginx
#上传 nginx 安装包 nginx-1.12.0.tar.gz和wordpress 服务包 wordpress-4.9.4-zh_CN.tar.gz

ls
nginx-1.12.0.tar.gz  wordpress-4.9.4-zh_CN.tar.gz

2.编写 Dockerfile 脚本

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 配置文件

vim 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;
        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;
            #此地址是php端地址
            fastcgi_pass   172.20.0.30:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
egrep -v "^(.)*#(.)*$" nginx.conf | grep -v "^$"

4.生成镜像

docker build -t nginx:lnmp .
docker images

在这里插入图片描述

在这里插入图片描述

5.创建自定义网络

docker network create --subnet=172.20.0.0/16 --opt "com.docker.network.bridge.name"="docker1" mynetwork

docker network ls
ifconfig docker1

在这里插入图片描述

在这里插入图片描述

6.启动镜像容器

docker run -d --name nginx -p 80:80 -m 500m --memory-swap 1g --net mynetwork --ip 172.20.0.10 nginx:lnmp
docker ps

在这里插入图片描述

docker inspect nginx

在这里插入图片描述

7.验证 nginx

#进入容器验证
[root@nginx nginx]# curl http://192.168.198.11:80

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

四.docker构建Mysql

1. 建立工作目录

mkdir /opt/mysql
cd /opt/mysql/
#传入mysql安装包mysql-boost-5.7.20.tar.gz

2. 编写 Dockerfile

vim Dockerfile 

FROM centos:7
MAINTAINER this is mysql image <lnmp>
RUN yum -y install ncurses ncurses-devel bison cmake pcre-devel zlib-devel gcc gcc-c++ make;useradd -M -s /sbin/nologin mysql
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_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=boost \
-DWITH_SYSTEMD=1;make -j4;make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/;chown mysql:mysql /etc/my.cnf
WORKDIR /usr/local/mysql/bin/
RUN ./mysqld \
--initialize-insecure \
--user=mysql \
--basedir=/usr/local/mysql \
--datadir=/usr/local/mysql/data;cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/;systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]

3. 准备 my.cnf 文件

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. 生成镜像

docker build -t mysql:lnmp .
docker images

在这里插入图片描述

在这里插入图片描述

5. 启动镜像容器

 docker run --name=mysql -d --privileged --device-write-bps /dev/sda:10M -v /usr/local/mysql --net mynetwork --ip 172.20.0.20 mysql:lnmp
docker ps -a

在这里插入图片描述

6. 验证 mysql

docker exec -it mysql bash
systemctl status mysqld

在这里插入图片描述

五、docker构建Php

1. 建立工作目录

mkdir /opt/php
cd /opt/php/
#传入php安装包php-7.1.10.tar.bz2

2. 编写 Dockerfile 脚本

vim Dockerfile 

FROM centos:7
MAINTAINER this is php image <lnmp>
RUN yum install -y 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;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 -j 4 ; 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
CMD /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 位置

vim php.ini
#939行,取消注释,修改
date.timezone = Asia/Shanghai
#1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
egrep -v "^;" php.ini | egrep -v "^$"

3.2 php-fpm.conf

该配置文件位于 /usr/local/php/etc/php-fpm.conf.default

vim php-fpm.conf
#17行,删除注释符号“;”
pid = run/php-fpm.pid
egrep -v "^;" php-fpm.conf | egrep -v "^$"

3.3 www.conf

该配置文件位于 cd /usr/local/php/etc/php-fpm.d/

vim www.conf

user = nginx
group = nginx
#36行,修改监听IP和端口为容器IP:9000端口
listen = 172.20.0.30:9000
egrep -v "^;" www.conf | egrep -v "^$"

[www]
user = nginx
group = nginx
listen = 172.20.0.30:9000
listen.allowed_clients = 127.0.0.1,172.20.0.10
pm = dynamic
pm.max_children = 5
pm.start_servers = 2
pm.min_spare_servers = 1
pm.max_spare_servers = 3

4. 生成镜像

docker build -t php:lnmp .

在这里插入图片描述

docker images

在这里插入图片描述

5. 启动镜像容器

docker run -itd --name php --net mynetwork --ip 172.20.0.30 -p 9000:9000 --volumes-from nginx --volumes-from mysql php:lnmp
docker ps

在这里插入图片描述

6. 验证 php

docker exec -it php bash
ps -aux

在这里插入图片描述

六、启动 wordpress 服务

1. mysql 授权

#进入 mysql 容器
docker exec -it mysql bash
mysql -u root -p
Enter password: 
#初始密码为空直接回车
mysql> create database wordpress;
Query OK, 1 row affected (0.00 sec)
 
mysql> grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> grant all privileges on *.* to 'root'@'%' identified by '123456';
Query OK, 0 rows affected, 1 warning (0.00 sec)
 
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)

在这里插入图片描述

2.浏览器访问测试

访问 http://192.168.198.11/wordpress/index.php

dpress;
Query OK, 1 row affected (0.00 sec)

mysql> grant all privileges on wordpress.* to ‘wordpress’@‘%’ identified by ‘123456’;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> grant all privileges on . to ‘root’@‘%’ identified by ‘123456’;
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)


[外链图片转存中...(img-wKachR0L-1692686561181)]

## 2.浏览器访问测试 

访问 http://192.168.198.11/wordpress/index.php

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值