www.icesr.com
IT运维工程师的摇篮

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

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

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

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

PHP7扩展包括

<code>PDO、PDO_MYSQL、GD、CURL、
mysqli、pcntl、soap、Redis、
mongodb、memcached、gearman、
zeromq、ICE</code>

创建名为Dockerfile的文件

<code>FROM <span class="hljs-attribute">centos</span>:<span class="hljs-number">7.4</span><span class="hljs-number">.1708</span>
MAINTAINER hanlc &lt;<span class="hljs-number">282552154</span><span class="hljs-property">@qq</span>.com&gt;
RUN mkdir -p /data/software &amp;&amp; mkdir -p /usr/local/webserver
ADD repo/ /
<span class="hljs-regexp">/data/software/</span>
WORKDIR /data/software
<span class="hljs-comment">#安装依赖</span>
RUN mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup &amp;&amp; /
mv CentOS7-Base-<span class="hljs-number">163.</span>repo /etc/yum.repos.d/CentOS-Base.repo
RUN yum makecache &amp;&amp; /
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 &amp;&amp;/
yum clean all &amp;&amp;/
rm -rf <span class="hljs-regexp">/var/lib/apt/lists/</span>* &amp;&amp;/
rm -rf /<span class="hljs-reserved">var</span>/cache/yum
<span class="hljs-comment">#用户账号设置</span>
RUN echo <span class="hljs-string">'root:123465'</span> | chpasswd
RUN /usr/sbin/sshd-keygen
RUN /usr/sbin/groupadd oae &amp;&amp;/usr/sbin/useradd -r -m -s /bin/bash -g oae oae &amp;&amp; echo <span class="hljs-string">"oae
ALL=(ALL) ALL"</span> &gt;&gt; /etc/sudoers &amp;&amp; echo <span class="hljs-string">'root:123465'</span> | chpasswd
<span class="hljs-comment">#安装tengine</span>
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/pcre-<span class="hljs-number">8.10</span>.tar.gz &amp;&amp;/
tar zxvf pcre-<span class="hljs-number">8.10</span>.tar.gz &amp;&amp;rm -f pcre-<span class="hljs-number">8.10</span>.tar.gz &amp;&amp; /
cd pcre-<span class="hljs-number">8.10</span> &amp;&amp;./configure&amp;&amp; make&amp;&amp;make install &amp;&amp; /
cd .. &amp;&amp; rm -rf pcre-<span class="hljs-number">8.10</span> &amp;&amp; /
wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/tengine-<span class="hljs-number">2.1</span><span class="hljs-number">.2</span>.tar.gz &amp;&amp;/
tar -zxvf tengine-<span class="hljs-number">2.1</span><span class="hljs-number">.2</span>.tar.gz &amp;&amp; rm -f tengine-<span class="hljs-number">2.1</span><span class="hljs-number">.2</span>.tar.gz &amp;&amp;/
cd tengine-<span class="hljs-number">2.1</span><span class="hljs-number">.2</span> &amp;&amp; /
./configure --prefix=/usr/local/webserver/tengine --user=oae --group=oae
--<span class="hljs-reserved">with</span>-http_stub_status_module --<span class="hljs-reserved">with</span>-http_ssl_module --<span class="hljs-reserved">with</span>-file-aio --<span class="hljs-reserved">with</span>-http_realip_module &amp;&amp;/
make &amp;&amp;make install &amp;&amp;/
cd ../ &amp;&amp; rm -rf ./tengine-<span class="hljs-number">2.1</span><span class="hljs-number">.2</span> &amp;&amp;/
wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/nginx.conf &amp;&amp;/
mv nginx.conf /
/usr/local/webserver/tengine/conf/nginx.conf &amp;&amp;/
wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/nginx &amp;&amp;/
mv ./nginx /etc/init.d/nginx &amp;&amp; /
chmod +x /etc/init.d/nginx
<span class="hljs-comment"># 安装 Libmcrypt</span>
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/libmcrypt-<span class="hljs-number">2.5</span><span class="hljs-number">.8</span>.tar.gz &amp;&amp;/
tar zxvf libmcrypt-<span class="hljs-number">2.5</span><span class="hljs-number">.8</span>.tar.gz &amp;&amp;rm -f libmcrypt-<span class="hljs-number">2.5</span><span class="hljs-number">.8</span>.tar.gz &amp;&amp;/
cd libmcrypt-<span class="hljs-number">2.5</span><span class="hljs-number">.8</span> &amp;&amp; /
./configure --prefix=/usr/local &amp;&amp; /
make &amp;&amp; make install &amp;&amp;/
cd .. &amp;&amp; rm -rf libmcrypt-<span class="hljs-number">2.5</span><span class="hljs-number">.8</span>
<span class="hljs-comment"># 安装 PHP7</span>
RUN wget <span class="hljs-attribute">http</span>:<span class="hljs-regexp">//</span>cn2.php.net/get/php-<span class="hljs-number">7.0</span><span class="hljs-number">.17</span>.tar.gz/from/<span class="hljs-keyword">this</span>/mirror &amp;&amp; /
tar zxvf mirror &amp;&amp; rm -f php-<span class="hljs-number">7.0</span><span class="hljs-number">.17</span>.tar.gz &amp;&amp; rm -rf mirror &amp;&amp;/
cd php-<span class="hljs-number">7.0</span><span class="hljs-number">.17</span> &amp;&amp; /
./configure --prefix=/usr/local/webserver/php --<span class="hljs-reserved">with</span>-config-file-path=/usr/local/webserver/php/etc
--enable-fpm --<span class="hljs-reserved">with</span>-fpm-user=oae --<span class="hljs-reserved">with</span>-fpm-group=oae --<span class="hljs-reserved">with</span>-mysqli --<span class="hljs-reserved">with</span>-pdo-mysql
--<span class="hljs-reserved">with</span>-iconv-dir --<span class="hljs-reserved">with</span>-freetype-dir --<span class="hljs-reserved">with</span>-jpeg-dir --<span class="hljs-reserved">with</span>-png-dir --<span class="hljs-reserved">with</span>-zlib --<span class="hljs-reserved">with</span>-libxml-dir=/usr
--enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem
--enable-inline-optimization --<span class="hljs-reserved">with</span>-curl --enable-mbregex --enable-mbstring --enable-ftp --<span class="hljs-reserved">with</span>-gd
--enable-gd-<span class="hljs-reserved">native</span>-ttf --<span class="hljs-reserved">with</span>-openssl --<span class="hljs-reserved">with</span>-mhash --enable-pcntl --enable-sockets --<span class="hljs-reserved">with</span>-xmlrpc
--enable-zip --enable-soap --without-pear --<span class="hljs-reserved">with</span>-gettext --enable-fileinfo --enable-maintainer-zts &amp;&amp;
make clean &amp;&amp;make &amp;&amp; make install &amp;&amp; /
cp ./php.ini-development /usr/local/webserver/php/etc/php.ini &amp;&amp; /
cp ./sapi/fpm/init.d.php-fpm.<span class="hljs-keyword">in</span> /etc/init.d/php-fpm &amp;&amp;/
chmod <span class="hljs-number">755</span> /etc/init.d/php-fpm &amp;&amp; /
sed -i <span class="hljs-string">'$a chown -R oae:oae /var/run/'</span> /etc/init.d/php-fpm &amp;&amp; /
cd /data/software &amp;&amp; rm -rf php-<span class="hljs-number">7.0</span><span class="hljs-number">.17</span> &amp;&amp; /
sed -i <span class="hljs-string">'$aPATH=/usr/local/webserver/php/bin:/$PATH/n export PATH'</span> /etc/profile &amp;&amp;/
source /etc/profile &amp;&amp; /
mv /usr/local/webserver/php/etc/php-fpm.conf.<span class="hljs-reserved">default</span> /usr/local/webserver/php/etc/php-fpm.conf &amp;&amp;
/
sed -i <span class="hljs-string">'s!@sbindir@!/usr/local/webserver/php/sbin!g'</span> /etc/init.d/php-fpm &amp;&amp; /
sed -i <span class="hljs-string">'s!@sysconfdir@!/usr/local/webserver/php/etc!g'</span> /etc/init.d/php-fpm &amp;&amp; /
sed -i <span class="hljs-string">'s!@localstatedir@!/var!g'</span> /etc/init.d/php-fpm &amp;&amp; /
mv /usr/local/webserver/php/etc/php-fpm.d/www.conf.<span class="hljs-reserved">default</span>
/usr/local/webserver/php/etc/php-fpm.d/www.conf &amp;&amp; /
sed -i <span class="hljs-string">'s!127.0.0.1:9000!/var/run/php-fpm.sock!g'</span> /usr/local/webserver/php/etc/php-fpm.d/www.conf
&amp;&amp;/
ln -s /usr/local/webserver/php/bin/php /usr/local/bin/php
<span class="hljs-comment">#安装 PHP的Redis扩展</span>
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/phpredis.tar.gz &amp;&amp;tar zxvf phpredis.tar.gz
&amp;&amp; rm -rf phpredis.tar.gz &amp;&amp;cd phpredis &amp;&amp; /usr/local/webserver/php/bin/phpize &amp;&amp; ./configure
--<span class="hljs-reserved">with</span>-php-config=/usr/local/webserver/php/bin/php-config &amp;&amp;make &amp;&amp;make install &amp;&amp;sed -i <span class="hljs-string">'$a
extension_dir =/usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012//nextension
=redis.so/n'</span> /usr/local/webserver/php/etc/php.ini &amp;&amp; cd .. &amp;&amp;rm -rf phpredis
<span class="hljs-comment">#安装 PHP的mongodb扩展</span>
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/mongodb.so &amp;&amp; mv mongodb.so
<span class="hljs-regexp">/usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/</span> &amp;&amp; sed -i <span class="hljs-string">'$a extension
=mongodb.so'</span> /usr/local/webserver/php/etc/php.ini
<span class="hljs-comment">#安装 PHP的memcached扩展</span>
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/libmemcached-<span class="hljs-number">1.0</span><span class="hljs-number">.18</span>.tar.gz &amp;&amp;tar zxvf
libmemcached-<span class="hljs-number">1.0</span><span class="hljs-number">.18</span>.tar.gz &amp;&amp; rm -rf libmemcached-<span class="hljs-number">1.0</span><span class="hljs-number">.18</span>.tar.gz &amp;&amp;cd libmemcached-<span class="hljs-number">1.0</span><span class="hljs-number">.18</span>/
&amp;&amp;mkdir -p /usr/local/webserver/libmemcached &amp;&amp; ./configure
--prefix=<span class="hljs-regexp">/usr/local/webserver/libmemcached &amp;&amp;make &amp;&amp; make install &amp;&amp; cd ../</span> &amp;&amp; rm -rf
libmemcached-<span class="hljs-number">1.0</span><span class="hljs-number">.18</span>/ &amp;&amp; wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/memcached-<span class="hljs-number">3.0</span><span class="hljs-number">.2</span>.tgz
&amp;&amp;tar zxvf memcached-<span class="hljs-number">3.0</span><span class="hljs-number">.2</span>.tgz &amp;&amp; rm -rf memcached-<span class="hljs-number">3.0</span><span class="hljs-number">.2</span>.tgz &amp;&amp; cd memcached-<span class="hljs-number">3.0</span><span class="hljs-number">.2</span>/
&amp;&amp;/usr/local/webserver/php/bin/phpize &amp;&amp; ./configure --enable-memcached
--<span class="hljs-reserved">with</span>-php-config=/usr/local/webserver/php/bin/php-config
--<span class="hljs-reserved">with</span>-libmemcached-dir=/usr/local/webserver/libmemcached --disable-memcached-sasl &amp;&amp; make &amp;&amp;
make install &amp;&amp;cd ../ &amp;&amp; rm -rf memcached-<span class="hljs-number">3.0</span><span class="hljs-number">.2</span>/ &amp;&amp; sed -i <span class="hljs-string">'$a extension =memcached.so'</span>
/usr/local/webserver/php/etc/php.ini
<span class="hljs-comment">#安装gearman扩展</span>
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/gearmand.tar.gz &amp;&amp;tar zxvf gearmand.tar.gz
&amp;&amp; rm -rf gearmand.tar.gz &amp;&amp; cd gearmand &amp;&amp;./bootstrap.sh -a &amp;&amp;./configure &amp;&amp;make &amp;&amp; make
install &amp;&amp; cd .. &amp;&amp;rm -rf gearmand &amp;&amp;wget
<span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/gearman.so &amp;&amp;mv gearman.so
<span class="hljs-regexp">/usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/</span> &amp;&amp; sed -i <span class="hljs-string">'$a extension
=gearman.so'</span> /usr/local/webserver/php/etc/php.ini
<span class="hljs-comment">#安装zmq扩展</span>
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/libzmq.tar.gz &amp;&amp;tar zxvf libzmq.tar.gz &amp;&amp;
rm -rf libzmq.tar.gz &amp;&amp; cd libzmq &amp;&amp; ./autogen.sh &amp;&amp; ./configure &amp;&amp; make -j <span class="hljs-number">4</span> &amp;&amp;make check &amp;&amp;
make install &amp;&amp; ldconfig &amp;&amp;cd .. &amp;&amp;wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/zmq.so
&amp;&amp;mv zmq.so <span class="hljs-regexp">/usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/</span> &amp;&amp; rm -rf libzmq
&amp;&amp; sed -i <span class="hljs-string">'$a extension =zmq.so'</span> /usr/local/webserver/php/etc/php.ini
<span class="hljs-comment">#或</span>
<span class="hljs-comment">#wget https://pecl.php.net/get/zmq-1.1.3.tgz &amp;&amp; rm -rf zmq-1.1.3.tgz &amp;&amp; /</span>
<span class="hljs-comment">#cd zmq-1.1.3 &amp;&amp; /usr/local/webserver/php/bin/phpize &amp;&amp;/</span>
<span class="hljs-comment">#./configure --prefix=/usr/local/webserver/zmq</span>
--<span class="hljs-reserved">with</span>-php-config=/usr/local/webserver/php/bin/php-config &amp;&amp;/
<span class="hljs-comment"># make &amp;&amp; make install &amp;&amp;/</span>
<span class="hljs-comment">#cd pecl-gearman-master</span>
<span class="hljs-comment">#/usr/local/webserver/php/bin/phpize</span>
<span class="hljs-comment">#./configure --prefix=/usr/local/webserver/gearman</span>
--<span class="hljs-reserved">with</span>-php-config=/usr/local/webserver/php/bin/php-config
<span class="hljs-comment">#安装php的ice扩展</span>
RUN cd <span class="hljs-regexp">/data/software &amp;&amp; wget https:/</span>/gitee.com/hanlicun/ltmp/raw/master/src/IcePHP.so &amp;&amp;mv
IcePHP.so <span class="hljs-regexp">/usr/local/webserver/php/lib/php/extensions/no-debug-zts-20151012/</span> &amp;&amp; wget
<span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/Ice-<span class="hljs-number">3.6</span><span class="hljs-number">.4</span>.tar.gz &amp;&amp;tar zxvf Ice-<span class="hljs-number">3.6</span><span class="hljs-number">.4</span>.tar.gz &amp;&amp; mv
Ice-<span class="hljs-number">3.6</span><span class="hljs-number">.4</span> /opt/ &amp;&amp; rm -rf Ice-<span class="hljs-number">3.6</span><span class="hljs-number">.4</span>.tar.gz&amp;&amp; sed -i <span class="hljs-string">'$a extension =IcePHP.so'</span>
/usr/local/webserver/php/etc/php.ini &amp;&amp; sed -i <span class="hljs-string">'$a export LD_LIBRARY_PATH=/opt/Ice-3.6.4/lib64'</span>
/etc/profile
<span class="hljs-comment">#Install Composer</span>
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/composer &amp;&amp;mv composer
/usr/local/bin/composer &amp;&amp; chmod +x /usr/local/bin/composer &amp;&amp; /usr/local/bin/composer config -g
repo.packagist composer <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//</span>packagist.phpcomposer.com
RUN chmod -R <span class="hljs-number">777</span> /usr<span class="hljs-regexp">/local/webserver/php/var/log/</span> &amp;&amp; chown -R <span class="hljs-attribute">oae</span>:oae <span class="hljs-regexp">/var/run/</span> &amp;&amp; /
mkdir -p <span class="hljs-regexp">/home/oae/web/wwwroot/public/</span> &amp;&amp; /
touch /home/oae/web/wwwroot/public/index.html &amp;&amp; /
echo ok &gt; /home/oae/web/wwwroot/public/index.html &amp;&amp;/
echo <span class="hljs-string">"&lt;?php phpinfo();?&gt;"</span> &gt; /home/oae/web/wwwroot/public/index.php
<span class="hljs-comment">#开放端口</span>
EXPOSE <span class="hljs-number">80</span> <span class="hljs-number">22</span>
<span class="hljs-comment"># 安装 supervisord</span>
RUN easy_install supervisor &amp;&amp; /
mkdir -p /usr/local/<span class="hljs-reserved">var</span>/log/supervisord
RUN wget <span class="hljs-attribute">https</span>:<span class="hljs-regexp">//gi</span>tee.com/hanlicun/ltmp/raw/master/src/supervisord.conf &amp;&amp;/
mv supervisord.conf /etc/supervisord.conf &amp;&amp; /
mkdir -p /usr/local/<span class="hljs-reserved">var</span>/run
CMD [<span class="hljs-string">"/usr/bin/supervisord"</span>,<span class="hljs-string">"-c"</span>, <span class="hljs-string">"/etc/supervisord.
</span></code>

构建docker容器

在Dockerfile当前目录下执行

<code>docker build <span class="hljs-attribute">-t</span> lnmp <span class="hljs-built_in">.</span></code>

然后运行启动一个docker容器

<code>docker run <span class="hljs-attribute">-d</span> <span class="hljs-attribute">-p</span> <span class="hljs-number">88</span>:<span class="hljs-number">80</span> <span class="hljs-attribute">-p</span> <span class="hljs-number">1022</span>:<span class="hljs-number">22</span> lnmp</code>

通过ssh登陆容器内

<code>ssh root<span class="hljs-variable">@127</span>.<span class="hljs-number">0</span>.<span class="hljs-number">0</span>.<span class="hljs-number">1</span><span class="hljs-symbol">:</span><span class="hljs-number">1022</span> -p</code>

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

从registry中拉取镜像:

<code>docker pull registry<span class="hljs-preprocessor">.cn</span>-zhangjiakou<span class="hljs-preprocessor">.aliyuncs</span><span class="hljs-preprocessor">.com</span>/hanlc/lnmp</code>

未经允许不得转载:冰点网络 » 基于Dockerfile构建LNMP环境(php7+mysql+nginx) –原创

分享到:更多 ()

评论 抢沙发

评论前必须登录!