docker(17):使用docker构建nginx+phpfpm环境2

来源:互联网 发布:java干货知识点分享 编辑:程序博客网 时间:2024/06/05 12:46

本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/77082783 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

1,关于php


由于php的官方没有nginx + php的方案,所以需要自己构建一个nginx+php的环境。
php的官方有php-fpm的镜像,并且是alpine的,很小。68.68 MB
增加nginx ,和其他相关的模块之后的大小就是125 MB了,还是可以接受的。

做好基础镜像之后,就可以启动php程序了,将php文件夹直接映射到 /var/www/html 目录即可。

2,构建php-fpm & nginx镜像


首先piwik的官方docker是有fpm版本的,也有apache的版本,就是没有nginx的版本。
并且镜像都比较大,我觉得使用alpine之后,大小在100MB最好。
构建跑起来都非常的快。

使用的基础镜像是 docker.io/php:fpm-alpine 。php官网的alpine版本。

然后在这个上面构建nginx。配置 alpine的镜像:

http://mirrors.aliyun.com/alpine/v3.6/mainhttp://mirrors.aliyun.com/alpine/v3.6/community

这样在使用apk add lib包的时候速度快很多。

使用 NGINX_VERSION 1.13.2 进行代码的构建。

php-ext 安装插件:pdo_mysql mysqli iconv mcrypt 是直接安装的。
而gd需要configure 配置freetypedir,再进行安装才行。
否则在piwik中就显示不了曲线的小图了。

docker-php-ext-install pdo_mysql mysqli iconv mcrypt && \    docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \    docker-php-ext-install gd

3,全部dockerfile 文件


如下:

FROM docker.io/php:fpm-alpineRUN echo -e "http://mirrors.aliyun.com/alpine/v3.6/main\n\http://mirrors.aliyun.com/alpine/v3.6/community" > /etc/apk/repositories#set timezoneRUN apk update && apk add tzdata protobuf && \    ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \    echo "Asia/Shanghai" > /etc/timezone && \    rm -rf /root/.cacheENV php_conf /usr/local/etc/php-fpm.confENV fpm_conf /usr/local/etc/php-fpm.d/www.confENV php_vars /usr/local/etc/php/conf.d/docker-vars.iniENV NGINX_VERSION 1.13.2RUN CONFIG="\    --prefix=/etc/nginx \    --sbin-path=/usr/sbin/nginx \    --modules-path=/usr/lib/nginx/modules \    --conf-path=/etc/nginx/nginx.conf \    --error-log-path=/var/log/nginx/error.log \    --http-log-path=/var/log/nginx/access.log \    --pid-path=/var/run/nginx.pid \    --lock-path=/var/run/nginx.lock \    --http-client-body-temp-path=/var/cache/nginx/client_temp \    --http-proxy-temp-path=/var/cache/nginx/proxy_temp \    --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp \    --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp \    --http-scgi-temp-path=/var/cache/nginx/scgi_temp \    --user=nginx \    --group=nginx \    --with-http_ssl_module \    --with-http_realip_module \    --with-http_addition_module \    --with-http_sub_module \    --with-http_dav_module \    --with-http_flv_module \    --with-http_mp4_module \    --with-http_gunzip_module \    --with-http_gzip_static_module \    --with-http_random_index_module \    --with-http_secure_link_module \    --with-http_stub_status_module \    --with-http_auth_request_module \    --with-http_xslt_module=dynamic \    --with-http_image_filter_module=dynamic \    --with-http_geoip_module=dynamic \    --with-http_perl_module=dynamic \    --with-threads \    --with-stream \    --with-stream_ssl_module \    --with-stream_ssl_preread_module \    --with-stream_realip_module \    --with-stream_geoip_module=dynamic \    --with-http_slice_module \    --with-mail \    --with-mail_ssl_module \    --with-compat \    --with-file-aio \    --with-http_v2_module \  " \  && addgroup -S nginx \  && adduser -D -S -h /var/cache/nginx -s /sbin/nologin -G nginx nginx \  && apk add --no-cache --virtual .build-deps \    gcc \    libc-dev \    make \    openssl-dev \    pcre-dev \    zlib-dev \    linux-headers \    curl \    gnupg \    libxslt-dev \    gd-dev \    geoip-dev \    perl-dev \  && curl -fSL http://nginx.org/download/nginx-$NGINX_VERSION.tar.gz -o nginx.tar.gz \  && export GNUPGHOME="$(mktemp -d)" \  && rm -r "$GNUPGHOME" \  && mkdir -p /usr/src \  && tar -zxC /usr/src -f nginx.tar.gz \  && rm nginx.tar.gz \  && cd /usr/src/nginx-$NGINX_VERSION \  && ./configure $CONFIG --with-debug \  && make -j$(getconf _NPROCESSORS_ONLN) \  && mv objs/nginx objs/nginx-debug \  && mv objs/ngx_http_xslt_filter_module.so objs/ngx_http_xslt_filter_module-debug.so \  && mv objs/ngx_http_image_filter_module.so objs/ngx_http_image_filter_module-debug.so \  && mv objs/ngx_http_geoip_module.so objs/ngx_http_geoip_module-debug.so \  && mv objs/ngx_http_perl_module.so objs/ngx_http_perl_module-debug.so \  && mv objs/ngx_stream_geoip_module.so objs/ngx_stream_geoip_module-debug.so \  && ./configure $CONFIG \  && make -j$(getconf _NPROCESSORS_ONLN) \  && make install \  && rm -rf /etc/nginx/html/ \  && mkdir /etc/nginx/conf.d/ \  && mkdir -p /usr/share/nginx/html/ \  && install -m644 html/index.html /usr/share/nginx/html/ \  && install -m644 html/50x.html /usr/share/nginx/html/ \  && install -m755 objs/nginx-debug /usr/sbin/nginx-debug \  && install -m755 objs/ngx_http_xslt_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_xslt_filter_module-debug.so \  && install -m755 objs/ngx_http_image_filter_module-debug.so /usr/lib/nginx/modules/ngx_http_image_filter_module-debug.so \  && install -m755 objs/ngx_http_geoip_module-debug.so /usr/lib/nginx/modules/ngx_http_geoip_module-debug.so \  && install -m755 objs/ngx_http_perl_module-debug.so /usr/lib/nginx/modules/ngx_http_perl_module-debug.so \  && install -m755 objs/ngx_stream_geoip_module-debug.so /usr/lib/nginx/modules/ngx_stream_geoip_module-debug.so \  && ln -s ../../usr/lib/nginx/modules /etc/nginx/modules \  && strip /usr/sbin/nginx* \  && strip /usr/lib/nginx/modules/*.so \  && rm -rf /usr/src/nginx-$NGINX_VERSION \  \  # Bring in gettext so we can get `envsubst`, then throw  # the rest away. To do this, we need to install `gettext`  # then move `envsubst` out of the way so `gettext` can  # be deleted completely, then move `envsubst` back.  && apk add --no-cache --virtual .gettext gettext \  && mv /usr/bin/envsubst /tmp/ \  \  && runDeps="$( \    scanelf --needed --nobanner /usr/sbin/nginx /usr/lib/nginx/modules/*.so /tmp/envsubst \      | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \      | sort -u \      | xargs -r apk info --installed \      | sort -u \  )" \  && apk add --no-cache --virtual .nginx-rundeps $runDeps \  && apk del .build-deps \  && apk del .gettext \  && mv /tmp/envsubst /usr/local/bin/ \  \  # forward request and error logs to docker log collector  && ln -sf /dev/stdout /var/log/nginx/access.log \  && ln -sf /dev/stderr /var/log/nginx/error.logCOPY nginx.conf /etc/nginx/nginx.confCOPY default.conf /etc/nginx/conf.d/default.conf# https://github.com/docker-library/docs/tree/master/php# PHP Core ExtensionsRUN apk update && apk add libpng-dev freetype-dev libjpeg-turbo-dev libmcrypt-dev && \    docker-php-ext-install pdo_mysql mysqli iconv mcrypt && \    docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ && \    docker-php-ext-install gdEXPOSE 80ADD docker-run.sh /usr/local/bin/ENTRYPOINT ["docker-run.sh"]

其中docker-run.sh 内容:需要一次启动php-fpm和nginx两个服务。

#!/bin/shnohup php-fpm > /var/log/php-fpm.log 2>&1 &nginx -g "daemon off;"

nginx.conf配置文件内容:

user  nginx;worker_processes  2;error_log  /var/log/nginx/error.log warn;pid        /var/run/nginx.pid;events {    use epoll;    worker_connections  1024;}http {    include       /etc/nginx/mime.types;    default_type  application/octet-stream;    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '                      '$status $body_bytes_sent "$http_referer" '                      '"$http_user_agent" "$http_x_forwarded_for"';    access_log  /var/log/nginx/access.log  main;    sendfile        on;    #tcp_nopush     on;    keepalive_timeout  65;    #gzip  on;    include /etc/nginx/conf.d/*.conf;}

其中default.conf 的配置文件,其中需要制定php通过fpm解析。其他的资源通过nginx解析。
必须将nginx和php-fpm放到一个docker镜像下面才行。

server {    listen       80;    server_name  localhost;    index index.php index.html;    root /var/www/html;    access_log  /var/log/nginx/host.80.access.log  main;    error_log  /var/log/nginx/host.80.error.log;    location ~ \.php$ {        root           /var/www/html;        fastcgi_pass   127.0.0.1:9000;        try_files $uri /index.php =404;        fastcgi_split_path_info ^(.+\.php)(/.+)$;        fastcgi_index  index.php;        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;        include        fastcgi_params;    }    location ~* ^.+\.(?:css|gif|html?|jpe?g|js|png|swf|svg|ico|bmp|gif|htm?|html|ttf|woff)$ {            root           /var/www/html;            expires 30d;            tcp_nodelay off;            ## Set the OS file cache.            open_file_cache max=1000 inactive=120s;            open_file_cache_valid 45s;            open_file_cache_min_uses 2;            open_file_cache_errors off;     }}

其中php目录在 /var/www/html 下面。启动docker 将磁盘映射即可。

4,总结


还是有很多的项目都是使用php进行开发的。
php官方是没有nginx+phpfpm 这种组合的。自己搭建一个环境,比之前的要在centos进行安装要方便多了,而且排除了不同操作系统版本的问题。

做好的镜像通过映射不同的磁盘可以复用,使用docker真方便。

本文的原文连接是: http://blog.csdn.net/freewebsys/article/details/77082783 未经博主允许不得转载。
博主地址是:http://blog.csdn.net/freewebsys

原创粉丝点击