蓝易云

Docker 制作 nginx-php 镜像 – 可以根据参数构建不同版本

427次阅读
2条评论

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

一、需求

需求是制作一个能处理 php 脚本文件的 nginx 服务器,通过传递不同的 nginx 和 php 版本进行相应的构建。

二、实战

1、创建所需 nginx 配置文件

touch nginx.conf && cat > nginx.conf <<EOF
#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       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  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /var/www/html/;
            index  index.html index.htm;
        }

        location ~ \.php$ {
            root html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            #fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
            fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name;
            include        fastcgi_params;
        }
        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}
EOF

2、同级别目录下创建 dockerfile 文件

FROM ubuntu:20.04

LABEL AUTHOR="www.amjun.com" EMAIL="1532246395@qq.com"

ARG DEBIAN_FRONTEND=noninteractive \
    TZ=Asia/Shanghai \
    PHP_VERSION="7.4.29" \
    NGINX_VERSION="1.19.0"

RUN sed -i "s/archive.ubuntu.com/mirrors.aliyun.com/g" /etc/apt/sources.list && apt update && \
    apt install -y wget make gcc libpcre3 libpcre3-dev zlib1g zlib1g-dev openssl  libssl-dev \
    libzip-dev bison autoconf build-essential pkg-config git-core \
    libltdl-dev libbz2-dev libxml2-dev libxslt1-dev libssl-dev libicu-dev libpspell-dev \
    libenchant-dev libmcrypt-dev libpng-dev libjpeg8-dev libfreetype6-dev libmysqlclient-dev \
    libreadline-dev libcurl4-openssl-dev librecode-dev libsqlite3-dev libonig-dev php-pear

RUN wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz && \
    tar -zxvf nginx-${NGINX_VERSION}.tar.gz  -C /tmp && \
    useradd -s /sbin/nologin nginx && \
    mkdir -p /usr/local/nginx && cd /tmp/nginx-${NGINX_VERSION} && \
    ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-stream --with-http_ssl_module --with-http_stub_status_module && \
    make && make install && \
    wget https://www.php.net/distributions/php-${PHP_VERSION}.tar.gz && \
    tar -zxvf php-${PHP_VERSION}.tar.gz -C /tmp && \
    cd /tmp/php-${PHP_VERSION} && \
    ./configure --prefix=/usr/local/php7 --with-config-file-scan-dir=/usr/local/php7/etc/php.d --with-config-file-path=/usr/local/php7/etc --enable-fpm && \
    make && make install && \
    groupadd nobody && \
    echo "${TZ}" > /etc/timezone && \
    ln -sf /usr/share/zoneinfo/${TZ} /etc/localtime && \
    cp /usr/local/php7/etc/php-fpm.conf.default /usr/local/php7/etc/php-fpm.conf && \
    cp /usr/local/php7/etc/php-fpm.d/www.conf.default /usr/local/php7/etc/php-fpm.d/www.conf && \
    cp /tmp/php-${PHP_VERSION}/php.ini-production /usr/local/php7/etc/php.ini && \
    rm -rf /tmp/* && rm -rf nginx-${NGINX_VERSION}.tar.gz && rm -rf php-${PHP_VERSION}.tar.gz && \
    rm -rf /var/lib/apt/lists/*

COPY nginx.conf /usr/local/nginx/conf/nginx.conf

ENV PATH="/usr/local/php7/bin:/usr/local/php7/sbin:/usr/local/nginx/sbin:${PATH}"

WORKDIR /var/www/html/

CMD php-fpm && nginx -g "daemon off;"

3、构建镜像

docker build -t nginx-php --build-arg NGINX_VERSION="1.19.1"  --build-arg PHP_VERSION="7.4.30" .

nginx 配置文件里面配置的网站根目录为 /var/www/html,只需要把网站放在该目录下即可。

提醒:本文发布于830天前,文中所关联的信息可能已发生改变,请知悉!

AD:【腾讯云服务器大降价】2核4G 222元/3年 1核2G 38元/年
正文完
 0
阿蛮君
版权声明:本站原创文章,由 阿蛮君 于2022-08-31发表,共计3149字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(2条评论)
shaun 评论达人 LV.1
2022-08-31 10:36:44 回复

这么早就发新内容呢

 Windows  Chrome  中国江西省南昌市电信
Copyright © 2022-2024 阿蛮君博客 湘ICP备2023001393号
本网站由 亿信互联 提供云计算服务 | 蓝易云CDN 提供安全防护和加速服务
Powered by Wordpress  Theme by Puock