阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Dockerfile构建LNMP分离环境部署WordPress

188次阅读
没有评论

共计 6366 个字符,预计需要花费 16 分钟才能阅读完成。

最近忙着写自己的项目,也把一个站点的 bbs 论坛打算迁移到 Docker 中,测试没发现啥大问题。在单台上面的架构如下;(往后我们也是要讲到 compose 和 swarm 调度的慢慢来)

Dockerfile 构建 LNMP 分离环境部署 WordPress

1、首先我们先安装一下 docker,好多人都发现国内用 yum 安装有各种问题; 这里我们用国内的 https://www.daocloud.io. 登录后注册,然后点击下载。里面有提示,我们点击 Linxu 安装然后复制代码执行到 shell 上即可。

[root@test nginx]# curl -sSL https://get.daocloud.io/docker | sh

2、安装好之后,安装 dockhub 加速器,点击加速器,复制代码粘贴到 shell.

[root@test nginx]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://681a96df.m.daocloud.io
{“registry-mirrors”: [“http://681a96df.m.daocloud.io”],
    “live-restore”: true
}
Success.
You need to restart docker to take effect: sudo systemctl restart docker

## 执行脚本,主要是把仓库地址写到 daemon.json 文件下。

[root@test nginx]# cat /etc/docker/daemon.json
{“registry-mirrors”: [“http://681a96df.m.daocloud.io”],
    “live-restore”: true
}

3、准备工作都已经完成了,接下来我们来构建一下 dockerfile 在三个目录下,看下目录结构:

[root@test test]# tree -L 2 –charset ASCII
|– MySQL
|  |– Dockerfile
|  |– epel-6.repo
|  |– my.cnf
|  `– startup.sh
|– nginx
|  |– Dockerfile
|  |– nginx-1.11.10
|  |– nginx-1.11.10.tar.gz
|  |– nginx.conf
|  `– nginx_default.conf
`– php-fpm
    |– CentOS-6.repo
    |– Dockerfile
    |– epel-6.repo
    |– php-5.5.38
    `– php-5.5.38.tar.gz

5、看一下 nginx 的 Dockerfile:

[root@test nginx]# cat Dockerfile
#lnmp centos 6.0
from centos:centos6
MAINTAINER xiaoluo <xiaoluo@test.com>
ENV APP_DIR /web
add nginx-1.11.10 /nginx-1.11.10
RUN yum -y groupinstall “Development Tools” “Server Platform Deveopment”
RUN yum -y install openssl-devel pcre-devel
RUN useradd nginx -s /sbin/nologin
RUN cd /nginx-1.11.10 && ./configure –prefix=/usr/local/nginx –user=nginx –group=nginx –with-http_ssl_module –with-http_flv_module –with-http_stub_status_module –with-http_gzip_static_module  –with-pcre && make && make install
RUN mkdir /usr/local/nginx/conf/vhosts
RUN mkdir /var/log/nginx
ADD nginx.conf /usr/local/nginx/conf/nginx.conf
ADD nginx_default.conf /usr/local/nginx/conf/vhosts/default.conf
EXPOSE 80
CMD [“/usr/local/nginx/sbin/nginx”]

##nginx 相关 php 配置:

[root@test nginx]# cat nginx_default.conf
server {
    listen      80 default_server;
    server_name  localhost;
    #charset koi8-r;
    location / {
        root  /web;
        index  index.php index.html index.htm;
    }
    # redirect server error pages to the static page /50x.html
    #
    error_page  500 502 503 504  /50x.html;
    location = /50x.html {
        root  APP_DIR;
    }
    # Disable nginx log write favicon.ico
    location = /favicon.ico {
    log_not_found off;
    access_log off;
        }
    # pass the PHP scripts to FastCGI server listening on port 9000
    #
    location ~ \.php$ {
        root          /web;
        fastcgi_pass  php:9000;
        #fastcgi_pass  unix:/tmp/php-fpm.sock;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}

###php:9000 是通过后面的 –link 容器之间互联指定

6、开始构建 nginx 镜像:

[root@test nginx]# docker build -t lnmp/nginx:1.0 .

## 查看是否生成镜像:

[root@test nginx]# docker images
REPOSITORY          TAG                IMAGE ID            CREATED            SIZE
lnmp/nginx          1.0                5f5d4169189d        4 minutes ago      669 MB

7、开始构建 php 镜像:

[root@test php-fpm]# cat Dockerfile
from centos:centos6
ADD Centos-6.repo /etc/yum.repos.d/CentOS-Base.repo
ADD epel-6.repo /etc/yum.repos.d/epel.repo
add php-5.5.38 /php-5.5.38
RUN yum -y groupinstall  “Desktop Platform Development”
RUN yum -y install libmcrypt-devel bzip2-devel gcc openssl-devel php-mcrypt libmcrypt
RUN cd /php-5.5.38 && ./configure –prefix=/usr/local/php –with-mysql=mysqlnd –with-pdo-mysql=mysqlnd –with-mysqli=mysqlnd –with-openssl –enable-mbstring –with-freetype-dir –with-jpeg-dir –with-png-dir –with-zlib –with-libxml-dir=/usr –enable-xml –enable-sockets –with-mcrypt  –with-bz2 –enable-fpm –with-gd && make && make install
RUN cp /php-5.5.38/php.ini-production  /usr/local/php/etc/php.ini
RUN mv /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
RUN useradd -M -s /sbin/nologin php
RUN sed -i -e ‘s\;pid = run/php-fpm.pid\pid = run/php-fpm.pid\g’ -e ‘s\nobody\php\g’ -e ‘s\listen = 127.0.0.1:9000\listen = 0.0.0.0:9000\g’ /usr/local/php/etc/php-fpm.conf
RUN sed -i ‘s\;daemonize = yes\daemonize = no\g’ /usr/local/php/etc/php-fpm.conf
EXPOSE 9000
CMD [“/usr/local/php/sbin/php-fpm”]

8、开始构建 php 镜像:

[root@test php-fpm]# docker build -t lnmp/php:1.0 .

9、构建 mysql 镜像的 Dockerfile:

[root@test mysql]# cat Dockerfile
FROM centos:centos6 
MAINTAINER xiaoluo “18878774@163.com” 
RUN yum install -y mysql-server mysql 
ADD ./startup.sh /opt/startup.sh
RUN chmod +x /opt/startup.sh
EXPOSE 3306
CMD [“/bin/bash”,”/opt/startup.sh”]

## 启动脚本:

[root@test mysql]# cat startup.sh
#!/bin/bash
if [! -f /var/lib/mysql/ibdata1]; then
        mysql_install_db
        /usr/bin/mysqld_safe &
        sleep 10s
        mysql -e “grant all privileges on *.* to ‘root’@’%’ identified by ‘123456’; FLUSH PRIVILEGES;”
        killall mysqld
        sleep 10s
fi
/usr/bin/mysqld_safe

** 正常启动的时候,是没有问题的;当时当我们用 - v 做持久化的时候,好像说用户就失去对 /var/lib/mysql 的控制权,所以启动的时候我们要判断初始化才可以用 - v 来持久化相关目录,这个地方之前搞了好久就是挂不起来,后面原来是这个地方。

10、开始构建 mysql 镜像:

[root@test mysql]# docker build -t lnmp/mysql:1.0 .

11、下面我们开始启动相关容器:

[root@test web]# docker run -dit –name php -v /web:/web lnmp/php:1.0
[root@test web]# docker run -dit –name web -p 80:80 -v /web:/web –link php:php lnmp/nginx:1.0
[root@test web]#docker run -dit –name mysql -p 3306:3306 -v /opt/data:/var/lib/mysql lnmp/mysql:1.0

#####

[root@test mysql]# docker ps
CONTAINER ID        IMAGE              COMMAND                  CREATED              STATUS              PORTS                    NAMES
3527cddb4c50        lnmp/mysql:1.0      “/bin/bash /opt/st…”  4 seconds ago        Up 3 seconds        0.0.0.0:3306->3306/tcp  mysql
fab93953c438        lnmp/nginx:1.0      “/usr/local/nginx/…”  About a minute ago  Up About a minute  0.0.0.0:80->80/tcp      web
d5854337c10b        lnmp/php:1.0        “/usr/local/php/sb…”  3 minutes ago        Up 2 minutes        9000/tcp                php

## 可以看到我们已经都启动了所有的容器了。

12、接下来我们登录一下 mysql. 创建一下 wordpress 使用的数据库:

[root@test mysql]# mysql -uroot -p123456 -h 192.168.63.200
MySQL [(none)]> CREATE DATABASE wordpress DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

13、然后我们把 wordpress 代码放到我们挂载的本地 /web 目录下面:

[root@test web]# wget https://cn.wordpress.org/wordpress-4.7.2-zh_CN.tar.gz

# 然后解压出来。我们直接访问一下当前主机的 IP 地址:

Dockerfile 构建 LNMP 分离环境部署 WordPress

直接往下走注册即可:

Dockerfile 构建 LNMP 分离环境部署 WordPress

Dockerfile 构建 LNMP 分离环境部署 WordPress

## 到此在 Docker 分离下安装 wordpress 已经完成,但是我们要思考一个问题,就是有没有更好的方法统一编排一下这些容器呢,给容器更好的分组管理:可以留意一下 docker-compose, 在 1.13 之后更是结合栈来实现跨主机编排。

## 还有一个就是如何给这些容器做成集群管理,保证节点的高可用。和资源监控调度呢。可以看一下 1.12 之后的 docker swarm,构建集群非常简单。

LNMP 环境搭建 (Discuz 论坛)  http://www.linuxidc.com/Linux/2016-03/129334.htm

Ubuntu 14.04 下 apt-get 方法安装 LNMP 环境  http://www.linuxidc.com/Linux/2016-07/133683.htm

CentOS 7 源码编译安装 PHP5.6 和 Nginx1.7.9 及 MySQL(搭建 LNMP 环境) http://www.linuxidc.com/Linux/2015-12/126200.htm

Ubuntu 14.04 LTS 安装 LNMP Nginx\PHP5 (PHP-FPM)\MySQL  http://www.linuxidc.com/Linux/2014-05/102351.htm

CentOS 7 源码安装最新版 LNMP 环境 http://www.linuxidc.com/Linux/2015-04/116058.htm

Ubuntu 16.04 下源码配置 LNMP 开发环境 http://www.linuxidc.com/Linux/2016-09/135381.htm

LNMP 环境搭建以及调优 http://www.linuxidc.com/Linux/2017-01/140072.htm

本文永久更新链接地址 :http://www.linuxidc.com/Linux/2017-03/141243.htm

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-21发表,共计6366字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中