首页 > 其他分享 >Docker 搭建 LNMP 架构的 Wordpress网站

Docker 搭建 LNMP 架构的 Wordpress网站

时间:2023-08-20 22:44:47浏览次数:53  
标签:-- LNMP nginx Wordpress usr mysql Docker php local

目录

一、项目环境

公司在实际的生产环境中,需要使用 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/

二、服务器环境

容器 操作系统 IP地址 主要软件
nginx Centos 7 172.20.0.10 Docker-Nginx
mysql Centos 7 172.20.0.20 Docker-Mysql
php Centos 7 172.20.0.30 Docker-php

三、任务需求

使用 Docker 构建 LNMP 环境并运行 Wordpress 网站平台

限制 Nginx 容器最多使用 512MB 的内存和 1G 的 Swap

限制 Mysql 容器写 /dev/sda 的速率为 10 MB/s

将所有容器进行快照,然后将 Docker 镜像打包成 tar 包备份到本地

四、获取 Linux 系统基础镜像

部署 docker

获取Linux基础镜像

docker pull centos:7
docker images

image-20230820152230702

五、Nginx

1. 建立工作目录

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

image-20230820153128746

2. 编写 Dockerfile 脚本

vim Dockerfile

FROM centos:7                           # 使用CentOS 7作为基础镜像
MAINTAINER this is nginx image <lnmp>    # 维护者信息

RUN yum -y install pcre-devel zlib-devel gcc gcc-c++ make     # 安装编译Nginx所需的依赖包
RUN useradd -M -s /sbin/nologin nginx     # 创建一个名为nginx的系统用户,用于运行Nginx

ADD nginx-1.12.0.tar.gz /usr/local/src/   # 将本地的nginx源码包复制到容器内的/usr/local/src/目录下
WORKDIR /usr/local/src/nginx-1.12.0      # 设置工作目录为nginx源码路径

RUN ./configure \                        # 配置Nginx编译选项
--prefix=/usr/local/nginx \               # 指定安装路径
--user=nginx \                            # 指定运行Nginx的用户
--group=nginx \                           # 指定运行Nginx的用户组
--with-http_stub_status_module            # 启用HTTP stub status模块

RUN make -j 4 && make install             # 编译并安装Nginx
ENV PATH /usr/local/nginx/sbin:$PATH      # 将Nginx可执行文件添加到环境变量中

ADD nginx.conf /usr/local/nginx/conf/     # 将本地的nginx配置文件复制到容器内的/usr/local/nginx/conf/目录下
ADD wordpress-4.9.4-zh_CN.tar.gz /usr/local/nginx/html   # 将本地的wordpress应用复制到容器内的/usr/local/nginx/html/目录下

RUN chmod 777 -R /usr/local/nginx/html/   # 设置/usr/local/nginx/html/目录的权限为777,使其对所有用户可写

EXPOSE 80                                # 暴露容器的80端口,供外部访问Nginx
VOLUME [ "/usr/local/nginx/html/" ]      # 创建一个卷,挂载到容器的/usr/local/nginx/html/目录上

CMD [ "/usr/local/nginx/sbin/nginx","-g","daemon off;" ]   # 容器启动后执行的命令,以非守护进程方式运行Nginx

image-20230820161528673

3. 编辑 nginx 的主配置文件

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;
            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;
        }
    }
}


image-20230820164039944

4. 生成镜像

docker build -t nginx:lnmp .
docker images

image-20230820165326876

5. 创建自定义网络

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

image-20230820170443299

6. 启动镜像容器

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

image-20230820171918806

image-20230820172046464

7. 验证 nginx

curl http://192.168.23.40:80

image-20230820172319961

六、MySQL

1. 建立工作目录

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

image-20230820182130582

2. 编写 Dockerfile

vim Dockerfile

mkdir /opt/mysql
FROM centos:7
MAINTAINER this is mysql image <lnmp>
RUN 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

RUN make -j 4 && make install
-DWITH_BLACKHOLE_STORAGE_ENGINE=1 \
-DWITH_PERFSCHEMA_STORAGE_ENGINE=1 \
-DMYSQL_DATADIR=/usr/local/mysql/data \
-DWITH_BOOST=boost \
-DWITH_SYSTEMD=1

RUN make -j 4 && make install
ADD my.cnf /etc/my.cnf
EXPOSE 3306
RUN chown -R mysql:mysql /usr/local/mysql/
RUN 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/dataRUN cp /usr/local/mysql/usr/lib/systemd/system/mysqld.service /usr/lib/systemd/system/
RUN systemctl enable mysqld
ENV PATH=/usr/local/mysql/bin:/usr/local/mysql/lib:$PATH
VOLUME [ "/usr/local/mysql" ]
CMD ["/usr/sbin/init"]



image-20230820180408418

3. 编辑 mysql 主配置文件

准备 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

image-20230820180752313

4. 生成镜像

docker build -t mysql:lnmp
docker images

image-20230820184051433

image-20230820184137215

5. 启动镜像容器

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

docker ps

image-20230820190030542

6. 验证 mysql

docker exec -it mysql bash
systemctl status mysqld

image-20230820190329918

七、PHP

1. 建立工作目录

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

image-20230820191010116

2. 编写 Dockerfile 脚本

vim Dockerfile

FROM centos:7
MAINTAINER this is php image <lnmp>
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

RUN 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


image-20230820192510020

3. 准备配置文件

准备 php.ini、php-fpm.conf、www.conf 配置文件

(1)php.ini

该配置文件模板位于安装目录的 php-7.1.10/php.ini-development 位置

# 上传 php.ini 模板
rz -E

vim php.ini
# 939 行,取消注释,修改
date.timezone = Asia/Shanghai
# 1170行,修改
mysqli.default_socket = /usr/local/mysql/mysql.sock

egrep -v "^;" php.ini | grep -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]

image-20230820193957158

image-20230820194051299

(2)php-fpm.conf

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

# 上传 php-fpm.conf 文件模板
rz -E

vim php-fpm.conf
# 17行,删除注释符号 “;”
php = run/php-fpm.pid

egrep -v "^;" php-fpm.conf | egrep -v "^$"
[global]
pid = run/php-fpm.pid
include=/usr/local/php/etc/php-fpm.d/*.conf

image-20230820195243529

image-20230820195348035

image-20230820195505124

(3)www.conf

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

# 上传 www.conf 模板
rz -E

vim www.conf
# 23、24行,修改用户和组
user = nginx
group = nginx
# 36行,修改监听IP和端口为容器IP:9000端口
list = 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

image-20230820200030793

image-20230820200108291

image-20230820200305457

image-20230820200426588

4. 生成镜像

docker build -t php:lnmp .
docker images

image-20230820204529971

5. 启动镜像容器

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

image-20230820204713417

6. 验证 php

docker exec -it php bash
ps -aux

image-20230820205018797

八、启动 wordpress 服务

1. mysql 授权

进入 mysql 容器

docker exec -it mysql

mysql -u root -p
Enter password:
# 初始密码为空
create database wordpress;
grant all privileges on wordpress.* to 'wordpress'@'%' identified by '123456';
grant all privileges on *.* to 'root'@'%' identified by '123456';
flush privileges;
\q

image-20230820210401440

2. 浏览器访问测试

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

image-20230820211440890

image-20230820221849437

image-20230820222131908

image-20230820222202667

image-20230820222226217

image-20230820222256364

标签:--,LNMP,nginx,Wordpress,usr,mysql,Docker,php,local
From: https://www.cnblogs.com/m-zhuang/p/17644779.html

相关文章

  • Dockerfile创建镜像
    Dockerfile是用于定义和构建Docker镜像的重要工具,它使得镜像的构建过程更加可控、可重复和可自动化,为应用程序的打包、分发和部署提供了便利性和灵活性。目录一、创建镜像方法二、Dockerfile概述三、Dockerfile操作指令 四、Dockerfile案例五、总结  一、创建镜像方......
  • WordPress、Drupal和Joomla!等PHP系统的对比分析 - 开源CMS比较
    选择一个适合自己网站的内容管理系统(CMS)是非常重要的。在众多的选择中,我们经常听到的有WordPress、Drupal和Joomla!等PHP系统。本文将对这三个开源CMS进行详细的比较和分析,帮助您做出最佳选择。1.WordPressWordPress是目前最受欢迎的开源CMS之一,它简单易用且拥有强大的生态系统......
  • docker——创建一个harbor仓库
    1.什么是harbor就像docker官方一样,它有一个hub.docker.com的官方仓库,给我们提供很多实用的镜像文件,使得我们直接使用dockerpull将需要的镜像拉取下来就可以运行使用了。同样的,我们用户自己也可以创建一个镜像仓库,把我们自己创建的镜像文件推送的私有的harbor仓库里面去,也可以拉取......
  • 如何解决docker 一直转圈圈而不start
    今天在进行启动docker的时候出现了一直转圈圈而不能启动的问题我的电脑是windows10,解决步骤是:首先右击右下角任务栏中的小图标docker,有个Switchtowindowscontainers选择后,打开WindowsPowerShell以管理员的身份运行然后输入Enable-WindowsOptionalFeature-Online-Feat......
  • docker网络
    一、docker网络1、docker网络默认启动的时候,会创建三个网络dockernetworkls2、dockernetworkinspectbridge查看网络 网络的详细信息,包括网络的名称、驱动程序、子网、网关、连接的容器等3、dockernetworkcreatedemo创建网络报错解决4、dockernetworkrmdemo删除网络二......
  • 【Docker实战】使用Docker部署Tomcat
    (【Docker实战】使用Docker部署Tomcat)一、Tomcat介绍1.Tomcat简介Tomcat服务器是一个免费的开放源代码的Web应用服务器,属于轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。2.Tomcat特点Tomcat是一种开源的、轻量......
  • 桌面版vscode用免费的微软4核8G服务器做远程开发(编译运行都在云上,还能自由创建docker
    欢迎访问我的GitHub这里分类和汇总了欣宸的全部原创(含配套源码):https://github.com/zq2599/blog_demos本篇概览在前文《浏览器上写代码,4核8G微软服务器免费用,Codespaces真香》中,咱们以一个Java程序员视角体验了GitHub的Codespaces产品的beta版,简单来说有以下两大收获:网......
  • Docker容器操作
    1dockerexec这个命令dockerexec是对运行状态的容器进行执行一个命令,exec就是execute的简写,单词就是执行的意思。例如我们基本上容器都是说linux环境下的容器,在linux下我们经常做的事情就是要执行各种shell命令,所以这个命令用到最多的场景是执行一个容器下的bash程序,然后输入一个......
  • Docker rootless部署应用
    一、创建普通用户并赋予sudo权限免密切换rootuseraddairechomcwlWS93Kjn0BCL9|passwdair--stdincd/etcchmod600sudoersvisudoers ###################################################################################Samethingwithoutapassword#%whee......
  • 解决虚拟上docker 启动ES访问不成功
    可能原因是ES太耗内存了,我发现启动了ES容器后,像dockerps这种命令都很慢而且查看日志发现进行垃圾回收出现错误。所以判断可能是因为ES太耗内存。采用以下命令启动容器,应该可以解决。dockerrun-id--nameelasticsearch-d--restart=always-p9200:9200-p9300:9300-v/usr......