使用Docker部署lnmp和wordpress

主机IP地址操作系统
Docker192.168.20.11Cenos 7
  • ps:资源可以在csdn资源中搜索
php-5.5.38.tar.gz        mysql-5.6.36.tar.gz
nginx-1.12.2.tar.gz        libmcrypt-2.5.7.tar.gz           wordpress-4.9.4-zh_CN.tar.gz

我这里是提前解压然后一股脑上传到xshell的

环境以及docker的安装

[root@localhost ~]# hostnamectl set-hostname docker     //修改主机名
[root@localhost ~]# su

[root@docker ~]# setenforce 0
[root@docker ~]# yum clean all && yum makecache             //清空repo源并重载
  • 安装docker源
[root@docker ~]# yum -y install yum-utils device-mapper-persistent-data lvm2    
[root@docker ~]# yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
已加载插件:fastestmirror, langpacks
adding repo from: https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
grabbing file https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo to /etc/yum.repos.d/docker-ce.repo
repo saved to /etc/yum.repos.d/docker-ce.repo
  • 安装docker并启动
[root@docker ~]# yum -y install docker-ce         //下载docker

[root@docker ~]# systemctl start docker          //启动docker
[root@docker ~]# systemctl enable docker         //开机自启
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
[root@docker ~]# systemctl daemon-reload       //启动守护进程
[root@docker ~]# systemctl restart docker       //重启docker

  • 按照下方树放入对应的安装包并解压
[root@docker docker]# mkdir docker && cd docker         //创建docker工作目录
[root@docker docker]# yum -y install tree            //下载工具
[root@docker docker]# tree -L 2
.
├── mysql
   ├── Dockerfile.txt
   ├── mysql-5.6.36
   └── mysql-5.6.36.tar.gz
└── nginx
    ├── 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

docker部署nginx

[root@docker nginx]# cat >>Dockerfile <<EOF
> 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
EOF

[root@docker nginx]# docker build -t "centos:nginx-php" .
....................
Successfully built 93c31fc4d972
Successfully tagged centos:nginx-php
[root@docker nginx]# docker images            //查看镜像
REPOSITORY   TAG         IMAGE ID       CREATED          SIZE
centos       nginx-php   93c31fc4d972   12 minutes ago   1.52GB
centos       7           8652b9f0cb4c   9 months ago     204MB

[root@docker nginx]# docker run -dit -p 80:80 -m 500m --memory-swap 1G 93c31fc4d972 /bin/bash        //指定端口和大小调用镜像创建容器
10b86f89e0854b75a7c16e2112ba96e16cae0f6df66c8a9c2efbb693ffcbed94
[root@docker nginx]# docker ps -a                        //查看容器
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS          PORTS                                         NAMES
10b86f89e085   f5db5aa42f96   "/bin/bash"   7 seconds ago    Up 6 seconds    0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   gallant_davinci

[root@10b86f89e085 /]# /etc/init.d/php-fpm start              //开启php
Starting php-fpm  done
[root@10b86f89e085 /]# nginx              //开启nginx
[root@10b86f89e085 /]# netstat -natp | grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      39/nginx: master pr 
[root@10b86f89e085 /]# netstat -natp | grep php  
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      32/php-fpm: master
  • mysql
[root@docker nginx]# cd ..
[root@docker docker]# cd mysql
[root@docker mysql]# cat >>Dockerfile<<EOF
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 -YSQL_TCP_PORT=3306 -DMYSQL_UNIX_ADDR=/var/lib/mysql/mysql.sock -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITMYISAM_STORAGE_ENGINE=1 -DENABLED_LOCAL_INFILE=1 -DWITH_PARTITION_STORAGE_ENGINE=1 -DDEFAULT_CHARSET=f8 -DEXTRA_CHARSETS=all -DDEFAULT_COLLATION=utf8_general_ci -DWITH-MYSQLD-LDFLAGS=-all-static -DWITH-IENT-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=/da/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
EOF

Successfully built 66f4746a3cb6
Successfully tagged centos:mysql-5.6
[root@docker mysql]# docker images
REPOSITORY   TAG         IMAGE ID       CREATED          SIZE
centos       mysql-5.6   66f4746a3cb6   20 seconds ago   5.24GB
centos       nginx-php   93c31fc4d972   31 minutes ago   1.52GB
centos       7           8652b9f0cb4c   9 months ago     204MB
[root@docker mysql]# docker run -dit -p 3306:3306 --device-write-bps /dev/sda:10M 66f4746a3cb6 /bin/bash
0bc36caae720e047120ec48d5a291ed10aa3e104f28e3340e5bdcda2d041ba1d
[root@docker mysql]# docker ps -a
CONTAINER ID   IMAGE          COMMAND       CREATED          STATUS          PORTS                                         NAMES
0bc36caae720   66f4746a3cb6   "/bin/bash"   5 seconds ago    Up 3 seconds    0.0.0.0:3306->3306/tcp, :::3306->3306/tcp     amazing_banach
10b86f89e085   93c31fc4d972   "/bin/bash"   18 minutes ago   Up 18 minutes   0.0.0.0:80->80/tcp, :::80->80/tcp, 9000/tcp   hopeful_wing
[root@docker mysql]# docker exec -it 0bc36caae720 /bin/bash
[root@0bc36caae720 mysql-5.6.29]# /etc/init.d/mysqld start
Starting MySQL............ SUCCESS! 


[root@0bc36caae720 mysql-5.6.29]# mysqladmin -uroot -p password       //给mysql设置密码123456
Enter password:                             //这里直接回车
New password: 
Confirm new password: 


[root@0bc36caae720 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 7
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;    //创建wordpress表
Query OK, 1 row affected (0.01 sec)

mysql> grant all privileges on wordpress.* to 'wordpress'@'%'identified by '123456' with grant option;           //给权限
Query OK, 0 rows affected (0.00 sec) 

mysql> flush privileges;              //刷新权限生效
Query OK, 0 rows affected (0.00 sec)

mysql> exit
Bye

  • 通过宿主机浏览器访问验证(http://xxx.xxx.xxx.xxx/wordpress/index.php)

在这里插入图片描述

  • 进入nginx容器
[root@docker nginx]# docker inspect --format='{{.NetworkSettings.IPAddress}}' 10b86f89e085
172.17.0.3

[root@docker nginx]# docker exec -it 2e52ca1ed647 /bin/bash
[root@10b86f89e085 /]# yum -y install vim             //下载vim工具
[root@10b86f89e085 /]# vim /usr/local/nginx/html/wordpress/wp-config.php      //修改对应的密码和主机ip

在这里插入图片描述

  • 再次尝试连接
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值