构建自己的 lnp 镜像文件
本文记录使用 Dockerfile 构建自己的lnp过程,没有数据库(需要加上数据库的也可以加上,过程类似) 本次构建使用编译安装,原因是想要学一下 首先分享我本次构建用到的资源 my-lnmp.zip
my-lnmp.zip
my-lnp8.zip # php8.1
my-lnp7.zip # php7.4 两个文件仅在php版本上有差异
然后放出Dockerfile 文件,Dockerfile有两个 分别是
layered.DockerFile
1FROM debian:11.1 2ADD ./resource.tar.gz /tmp 3COPY ./pre-operation.sh tmp/pre-operation.sh 4RUN sh /tmp/pre-operation.sh 5COPY ./install-php-rely.sh /tmp/install-php-rely.sh 6RUN sh /tmp/install-php-rely.sh 7COPY ./install-php.sh /tmp/install-php.sh 8RUN sh /tmp/install-php.sh 9COPY ./install-nginx-rely.sh /tmp/install-nginx-rely.sh 10RUN sh /tmp/install-nginx-rely.sh 11COPY ./install-nginx.sh /tmp/install-nginx.sh 12RUN sh /tmp/install-nginx.sh 13COPY ./install-composer.sh /tmp/install-composer.sh 14RUN sh /tmp/install-composer.sh 15COPY ./config.sh /tmp/config.sh 16RUN sh /tmp/config.sh 17EXPOSE 80 18LABEL wanQQ="w_c_y_929@163.com" 19WORKDIR /usr/share/nginx/html/public 20CMD ["/home/run.sh"]
polymeric.DockerFile
1FROM debian:11.1 2ADD ./resource.tar.gz /tmp 3RUN sh /tmp/build.sh 4EXPOSE 80 5LABEL wanQQ="w_c_y_929@163.com" 6WORKDIR /usr/share/nginx/html/public 7CMD ["/home/run.sh"]
- polymeric.DockerFile 和 layered.DockerFile 构建出的镜像大小类似

- 但是 polymeric.DockerFile 构建出的镜像层数比 layered.DockerFile 少,见仁见智吧
- my-lnp8.zip 和 my-lnp7.zip 中均有 resource.tar.gz 文件 内容仅有少许差异,这里只列举 my-lnp8.zip 中的resource.tar.gz 文件
resource.tar.gz 文件中包含
- layered.DockerFile # 多层构建的 DockerFile 文件
- polymeric.DockerFile # 紧凑构建的 DockerFile 文件
- run.sh # 镜像每次启动执行的 shell 放到 /home 目录
- build.sh # 最终构建镜像时运行的shell命令(暂时不必理会),放到 /tmp 目录
- sources.list # 阿里云镜像源文件在 此处 查看
- php-8.1.0.tar.xz # PHP源码文件 在此处找到并下载想要安装的 PHP 版本
- nginx-1.21.4.tar.gz # nginx源码文件在此处找到并下载想要安装的 nginx 版本
- default.conf # nginx 配置文件(会被 nginx.conf 引用)
- nginx.conf # nginx 配置文件
- nginx # 使 nginx 开机启动的脚本文件
- pre-operation.sh # 最先在 Dockerfile 运行的脚本
- install-php-rely.sh <br># 安装 编译 PHP 需要的依赖(这些依赖根据编译 PHP 时的扩展设置变化,需要自行判断需要那些依赖)
- install-php.sh # 编译安装 PHP
- install-nginx-rely.sh <br># 安装 编译 Nginx 需要的依赖(这些依赖根据编译 Nginx 时的扩展设置变化,需要自行判断需要那些依赖)
- install-nginx.sh <br># 编译安装 Nginx 由于部分依赖已经在 install-php-rely.sh 安装所以在 install-nginx-rely.sh 中没有重复安装 所以本文件需要在 install-php-rely.sh 和 install-nginx-rely.sh 都执行完以后执行
- install-composer.sh # 安装 composer
- config.sh # 对 php 和 nginx 进行配置
- 依赖的安装随着各系统和配置的不同会有所差异,在执行 configure 时会根据参数的不同编译不同的扩展,如果有缺失依赖的扩展将会报错,根据报错安装对应的包即可,需要注意的是,configure 时报错提示的包名和在软件源中的包名不一定一样,需要根据自己的系统在搜索引擎中搜索
- 下面看每个 shell 文件的内容和作用
pre-operation.sh
1#! /bin/bash 2# 换源 执行部分前置操作 3rm /etc/apt/sources.list #删除原来的apt源 4cp /tmp/sources.list /etc/apt/sources.list #安装提前准备好的软件源 5cp /tmp/run.sh /home/run.sh # 将运行文件放到 /home 6cd /tmp 7tar zxvf php-8.1.0.tar.gz 8tar zxvf nginx-1.21.4.tar.gz 9chmod +x /home/run.sh # 给 此文件执行权限 10groupadd nobody 11# 创建 nginx 用户 12groupadd nginx 13useradd -g nginx -s /sbin/nologin nginx 14apt update && apt upgrade -y && apt-get update && apt-get upgrade -y 15apt-get install -y apt-transport-https ca-certificates sysv-rc-conf vim wget unzip tmux redis php-pear #安装一些自己可能会用到的软件,自行选择 16
- 这一步主要做一些准备工作,将所有的资源解压到 /tmp 文件夹
- 更换软件源(这里是阿里的 sources.list 可以自行准备其他的国内源,不想换源也可以不换源)
- 创建 nginx 用户 如果这里不创建 nginx用户,在 nginx 编译安装时需要去除对应的配置
- 安装一些自己想用的软件 自行选择 阿里 sources.list 内容
1deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib 2deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib 3deb http://mirrors.aliyun.com/debian-security/ bullseye-security main 4deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main 5deb http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib 6deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib 7deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib 8deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib 9
install-php-rely.sh
1#! /bin/bash 2# 安装编译 PHP 所需依赖 3apt update && apt upgrade -y && apt-get update && apt-get upgrade -y 4apt-get -y install build-essential autoconf automake libtool make re2c bison pkg-config libxml2-dev openssl libssl-dev libcurl4-openssl-dev libsqlite3-dev libonig-dev zlib1g-dev # 安装 PHP 依赖 5#获取 redis 扩展 6cd /tmp/php-8.1.0/ext/ 7pecl channel-update pecl.php.net 8pecl download redis 9redis=$(ls -l | grep redis | awk '{print $9}') 10gzip -d < $redis | tar -xvf - 11rm $redis 12redis=$(ls -l | grep redis | awk '{print $9}') 13mv $redis redis 14cd /tmp/php-8.1.0/ 15rm configure 16./buildconf --force
- 安装依赖
install-php.sh
1#! /bin/bash 2# 编译安装 PHP 3 4# 进入 /tmp/php-8.1.0/ 目录, php 源码解压到了这里 5cd /tmp/php-8.1.0/ 6 7# 设置 8./configure --prefix=/usr/local/php \ 9--with-config-file-path=/usr/local/php/etc \ 10--enable-fpm \ 11--enable-mbstring \ 12--with-curl \ 13--with-mysqli \ 14--enable-bcmath \ 15--with-openssl \ 16--with-zlib \ 17--enable-redis 18 19# 编译 && 安装 20make && make install 21 22#注册环境变量 23ln -s /usr/local/php/bin/php /usr/local/bin/php 24ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/php-fpm
-首先进行配置,这里的扩展项我是根据 Tp 和 Laravel 的要求所选择的,有其他需求的自行选择
- 编译 && 安装 注册环境变量 没什么可说的
install-nginx-rely.sh
1 #! /bin/bash 2# 安装编译 Nginx 所需依赖(部分依赖已在PHP依赖中安装,不再重复安装) 3apt update && apt upgrade -y && apt-get update && apt-get upgrade -y 4apt-get -y install libpcre3 libpcre3-dev libgd-dev #安装 nginx 依赖
- 安装依赖,此文件中的依赖并不全,原因是在安装 PHP 依赖时一部分依赖已经被安装了
install-nginx.sh
1#! /bin/bash 2# 编译安装 Nginx 3 4# 进入 /tmp/nginx-1.21.4 目录, Nginx 源码解压到了这里 5cd /tmp/nginx-1.21.4/ 6 7# 设置 8./configure --prefix=/usr/local/nginx \ 9--user=nginx \ 10--group=nginx \ 11--with-pcre \ 12--with-http_ssl_module \ 13--with-http_v2_module \ 14--with-http_realip_module \ 15--with-http_addition_module \ 16--with-http_sub_module \ 17--with-http_dav_module \ 18--with-http_flv_module \ 19--with-http_mp4_module \ 20--with-http_gunzip_module \ 21--with-http_gzip_static_module \ 22--with-http_random_index_module \ 23--with-http_secure_link_module \ 24--with-http_stub_status_module \ 25--with-http_auth_request_module \ 26--with-http_image_filter_module \ 27--with-http_slice_module \ 28--with-mail \ 29--with-threads \ 30--with-file-aio \ 31--with-stream \ 32--with-mail_ssl_module \ 33--with-stream_ssl_module 34 35# 编译 && 安装 36make && make install 37 38#注册环境变量 39ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
-首先进行配置,这里的扩展项我是根据别人的文章里配置的,有其他需求的自行选择
- 如果前面没有创建 nginx 用户 那么 --user=nginx --group=nginx 配置项需要去掉
- 编译 && 安装 注册环境变量 没什么可说的
install-composer.sh
1#! /bin/bash 2 3# 安装composer 4cd /tmp 5php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" 6php composer-setup.php 7mv composer.phar /usr/local/bin/composer 8## 换源 9composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
- 来自 Packagist / Composer 中国全量镜像 和 阿里云 Composer 全量镜像 没什么好讲的
config.sh
1#! /bin/bash 2#配置 配置文件 3##php 4cp /tmp/php-8.1.0/php.ini-production /usr/local/php/php.ini 5cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 6cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf 7cp /tmp/php-8.1.0/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 8chmod +x /etc/init.d/php-fpm 9chmod 777 /etc/init.d/php-fpm 10sysv-rc-conf php-fpm on #开机自启 11 12##nginx 13mkdir -p /usr/share/nginx/html/public 14cp /tmp/index.php /usr/share/nginx/html/public/index.php 15cp /tmp/default.conf /usr/local/nginx/conf/default.conf 16cp /tmp/nginx.conf /usr/local/nginx/conf/nginx.conf 17cp /tmp/nginx /etc/init.d/nginx 18chmod +x /etc/init.d/nginx 19chmod 777 /etc/init.d/nginx 20sysv-rc-conf nginx on #开机自启 21
- 对 php 和 nginx 进行配置,这一块我也比较迷糊
- 配置的开机自启似乎并没有作用,所以我是使用 CMD ["/home/run.sh"] 来实现的,有懂的大佬可以给我留言
run.sh
1#! /bin/bash 2php-fpm 3nginx 4redis-server 5
- 用于启动php-fpm nginx 和 redis-server 可自行修改
build.sh
1#! /bin/bash 2 3#sh /tmp/pre-operation.sh 4 5# 换源 执行部分前置操作 6rm /etc/apt/sources.list #删除原来的apt源 7cp /tmp/sources.list /etc/apt/sources.list #安装提前准备好的软件源 8cp /tmp/run.sh /home/run.sh # 将运行文件放到 /home 9cd /tmp 10tar zxvf php-8.1.0.tar.gz 11tar zxvf nginx-1.21.4.tar.gz 12chmod +x /home/run.sh # 给 此文件执行权限 13groupadd nobody 14# 创建 nginx 用户 15groupadd nginx 16useradd -g nginx -s /sbin/nologin nginx 17apt update && apt upgrade -y && apt-get update && apt-get upgrade -y 18apt-get install -y apt-transport-https ca-certificates sysv-rc-conf vim wget unzip tmux redis php-pear #安装一些自己可能会用到的软件,自行选择 19 20 21 22#sh /tmp/install-php-rely.sh 23 24# 安装编译 PHP 所需依赖 25apt update && apt upgrade -y && apt-get update && apt-get upgrade -y 26apt-get -y install build-essential autoconf automake libtool make re2c bison pkg-config libxml2-dev openssl libssl-dev libcurl4-openssl-dev libsqlite3-dev libonig-dev zlib1g-dev # 安装 PHP 依赖 27#获取 redis 扩展 28cd /tmp/php-8.1.0/ext/ 29pecl channel-update pecl.php.net 30pecl download redis 31redis=$(ls -l | grep redis | awk '{print $9}') 32gzip -d < $redis | tar -xvf - 33rm $redis 34redis=$(ls -l | grep redis | awk '{print $9}') 35mv $redis redis 36cd /tmp/php-8.1.0/ 37rm configure 38./buildconf --force 39 40#sh /tmp/install-php.sh 41 42# 编译安装 PHP 43 44# 进入 /tmp/php-8.1.0/ 目录, php 源码解压到了这里 45cd /tmp/php-8.1.0/ 46 47# 设置 48./configure --prefix=/usr/local/php \ 49--with-config-file-path=/usr/local/php/etc \ 50--enable-fpm \ 51--enable-mbstring \ 52--with-curl \ 53--with-mysqli \ 54--enable-bcmath \ 55--with-openssl \ 56--with-zlib \ 57--enable-redis 58 59# 编译 && 安装 60make && make install 61 62#注册环境变量 63ln -s /usr/local/php/bin/php /usr/local/bin/php 64ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/php-fpm 65 66#sh /tmp/install-nginx-rely.sh 67 68# 安装编译 Nginx 所需依赖(部分依赖已在PHP依赖中安装,不再重复安装) 69apt update && apt upgrade -y && apt-get update && apt-get upgrade -y 70apt-get -y install libpcre3 libpcre3-dev libgd-dev #安装 nginx 依赖 71 72#sh /tmp/install-nginx.sh 73 74# 编译安装 Nginx 75 76# 进入 /tmp/nginx-1.21.4 目录, Nginx 源码解压到了这里 77cd /tmp/nginx-1.21.4/ 78 79# 设置 80./configure --prefix=/usr/local/nginx \ 81--user=nginx \ 82--group=nginx \ 83--with-pcre \ 84--with-http_ssl_module \ 85--with-http_v2_module \ 86--with-http_realip_module \ 87--with-http_addition_module \ 88--with-http_sub_module \ 89--with-http_dav_module \ 90--with-http_flv_module \ 91--with-http_mp4_module \ 92--with-http_gunzip_module \ 93--with-http_gzip_static_module \ 94--with-http_random_index_module \ 95--with-http_secure_link_module \ 96--with-http_stub_status_module \ 97--with-http_auth_request_module \ 98--with-http_image_filter_module \ 99--with-http_slice_module \ 100--with-mail \ 101--with-threads \ 102--with-file-aio \ 103--with-stream \ 104--with-mail_ssl_module \ 105--with-stream_ssl_module 106 107# 编译 && 安装 108make && make install 109 110#注册环境变量 111ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx 112 113 114#sh /tmp/install-composer.sh 115 116# 安装composer 117cd /tmp 118php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" 119php composer-setup.php 120mv composer.phar /usr/local/bin/composer 121## 换源 122composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ 123 124#sh /tmp/config.sh 125 126 127#配置 配置文件 128##php 129cp /tmp/php-8.1.0/php.ini-production /usr/local/php/php.ini 130cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf 131cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf 132cp /tmp/php-8.1.0/sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 133chmod +x /etc/init.d/php-fpm 134chmod 777 /etc/init.d/php-fpm 135sysv-rc-conf php-fpm on #开机自启 136 137##nginx 138mkdir -p /usr/share/nginx/html/public 139cp /tmp/index.php /usr/share/nginx/html/public/index.php 140cp /tmp/default.conf /usr/local/nginx/conf/default.conf 141cp /tmp/nginx.conf /usr/local/nginx/conf/nginx.conf 142cp /tmp/nginx /etc/init.d/nginx 143chmod +x /etc/init.d/nginx 144chmod 777 /etc/init.d/nginx 145sysv-rc-conf nginx on #开机自启
-
不再重复解释,仅是把前面的文件整合到一起
-
参考文章:
Debian/Ubuntu下使用sysv-rc-conf将NGINX设置为自启动 - VPS推荐 (vpstry.com)
编译安装php+nginx详细步骤 - ranblogs - 博客园 (cnblogs.com)
