[置顶] 基于Dockerfile构建LNMP环境(php7+mysql+nginx) --原创

标签: docker dockerfile lnmp php7 linux
515人阅读 评论(0) 收藏 举报
分类:

构建LNMP环境(php7+mysql+nginx) –原创

经几个星期反复测试总结出了整个lnmp环境的Dockerfile文件,并已经适用于公司开发环境中。

centOS7.4.1708 + tengine-2.1.2 + php-7.0.17 + PHP扩展

PHP7扩展包括

PDO、PDO_MYSQL、GD、CURL、
mysqli、pcntl、soap、Redis、
mongodb、memcached、gearman、
zeromq、ICE

创建名为Dockerfile的文件

FROM centos:7.4.1708
MAINTAINER hanlc <282552154@qq.com>
RUN mkdir -p /data/software && mkdir -p /usr/local/webserver
ADD repo/ \
/data/software/
WORKDIR /data/software
#安装依赖
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup && \
mv CentOS7-Base-163.repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum makecache && \
yum -y install gcc gcc-c++\
autoconf wget \
psmisc \
openssl openssl-devel \
gperftools-devel \
tar \
passwd \
openssh-server \
openssh-clients \
initscripts \
unzip pcre pcre-devel zlib zlib-devel git \
libxml2 libxml2-devel curl curl-devel \
libjpeg libjpeg-devel libpng libpng-devel freetype freetype-devel \
python-setuptools dos2unix gperf \
libevent libevent-devel bzip2-devel ncurses-devel \
boost libtool boost-devel* libuuid-devel python-sphinx.noarch &&\
yum clean all &&\
rm -rf /var/lib/apt/lists/* &&\
rm -rf /var/cache/yum
#用户账号设置
RUN echo 'root:123465' | chpasswd
RUN /usr/sbin/sshd-keygen
RUN /usr/sbin/groupadd oae &&/usr/sbin/useradd -r -m -s /bin/bash -g oae oae && echo "oae
ALL=(ALL) ALL" >> /etc/sudoers && echo 'root:123465' | chpasswd
#安装tengine
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/pcre-8.10.tar.gz &&\
tar zxvf pcre-8.10.tar.gz &&rm -f pcre-8.10.tar.gz && \
cd pcre-8.10 &&./configure&& make&&make install && \
cd .. && rm -rf pcre-8.10 && \
wget https://gitee.com/hanlicun/ltmp/raw/master/src/tengine-2.1.2.tar.gz &&\
tar -zxvf tengine-2.1.2.tar.gz && rm -f tengine-2.1.2.tar.gz &&\
cd tengine-2.1.2 && \
./configure --prefix=/usr/local/webserver/tengine --user=oae --group=oae
--with-http_stub_status_module --with-http_ssl_module --with-file-aio --with-http_realip_module &&\
make &&make install &&\
cd ../ && rm -rf ./tengine-2.1.2 &&\
wget https://gitee.com/hanlicun/ltmp/raw/master/src/nginx.conf &&\
mv nginx.conf \
/usr/local/webserver/tengine/conf/nginx.conf &&\
wget https://gitee.com/hanlicun/ltmp/raw/master/src/nginx &&\
mv ./nginx /etc/init.d/nginx && \
chmod +x /etc/init.d/nginx
# 安装 Libmcrypt
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/libmcrypt-2.5.8.tar.gz &&\
tar zxvf libmcrypt-2.5.8.tar.gz &&rm -f libmcrypt-2.5.8.tar.gz &&\
cd libmcrypt-2.5.8 && \
./configure --prefix=/usr/local && \
make && make install &&\
cd .. && rm -rf libmcrypt-2.5.8
# 安装 PHP7
RUN wget http://cn2.php.net/get/php-7.0.17.tar.gz/from/this/mirror && \
tar zxvf mirror && rm -f php-7.0.17.tar.gz && rm -rf mirror &&\
cd php-7.0.17 && \
./configure --prefix=/usr/local/webserver/php --with-config-file-path=/usr/local/webserver/php/etc
--enable-fpm --with-fpm-user=oae --with-fpm-group=oae --with-mysqli --with-pdo-mysql
--with-iconv-dir --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr
--enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem
--enable-inline-optimization --with-curl --enable-mbregex --enable-mbstring --enable-ftp --with-gd
--enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc
--enable-zip --enable-soap --without-pear --with-gettext --enable-fileinfo --enable-maintainer-zts &&
make clean &&make && make install && \
cp ./php.ini-development /usr/local/webserver/php/etc/php.ini && \
cp ./sapi/fpm/init.d.php-fpm.in /etc/init.d/php-fpm &&\
chmod 755 /etc/init.d/php-fpm && \
sed -i '$a chown -R oae:oae /var/run/' /etc/init.d/php-fpm && \
cd /data/software && rm -rf php-7.0.17 && \
sed -i '$aPATH=/usr/local/webserver/php/bin:\$PATH\n export PATH' /etc/profile &&\
source /etc/profile && \
mv /usr/local/webserver/php/etc/php-fpm.conf.default /usr/local/webserver/php/etc/php-fpm.conf &&
\
sed -i 's!@sbindir@!/usr/local/webserver/php/sbin!g' /etc/init.d/php-fpm && \
sed -i 's!@sysconfdir@!/usr/local/webserver/php/etc!g' /etc/init.d/php-fpm && \
sed -i 's!@localstatedir@!/var!g' /etc/init.d/php-fpm && \
mv /usr/local/webserver/php/etc/php-fpm.d/www.conf.default
/usr/local/webserver/php/etc/php-fpm.d/www.conf && \
sed -i 's!127.0.0.1:9000!/var/run/php-fpm.sock!g' /usr/local/webserver/php/etc/php-fpm.d/www.conf
&&\
ln -s /usr/local/webserver/php/bin/php /usr/local/bin/php
#安装 PHP的Redis扩展
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/phpredis.tar.gz &&tar zxvf phpredis.tar.gz
&& rm -rf phpredis.tar.gz &&cd phpredis && /usr/local/webserver/php/bin/phpize && ./configure
--with-php-config=/usr/local/webserver/php/bin/php-config &&make &&make install &&sed -i '$a
extension_dir =/usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/\nextension
=redis.so\n' /usr/local/webserver/php/etc/php.ini && cd .. &&rm -rf phpredis
#安装 PHP的mongodb扩展
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/mongodb.so && mv mongodb.so
/usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/ && sed -i '$a extension
=mongodb.so' /usr/local/webserver/php/etc/php.ini
#安装 PHP的memcached扩展
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/libmemcached-1.0.18.tar.gz &&tar zxvf
libmemcached-1.0.18.tar.gz && rm -rf libmemcached-1.0.18.tar.gz &&cd libmemcached-1.0.18/
&&mkdir -p /usr/local/webserver/libmemcached && ./configure
--prefix=/usr/local/webserver/libmemcached &&make && make install && cd ../ && rm -rf
libmemcached-1.0.18/ && wget https://gitee.com/hanlicun/ltmp/raw/master/src/memcached-3.0.2.tgz
&&tar zxvf memcached-3.0.2.tgz && rm -rf memcached-3.0.2.tgz && cd memcached-3.0.2/
&&/usr/local/webserver/php/bin/phpize && ./configure --enable-memcached
--with-php-config=/usr/local/webserver/php/bin/php-config
--with-libmemcached-dir=/usr/local/webserver/libmemcached --disable-memcached-sasl && make &&
make install &&cd ../ && rm -rf memcached-3.0.2/ && sed -i '$a extension =memcached.so'
/usr/local/webserver/php/etc/php.ini
#安装gearman扩展
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/gearmand.tar.gz &&tar zxvf gearmand.tar.gz
&& rm -rf gearmand.tar.gz && cd gearmand &&./bootstrap.sh -a &&./configure &&make && make
install && cd .. &&rm -rf gearmand &&wget
https://gitee.com/hanlicun/ltmp/raw/master/src/gearman.so &&mv gearman.so
/usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/ && sed -i '$a extension
=gearman.so' /usr/local/webserver/php/etc/php.ini
#安装zmq扩展
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/libzmq.tar.gz &&tar zxvf libzmq.tar.gz &&
rm -rf libzmq.tar.gz && cd libzmq && ./autogen.sh && ./configure && make -j 4 &&make check &&
make install && ldconfig &&cd .. &&wget https://gitee.com/hanlicun/ltmp/raw/master/src/zmq.so
&&mv zmq.so /usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/ && rm -rf libzmq
&& sed -i '$a extension =zmq.so' /usr/local/webserver/php/etc/php.ini
#或
#wget https://pecl.php.net/get/zmq-1.1.3.tgz && rm -rf zmq-1.1.3.tgz && \
#cd zmq-1.1.3 && /usr/local/webserver/php/bin/phpize &&\
#./configure --prefix=/usr/local/webserver/zmq
--with-php-config=/usr/local/webserver/php/bin/php-config &&\
# make && make install &&\
#cd pecl-gearman-master
#/usr/local/webserver/php/bin/phpize
#./configure --prefix=/usr/local/webserver/gearman
--with-php-config=/usr/local/webserver/php/bin/php-config
#安装php的ice扩展
RUN cd /data/software && wget https://gitee.com/hanlicun/ltmp/raw/master/src/IcePHP.so &&mv
IcePHP.so /usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/ && wget
https://gitee.com/hanlicun/ltmp/raw/master/src/Ice-3.6.4.tar.gz &&tar zxvf Ice-3.6.4.tar.gz && mv
Ice-3.6.4 /opt/ && rm -rf Ice-3.6.4.tar.gz&& sed -i '$a extension =IcePHP.so'
/usr/local/webserver/php/etc/php.ini && sed -i '$a export LD_LIBRARY_PATH=/opt/Ice-3.6.4/lib64'
/etc/profile
#Install Composer
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/composer &&mv composer
/usr/local/bin/composer && chmod +x /usr/local/bin/composer && /usr/local/bin/composer config -g
repo.packagist composer https://packagist.phpcomposer.com
RUN chmod -R 777 /usr/local/webserver/php/var/log/ && chown -R oae:oae /var/run/ && \
mkdir -p /home/oae/web/wwwroot/public/ && \
touch /home/oae/web/wwwroot/public/index.html && \
echo ok > /home/oae/web/wwwroot/public/index.html &&\
echo "<?php phpinfo();?>" > /home/oae/web/wwwroot/public/index.php
#开放端口
EXPOSE 80 22
# 安装 supervisord
RUN easy_install supervisor && \
mkdir -p /usr/local/var/log/supervisord
RUN wget https://gitee.com/hanlicun/ltmp/raw/master/src/supervisord.conf &&\
mv supervisord.conf /etc/supervisord.conf && \
mkdir -p /usr/local/var/run
CMD ["/usr/bin/supervisord","-c", "/etc/supervisord"]

构建docker容器

在Dockerfile当前目录下执行

docker build -t lnmp .

然后运行启动一个docker容器

docker run -d -p 88:80 -p 1022:22 lnmp

通过ssh登陆容器内

ssh root@127.0.0.1:1022 -p

或可以直接从阿里云直接下载该镜像

从registry中拉取镜像:

docker pull registry.cn-zhangjiakou.aliyuncs.com/hanlc/lnmp
查看评论

Dockerfile指令用法详解【18年新版】

-
  • 1970年01月01日 08:00

Docker笔记一:基于Docker容器构建并运行 nginx + php + mysql ( mariadb ) 服务环境

在编辑Dockerfile过程中参考了以下相关范例:https://hub.docker.com/_/php/、https://hub.docker.com/_/mysql/、https://hub....
  • jkx1132
  • jkx1132
  • 2017-01-04 19:54:32
  • 4949

使用docker部署php7 mysql nginx(richarvey/nginx-php-fpm+mysql)

启动mysql容器 docker run --name mysql \ -e MYSQL_ROOT_PASSWORD=123456 \ --restart=always \ -v `pwd`...
  • jiaHey
  • jiaHey
  • 2018-01-30 10:11:07
  • 302

ubuntu16.10 docker 配置ngixn + php7 + mysql

一:环境 环境ubuntu 16.10 64位 安装docker : 方法1: sudo apt-get update sudo apt-get install docker-engine 方法2:...
  • liulele549160595
  • liulele549160595
  • 2017-06-08 15:13:24
  • 1133

【docker】使用docker快速搭建nginx+php开发环境

前面说了一下docker的简介以及在Windows中的安装过程,下面就开始简单说一下如何用docker搭建常用的开发环境,以便读者快速上手。事先准备 docker已经下载完毕并正确安装。 找一个适合自...
  • qq_28602957
  • qq_28602957
  • 2016-12-18 15:27:37
  • 19902

用dockerfile构建基于ubuntu的lnmp环境的镜像

本文主要讲述的是如何用dockerfile构建lnmp环境的镜像,前提默认你已安装好docker...
  • turtleo
  • turtleo
  • 2016-09-06 18:50:21
  • 2661

基于Docker搭建LNMP环境

作为一个程序员,我们在做一个项目的时候,往往需要用到一台正式的服务器和一台测试的服务器。如果你的主机配置足够好,那么,你可以利用虚拟机在同一台主机上装多台Web应用的服务器。当然,土豪公司的话也可以多...
  • xy752068432
  • xy752068432
  • 2017-07-24 10:06:55
  • 8489

Docker部署WordPress LNMP(Nginx PHP MySQL)环境实践

Docker基于LXC实现了把软件封装到一个完整的文件系统,可以在docker容器中运行所需的一切代码,运行环境,系统工具和系统库。由于docker使用独立于主机的文件系统,可以确保软件在不同的主机环...
  • tlrxt1xn
  • tlrxt1xn
  • 2017-02-19 11:16:03
  • 825

docker lnmp liunx+nginx+mysql+php 分离部署

背景每次换电脑都要重新部署lnmp,每次花费的时间至少要半天,很浪费时间。所以就把lnmp docker化。环境本机环境:ubuntu 16.04 LTS安装dockeer$ apt-get inst...
  • SakuraLLj
  • SakuraLLj
  • 2017-06-09 17:30:03
  • 1178

docker 之基于Dockerfile构建 Nginx+ php7

前言 本文是将Nginx 和PHP7安装在不同容器,同–link实现交互通信,网上查有些资料将LNMP装在一个容器,我认为装在一起固然简单一些,但不够灵活(纯个人看法,有不同观点,欢迎回复)。 系...
  • guyan0319
  • guyan0319
  • 2018-03-21 17:09:25
  • 73
    个人资料
    持之以恒
    等级:
    访问量: 992
    积分: 68
    排名: 160万+
    文章分类
    文章存档