Docker部署lnmp+wordpress

1. 环境

主机名         系统            IP
server       centos7         192.168.152.130

2.部署过程:

2.1 安装docker:

#关闭核心防护
hostnamectl set-hostname docker_lnmp
systemctl stop firewalld
systemctl disable firewalld
setenforce 0

#设置yum源,进行安装docker社区版,并且设置开机自启
yum install -y yum-utils device-mapper-persistent-data lvm2
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install -y docker-ce
systemctl start docker
systemctl enable docker

#创建dockerfile目录
mkdir docker && cd docker
yum install -y tree

2.2 部署LNMP:

在docker目录创建nginx和mysql目录,上传nginx-1.12.2.tar.gz、wordpress-4.9.4-zh_CN.tar、libmcrypt-2.5.7.tar.gz、nginx.conf、wp-config.php等包到两个目录下

2.2.1 先部署nginx dockerfile 文件

2.2.2 再部署的php dockerfile文件

#创建nginx-php、mysql目录
mkdir nginx mysql


#将文件分别拖进练个目录之下,并且进行解压
#解压软件包
[root@docker-lnmp nginx]# tar zxvf wordpress-4.9.4-zh_CN.tar.gz
[root@docker-lnmp nginx]# tar zxvf nginx-1.12.2.tar.gz
[root@docker-lnmp nginx]# tar zxvf libmcrypt-2.5.7.tar.gz
[root@docker-lnmp nginx]# tar zxvf php-5.5.38.tar.gz


[root@server docker]# tree -L 2
.
├── mysql
│?? ├── Dockerfile
│?? ├── mysql-5.7.20
│?? └── mysql-boost-5.7.20.tar.gz
└── nginx
    ├── Dockerfile
    ├── libmcrypt-2.5.7
    ├── libmcrypt-2.5.7.tar.gz
    ├── nginx-1.12.2
    ├── nginx-1.12.2.tar.gz
    ├── nginx.conf
    ├── php-5.5.38
    ├── php-5.5.38.tar.gz
    ├── wordpress
    ├── wordpress-4.9.4-zh_CN.tar.gz
    └── wp-config.php

7 directories, 9 files


#创建nginx-dockfile
[root@docker-lnmp nginx]# cat Dockerfile 
FROM docker.io/centos:7

#部署nginx
RUN yum -y update
RUN yum -y install gcc gcc-c++ openssl-devel openssl autoconf cmake autoconf zlib zlib-devel libtool pcre pcre-devel wget net-tools make
RUN groupadd  -g 900 nginx && useradd nginx -g nginx -s /sbin/nologin
ADD nginx-1.12.2 nginx-1.12.2
RUN cd /nginx-1.12.2/ && ./configure --prefix=/usr/local/nginx --with-http_dav_module  --with-http_stub_status_module --with-http_addition_module --with-http_sub_module --with-http_flv_module --with-http_mp4_module --with-http_ssl_module --with-http_gzip_static_module --user=nginx --group=nginx
RUN cd /nginx-1.12.2/ && make && make install
RUN ln -s /usr/local/nginx/sbin/nginx  /usr/local/sbin/
RUN sed -i '1afastcgi_param  SCRIPT_FILENAME    $document_root$fastcgi_script_name;' /usr/local/nginx/conf/fastcgi_params
ADD nginx.conf /usr/local/nginx/conf/
ADD wordpress /usr/local/nginx/html/wordpress
ADD wp-config.php /usr/local/nginx/html/wordpress

#部署php
RUN yum -y install gcc gcc-c++ libxml2-devel libcurl-devel openssl-devel bzip2-devel  openssl automake make autoconf libtool zlib-devel make pcre-devel wget net-tools
ADD libmcrypt-2.5.7 libmcrypt-2.5.7
RUN cd libmcrypt-2.5.7/&& ./configure --prefix=/usr/local/libmcrypt && make && make install
ADD php-5.5.38 php-5.5.38
RUN  cd php-5.5.38/ && ./configure --prefix=/usr/local/php5.5 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd --with-openssl --enable-fpm --enable-sockets --enable-sysvshm --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --with-mhash --with-mcrypt=/usr/local/libmcrypt --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --enable-maintainer-zts && make && make install
RUN  cd php-5.5.38 && cp php.ini-production /etc/php.ini
RUN  cd /php-5.5.38 && cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm
RUN  chmod +x /etc/init.d/php-fpm && chkconfig --add php-fpm && chkconfig php-fpm on
RUN  cp /usr/local/php5.5/etc/php-fpm.conf.default  /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's*;pid = run/php-fpm.pid*pid = run/php-fpm.pid*g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/user = nobody/user = nginx/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/group = nobody/group = nginx/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.max_children = 5/pm.max_children = 50/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.start_servers = 2/pm.start_servers = 5/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.min_spare_servers = 1/pm.min_spare_servers = 5/g' /usr/local/php5.5/etc/php-fpm.conf
RUN sed -i 's/pm.max_spare_servers = 3/pm.max_spare_servers = 30/g' /usr/local/php5.5/etc/php-fpm.conf
EXPOSE 9000
EXPOSE 80

#Docker部署nginx-php
[root@docker-lnmp nginx]# docker build -t "centos:nginx-php" .  //构建nginx-php镜像
#列出镜像
[root@server nginx]# docker images
REPOSITORY   TAG         IMAGE ID       CREATED         SIZE
centos       nginx-php   c5329a769fa7   5 minutes ago   1.52GB
tomcat       tomcat01    0bf0df22fdea   5 hours ago     584MB
centos       7           8652b9f0cb4c   9 months ago    204MB

#容器能使用的内存和交换分区大小
[root@server nginx]# docker run -dit -p 80:80 -m 500m --memory-swap 1G c5329a769fa7 /bin/bash
eeef4f39dc3af2d31769f1eea3e4c662eed5a897033b434ecf9c402294c98302

#查看当前运行容器
[root@server nginx]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                         NAMES
eeef4f39dc3a   c5329a769fa7      "/bin/bash"              41 seconds ago   Up 40 seconds   0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   sad_bhaskara
cceec12535db   tomcat:tomcat01   "/usr/local/apache-t…"   5 hours ago      Up 5 hours      0.0.0.0:49153->8080/tcp, :::49153->8080/tcp   tomcat

#进入容器
[root@server nginx]# docker exec -it eeef4f39dc3a /bin/bash 

[root@eeef4f39dc3a /]# nginx 
[root@eeef4f39dc3a /]#  netstat -antp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      40/nginx: master pr 
[root@eeef4f39dc3a /]# 

2.3 创建mysql-Dockerfile:

[root@docker-lnmp mysql]# cat Dockerfile 

FROM docker.io/centos:7
RUN yum -y install gcc gcc-c++ make autoconf make cmake wget
RUN groupadd mysql; useradd -r -M -u 3306 -s /sbin/nologin -g mysql mysql
RUN mkdir /usr/local/mysql; mkdir /data/mysql -pv
RUN yum install gcc gcc-c++ ncurses-devel bison bison-devel -y
RUN wget -c http://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.29.tar.gz
RUN tar xf mysql-5.6.29.tar.gz -C /usr/local/src/
WORKDIR /usr/local/src/mysql-5.6.29
RUN cmake . -DCMAKE_INSTALL_PREFIX=/usr/local/mysql -DMYSQL_DATADIR=/data/mysql -DSYSCONFDIR=/etc -DMYSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_MYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=utf8 -DEXTRA_CHARSETS=all -DDEFAULT_COLLATION=utf8_general_ci -DWITH-MYSQLD-LDFLAGS=-all-static -DWITH-CLIENT-LD-FLAGS=-all-static -DWITH_DEBUG=0 && gmake && gmake install
RUN chown -R root:mysql /usr/local/mysql/ && chown -R mysql:mysql /data/mysql
RUN chmod 755 /usr/local/src/mysql-5.6.29/scripts/mysql_install_db.sh
RUN /usr/local/src/mysql-5.6.29/scripts/mysql_install_db.sh --basedir=/usr/local/mysql --datadir=/data/mysql --no-defaults --user=mysql
RUN cp /usr/local/src/mysql-5.6.29/support-files/my-default.cnf  /etc/my.cnf
RUN cp /usr/local/src/mysql-5.6.29/support-files/mysql.server  /etc/init.d/mysqld
RUN chmod 775 /etc/init.d/mysqld && /etc/init.d/mysqld start
RUN echo -e '#!/bin/bash\nexport PATH=$PATH:/usr/local/mysql/bin' >/etc/profile.d/mysql.sh
RUN source /etc/profile
EXPOSE 3306


#Docker部署mysql
[root@docker-lnmp mysql]# docker build -t "centos:mysql-5.6" . 
[root@server mysql]# docker images
REPOSITORY   TAG         IMAGE ID       CREATED              SIZE
centos       mysql-5.6   c3bf6d638115   About a minute ago   5.24GB
centos       nginx-php   c5329a769fa7   49 minutes ago       1.52GB
tomcat       tomcat01    0bf0df22fdea   6 hours ago          584MB
centos       7           8652b9f0cb4c   9 months ago         204MB
[root@server mysql]# 
[root@server mysql]# docker run -dit -p 3306:3306 --device-write-bps /dev/sda:10M c3bf6d638115 /bin/bash
4b9d41a8c474b8d3188867601b31da96208a623781fadc830ec6db88cd2bcc93
[root@server mysql]# 
[root@server mysql]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED          STATUS          PORTS                                         NAMES
4b9d41a8c474   c3bf6d638115      "/bin/bash"              5 minutes ago    Up 5 minutes    0.0.0.0:3306->3306/tcp, :::3306->3306/tcp     intelligent_ellis
eeef4f39dc3a   c5329a769fa7      "/bin/bash"              49 minutes ago   Up 49 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   sad_bhaskara
cceec12535db   tomcat:tomcat01   "/usr/local/apache-t…"   6 hours ago      Up 6 hours      0.0.0.0:49153->8080/tcp, :::49153->8080/tcp   tomcat

#进入容器
[root@server mysql]# docker exec -it 4b9d41a8c474 /bin/bash
#开启服务
[root@4b9d41a8c474 mysql-5.6.29]# /etc/init.d/mysqld start
Starting MySQL... SUCCESS! 

#设置数据库密码,进入数据库,授权
[root@4b9d41a8c474 mysql-5.6.29]# mysqladmin -uroot -p password
Enter password:        #默认回车
New password:          #设置密码123456
Confirm new password:  #再次输入密码213456

[root@4b9d41a8c474 mysql-5.6.29]# mysql -uroot -p123456
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.6.29 Source distribution

Copyright (c) 2000, 2016, 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 default charset utf8 COLLATE utf8_general_ci;   #创建库
mysql> grant all privileges on wordpress.*  to 'wordpress'@'%' identified by '123456' with grant option;   #提权
mysql> flush privileges;

mysql> exit
Bye

2.4 测试

[root@server ~]# docker ps
CONTAINER ID   IMAGE             COMMAND                  CREATED             STATUS             PORTS                                         NAMES
4b9d41a8c474   c3bf6d638115      "/bin/bash"              20 minutes ago      Up 20 minutes      0.0.0.0:3306->3306/tcp, :::3306->3306/tcp     intelligent_ellis
eeef4f39dc3a   c5329a769fa7      "/bin/bash"              About an hour ago   Up About an hour   0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   sad_bhaskara
cceec12535db   tomcat:tomcat01   "/usr/local/apache-t…"   6 hours ago         Up 6 hours         0.0.0.0:49153->8080/tcp, :::49153->8080/tcp   tomcat

[root@server ~]# docker exec -it eeef4f39dc3a /bin/bash

[root@eeef4f39dc3a /]# vi /usr/local/nginx/html/wordpress/wp-config.php

###可查看容器内应用的ip,如果出现数据库连接错误,记得修改/usr/local/nginx/html/wordpress/wp-config.php中的IP地址

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Docker部署LNMP架构(Linux+Nginx+MySQL+PHP),首先需要安装DockerDocker Compose。你可以按照以下步骤进行部署: 1. 安装DockerDocker Compose: 首先,按照引用中的指引,配置Docker的yum源,并安装DockerDocker Compose。安装完成后,启动Docker服务。 2. 准备部署文件: 下载引用中的docker-lnmp项目,将docker-compose-linux-x86_64-v2.5.0文件移动到/usr/local/bin目录下,并赋予执行权限。 3. 编辑docker-compose.yml文件: 进入docker-lnmp项目文件夹,在该目录下找到docker-compose.yml文件,并使用任意文本编辑器进行编辑。根据你的需求,可以对镜像版本、端口映射、环境变量等进行配置。 4. 运行Docker Compose: 在docker-lnmp项目文件夹中打开终端,并执行以下命令: ``` docker-compose up -d ``` 这将启动LNMP架构的Docker容器,并在后台运行。 5. 验证部署: 使用浏览器访问本地主机的Nginx服务,默认端口为80。如果一切正常,你应该能够看到Nginx的欢迎页面。 通过以上步骤,你可以使用Docker快速部署LNMP架构,并实现应用程序的隔离性和安全性。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Docker部署LNMP完整教程](https://blog.csdn.net/ThesCript_j/article/details/107402923)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [docker部署lnmp架构](https://blog.csdn.net/weixin_65643437/article/details/131124609)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值