@[TOC](Docker 搭建 LNMP + Wordpress
- 一、项目介绍
- 1.1、项目环境
- 1.2、 服务器环境
- 1.3、 任务需求
- 二、部署Nginx
- 2.1、建立工作目录
- 2.2、 编写 Dockerfile 脚本
- 2.3、准备 nginx.conf 配置文件
- 2.4、生成镜像
- 2.5、创建自定义网络
- 三、部署Mysql
- 3.1、建立工作目录
- 3.2、编写 Dockerfile
- 3.3、准备 my.cnf 文件
- 3.4、创建镜像
- 3.5、 使用新的镜像运行容器并测试
- 四、部署PHP
- 4.1、创建工作目录并上传代码包
- 4.2、 编写 Dockerfile 脚本
- 4.3、准备 php.ini、php-fpm.conf、www.conf 配置文件
- 4.3.1、 php.ini
- 4.3.2、 php-fpm.conf
- 4.4、 生成镜像
- 4.5、运行容器
- 4.6、数据库授权
- 4.7、运行Wordpress
一、项目介绍
1.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/
1.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-Mysql |
php | Centos 7.9 x86_64 | 172.111.0.30 | Docker-php |
宿主机 | Centos 7.9 x86_64 | 192.168.11.11 | Docker 19.03 |
1.3、 任务需求
- 使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台。
- 限制 Nginx 容器最多使用 500MB 的内存和 1G 的 Swap。
- 限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s。
- 将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地。
[root ~]# systemctl disable --now firewalld
[root ~]# setenforce 0
setenforce: SELinux is disabled
[root ~]# docker pull centos:7
#从公有仓库中下载 centos7 作为系统基础镜像
[root ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
二、部署Nginx
2.1、建立工作目录
在docker容器中拉取centoos7作为基础镜像
docker pull centos:7
[root ~]$ mkdir /opt/nginx
[root ~]$ cd /opt/nginx/
[root /opt/nginx]$ ls
nginx-1.24.0.tar.gz
2.2、 编写 Dockerfile 脚本
[root /opt/nginx/html]$ touch Dockerfile
[root /opt/nginx/html]$ ls
Dockerfile wordpress
[root nginx]# vim Dockerfile
FROM centos:7 AS first
MAINTAINER this is nginx image <wl 20231021>
ADD nginx-1.24.0.tar.gz /opt/
RUN yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++ make && \
useradd -M -s /sbin/nologin nginx && \
cd /opt/nginx-1.24.0 && \
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module && \
make && \
make install
FROM centos:7
COPY --from=first /usr/local/nginx/ /usr/local/nginx/
RUN useradd -M -s /sbin/nologin nginx
EXPOSE 80
EXPOSE 443
CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
2.3、准备 nginx.conf 配置文件
vim nginx.conf
#user nobody;
worker_processes 2;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
use epoll
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.byyb.com;
charset utf-8;
#access_log logs/host.access.log main;
location / {
root html;
index index.php index.html;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ \.php$ {
root html;
fastcgi_pass 172.18.0.30:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
2.4、生成镜像
[root nginx]# docker build -t nginx:lnmp .
[root nginx]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
nginx lnmp 35a6404fcfa1 5 seconds ago 522MB
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
2.5、创建自定义网络
docker network create --subnet=172.18.0.0/16 --opt "com.docker.network.bridge.name"="docker1" lnmpnetwork
启动镜像容器
docker run -d --name nginx -p 80:80 -v /opt/nginx/html/:/usr/local/nginx/html/ -v /opt/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf --network lnmpnetwork --ip 172.18.0.10 nginx:lnmp
三、部署Mysql
3.1、建立工作目录
[root nginx]# mkdir /opt/mysql
[root nginx]# cd /opt/mysql/
[root mysql]# rz -E
rz waiting to receive.
#传入mysql安装包mysql-boost-5.7.41.tar.gz
3.2、编写 Dockerfile
[root mysql]# vim Dockerfile
FROM centos:7 AS first
MAINTAINER this is mysql image <wl 20231021>
ADD mysql-boost-5.7.41.tar.gz /opt/
RUN yum -y install gcc gcc-c++ ncurses ncurses-devel bison cmake make openssl-devel && \
cd /opt/mysql-5.7.41/ && \
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 -j 2 && make install
FROM centos:7
COPY --from=first /usr/local/mysql/ /usr/local/mysql/
ADD my.cnf /etc/my.cnf
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
RUN useradd -M -s /sbin/nologin mysql && \
chown mysql:mysql /etc/my.cnf && \
chown -R mysql:mysql /usr/local/mysql/ && \
/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.3、准备 my.cnf 文件
[root mysql]# vim my.cnf
[client]
port = 3306
socket=/usr/local/mysql/mysql.sock
[mysql]
port = 3306
socket = /usr/local/mysql/mysql.sock
auto-rehash
[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
3.4、创建镜像
docker build -t mysql:lnmp .
#名称:标签
3.5、 使用新的镜像运行容器并测试
docker run -d --name mysql --privileged -v /usr/local/mysql --network lnmpnetwork --ip 172.18.0.20 mysql:lnmp
- `--name=mysql`:指定容器的名称为mysql。
- `-d`:以后台模式运行容器。
- `--privileged`:在容器内部启用特权模式,可以执行一些底层操作。
- `-v /usr/local/mysql`:将宿主机的`/usr/local/mysql`目录挂载到容器的相应位置。
- `--net mynetwork`:将容器连接到名为mynetwork的网络。
四、部署PHP
4.1、创建工作目录并上传代码包
mkdir /opt/php
cd /opt/php
上传 php-7.1.10.tar.bz2 到 /opt/php 目录中
4.2、 编写 Dockerfile 脚本
vim Dockerfile
FROM centos:7 AS first
MAINTAINER this is php image <wl 20231021>
ADD php-7.1.10.tar.bz2 /opt/
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 && \
cd /opt/php-7.1.10/ && \
./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 2 && make install && \
cp /opt/php-7.1.10/php.ini-development /usr/local/php/lib/php.ini && \
sed -i -e '939c date.timezone = Asia/Shanghai' -e '1170c mysqli.default_socket = /usr/local/mysql/mysql.sock' /usr/local/php/lib/php.ini && \
cd /usr/local/php/etc/ && \
cp php-fpm.conf.default php-fpm.conf && \
sed -i '17 s/^;//' php-fpm.conf && \
cd /usr/local/php/etc/php-fpm.d/ && \
cp www.conf.default www.conf && \
sed -i -e '23c user = nginx' -e '24c group = nginx' -e '36c listen = 172.18.0.30:9000' -e '62c listen.allowed_clients = 172.18.0.10' www.conf
FROM centos:7
COPY --from=first /usr/local/php/ /usr/local/php/
RUN yum -y install gd \
libjpeg-devel \
libpng-devel \
freetype-devel \
libxml2-devel \
zlib-devel \
curl-devel \
openssl-devel && \
useradd -M -s /sbin/nologin nginx && \
rm -rf /var/cache/yum/*
EXPOSE 9000
CMD ["/usr/local/php/sbin/php-fpm", "-F"]
4.3、准备 php.ini、php-fpm.conf、www.conf 配置文件
4.3.1、 php.ini
该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置
[root php]# vim php.ini
#939行,取消注释,修改
date.timezone = Asia/Shanghai
#1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock
[root php]# egrep -v "^;" php.ini | egrep -v "^$"
[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL
display_errors = On
display_startup_errors = On
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
track_errors = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
date.timezone = Asia/Shanghai
[filter]
[iconv]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.cache_size = 2000
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = On
[SQL]
sql.safe_mode = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.cache_size = 2000
mysqli.default_port = 3306
mysqli.default_socket = /usr/local/mysql/mysql.sock
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = On
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = 1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[mcrypt]
[dba]
[opcache]
[curl]
[openssl]
4.3.2、 php-fpm.conf
该配置文件位于 /usr/local/php/etc/php-fpm.conf.default
[root php]# vim php-fpm.conf
#17行,删除注释符号“;”
pid = run/php-fpm.pid
[root php]# egrep -v "^;" php-fpm.conf | egrep -v "^$"
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf
4.4、 生成镜像
[root php]# docker build -t php:lnmp .
[root php]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
php lnmp 985842f961f1 About a minute ago 1.2GB
mysql lnmp 49da8f7299af 12 minutes ago 9.98GB
nginx lnmp 35a6404fcfa1 26 minutes ago 522MB
centos 7 eeb6ee3f44bd 4 weeks ago 204MB
4.5、运行容器
docker run -d --name=php -p 9000:9000 --volumes-from mysql --volumes-from nginx --network lnmpnetwork --ip 172.18.0.30 php:lnmp
4.6、数据库授权
docker exec -it mysql /bin/bash
mysql
create database wordpress;
grant all on wordpress.* to 'wordpress'@'172.18.0.%' identified by 'admin123';
grant all on wordptess.* to 'root'@'172.18.0.%' identified by 'admin123';
grant all on wordpress.* to 'wordpress'@'localhost' identified by 'admin123';
4.7、运行Wordpress
浏览器访问:
http://192.168.11.11/wordpress/index.php
v