Build your own LNP image file

This article records the process of using Dockerfile to build their own LNP, no database (need to add a database can also be added, the process is similar) this build uses compilation installation, because I want to learn

First, share the resource My-lnmp.zip that I used for this build

my-lnmp.zip

My – lnp8.zip # php8.1

My – lnp7.zip # php7.4

The two files differ only in the PHP version

And then it releases the Dockerfile file, and the Dockerfile has two different names

layered.DockerFile

FROM Debian :11.1 ADD./ resource-tar. gz/TMP COPY./pre-operation.sh TMP /pre-operation.sh RUN sh/TMP /pre-operation.sh COPY ./install-php-rely.sh /tmp/install-php-rely.sh RUN sh /tmp/install-php-rely.sh COPY ./install-php.sh /tmp/install-php.sh RUN sh /tmp/install-php.sh COPY ./install-nginx-rely.sh /tmp/install-nginx-rely.sh RUN sh /tmp/install-nginx-rely.sh COPY ./install-nginx.sh /tmp/install-nginx.sh RUN sh /tmp/install-nginx.sh COPY ./install-composer.sh /tmp/install-composer.sh RUN sh /tmp/install-composer.sh COPY ./config.sh /tmp/config.sh RUN sh /tmp/config.sh EXPOSE 80 LABEL wanQQ="[email protected]" WORKDIR /usr/share/nginx/html/public CMD ["/home/run.sh"]Copy the code

polymeric.DockerFile

FROM Debian :11.1 ADD./ resource-tar. gz/TMP RUN sh/TMP /build.sh EXPOSE 80 LABEL wanQQ="[email protected]" WORKDIR /usr/share/nginx/html/public CMD ["/home/run.sh"]Copy the code
  • The polymeric.DockerFile and layered.DockerFile are constructed with similar image sizes

  • The polymeric.DockerFile constructs fewer image layers than the layered.DockerFile, so it depends
  • My-lnp8.zip and my-lnp7.zip both have resource.tar.gz files with only a few differences. Here only the resource.tar.gz files in my-lnp8.zip are listed

Contained in the resource. Tar. gz file

  • DockerFile # Layered DockerFile
  • Polymeric.DockerFile # Compactly built DockerFile file
  • Run. Sh # Put the shell executed each time the image is started in /home
  • Build. Sh # The shell command to run when the image is finally built (ignore it for now) in the/TMP directory
  • Sources. list # Aliyun image source file can be viewed here
  • Find and download the version of PHP you want to install here
  • Nginx-1.21.4.tar. gz # Nginx source file Find and download the version of Nginx you want to install here
  • Default. Conf # nginx configuration file (referenced by nginx.conf)
  • Conf # nginx configuration file
  • Nginx # Nginx startup script file
  • Sh # The first script to run in Dockerfile
  • Install-php-rely. Sh # Install the dependencies required to compile PHP.
  • Install -php.sh #
  • Install -nginx-rely. Sh # Install the dependencies required to compile nginx (depending on the extension Settings used when compiling nginx)
  • Install – nginx. Sh # compiler to install nginx because some dependence has been install – PHP – rely. Sh installation in the install nginx – rely. No repeat installation in sh So need in this document Run install-php-rely. Sh and install-nginx-rely
  • Install-composer. Sh # Install composer
  • Config. Sh # configure PHP and nginx
  • When executing configure, different extensions will be compiled according to different parameters. If there is a dependency missing extension, an error will be reported. Install the corresponding package according to the error. The package name displayed by configure may not be the same as the package name in the software source. You need to search the package name in the search engine based on your system
  • Let’s look at the contents and purpose of each shell file

pre-operation.sh

#! /bin/bash
Source switching performs some of the pre-operations
rm /etc/apt/sources.list # delete apt source
cp /tmp/sources.list /etc/apt/sources.list Install pre-prepared software sources
cp /tmp/run.sh /home/run.sh Put the run file in /home
cd/ TMP tar ZXVF php-8.1.0.tar.gz tar ZXVF nginx-1.21.4.tar.gz chmod +x /home/run.sh# give execute permission to this file
groupadd nobody
# create nginx usergroupadd nginx useradd -g nginx -s /sbin/nologin nginx apt update && apt upgrade -y && apt-get update && apt-get upgrade  -y apt-get install -y apt-transport-https ca-certificates sysv-rc-conf vim wget unzip tmux redis php-pearInstall some software you might use, of your choice

Copy the code
  • This step does some preparatory work by unpacking all resources into the/TMP folder
  • Change the software source (here is Ali sources. List can prepare other domestic sources, do not want to change the source or do not change the source)
  • Create an nginx user. If you do not create an nginx user, you need to remove the corresponding configuration during nginx compilation and installation
  • Install some software you want to use and choose it yourself

Ali Sources. list content

deb http://mirrors.aliyun.com/debian/ bullseye main non-free contrib deb-src http://mirrors.aliyun.com/debian/ bullseye main non-free contrib deb http://mirrors.aliyun.com/debian-security/ bullseye-security main deb-src http://mirrors.aliyun.com/debian-security/ bullseye-security main deb http://mirrors.aliyun.com/debian/ bullseye-updates  main non-free contrib deb-src http://mirrors.aliyun.com/debian/ bullseye-updates main non-free contrib deb http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contrib deb-src http://mirrors.aliyun.com/debian/ bullseye-backports main non-free contribCopy the code

install-php-rely.sh

#! /bin/bash
Install the dependencies required to compile PHP
apt update && apt upgrade -y && apt-get update && apt-get upgrade -y
apt-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 Install PHP dependencies
Get the Redis extension
cd- 8.1.0 / TMP/PHP/ext/pecl channel - update pecl.php.net pecl download redis redis = $(ls -l | grep redis | awk'{print $9}')
gzip -d < $redis | tar -xvf -
rm $redis
redis=$(ls -l | grep redis | awk '{print $9}')
mv $redis redis
cd/ TMP /php-8.1.0/ rm configure. /buildconf --forceCopy the code
  • Install dependencies

install-php.sh

#! /bin/bash
Build and install PHP

/ TMP /php-8.1.0/
cd/ TMP/PHP - 8.1.0 /# set
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--enable-mbstring \
--with-curl \
--with-mysqli \
--enable-bcmath \
--with-openssl \
--with-zlib \
--enable-redis

# compile && install
make && make install

Register environment variables
ln -s /usr/local/php/bin/php /usr/local/bin/php
ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/php-fpm
Copy the code

– First of all, I will configure the extension items here according to the requirements of Tp and Laravel. I can choose other requirements by myself

  • Compile & install register environment variables nothing to say

install-nginx-rely.sh

 #! /bin/bash
# install Nginx dependencies (some dependencies are already installed in PHP dependencies)
apt update && apt upgrade -y && apt-get update && apt-get upgrade -y
apt-get -y install libpcre3 libpcre3-dev libgd-dev  # Install nginx dependencies
Copy the code
  • Install dependencies. The dependencies in this file are not complete because some of them were already installed when the PHP dependencies were installed

install-nginx.sh

#! /bin/bash
# install Nginx

/ TMP /nginx-1.21.4 / TMP /nginx-1.21.4
cd/ TMP/nginx 1.21.4 /# set
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_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_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module

# compile && install
make && make install

Register environment variables
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx
Copy the code

– First configure, the extension here IS configured according to other people’s articles, there are other needs of their own choice

  • The –user=nginx –group=nginx configuration item needs to be removed if the nginx user is not created
  • Compile & install register environment variables nothing to say

install-composer.sh

#! /bin/bash

# install composer
cd /tmp
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
mv composer.phar /usr/local/bin/composer
# # in the source
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
Copy the code
  • Packagist/Composer full mirroring in China and Ali Cloud Composer full mirroring

config.sh

#! ## PHP cp/TMP /php-8.1.0/php.ini-production /usr/local/php.ini cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf cp /usr/local/php/etc/php-fpm.d/www.conf.default / usr/local/PHP/etc/PHP - FPM. D/www.conf - 8.1.0 cp/TMP/PHP/sapi/FPM/init d.p HP - FPM/etc/init. D/PHP - FPM chmod + x /etc/init.d/php-fpm chmod 777 /etc/init.d/php-fpm sysv-rc-conf php-fpm on ##nginx mkdir -p /usr/share/nginx/html/public cp /tmp/index.php /usr/share/nginx/html/public/index.php cp /tmp/default.conf /usr/local/nginx/conf/default.conf cp /tmp/nginx.conf /usr/local/nginx/conf/nginx.conf cp /tmp/nginx /etc/init.d/nginx D /nginx chmod 777 /etc/init.d/nginx sysv-rc-conf nginx onCopy the code
  • I’m also a bit confused about configuring PHP and Nginx
  • CMD [“/home/run.sh”] to do this

run.sh

#! /bin/bash
php-fpm
nginx
redis-server

Copy the code
  • Used to start phP-fpm nginx and Redis-server can be modified

build.sh

#! /bin/bash

#sh /tmp/pre-operation.sh

Source switching performs some of the pre-operations
rm /etc/apt/sources.list # delete apt source
cp /tmp/sources.list /etc/apt/sources.list Install pre-prepared software sources
cp /tmp/run.sh /home/run.sh Put the run file in /home
cd/ TMP tar ZXVF php-8.1.0.tar.gz tar ZXVF nginx-1.21.4.tar.gz chmod +x /home/run.sh# give execute permission to this file
groupadd nobody
# create nginx usergroupadd nginx useradd -g nginx -s /sbin/nologin nginx apt update && apt upgrade -y && apt-get update && apt-get upgrade  -y apt-get install -y apt-transport-https ca-certificates sysv-rc-conf vim wget unzip tmux redis php-pearInstall some software you might use, of your choice



#sh /tmp/install-php-rely.sh

Install the dependencies required to compile PHP
apt update && apt upgrade -y && apt-get update && apt-get upgrade -y
apt-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 Install PHP dependencies
Get the Redis extension
cd- 8.1.0 / TMP/PHP/ext/pecl channel - update pecl.php.net pecl download redis redis = $(ls -l | grep redis | awk'{print $9}')
gzip -d < $redis | tar -xvf -
rm $redis
redis=$(ls -l | grep redis | awk '{print $9}')
mv $redis redis
cd/ TMP /php-8.1.0/ rm configure. /buildconf --force#sh /tmp/install-php.sh

Build and install PHP

/ TMP /php-8.1.0/
cd/ TMP/PHP - 8.1.0 /# set
./configure --prefix=/usr/local/php \
--with-config-file-path=/usr/local/php/etc \
--enable-fpm \
--enable-mbstring \
--with-curl \
--with-mysqli \
--enable-bcmath \
--with-openssl \
--with-zlib \
--enable-redis

# compile && install
make && make install

Register environment variables
ln -s /usr/local/php/bin/php /usr/local/bin/php
ln -s /usr/local/php/sbin/php-fpm /usr/local/bin/php-fpm

#sh /tmp/install-nginx-rely.sh

# install Nginx dependencies (some dependencies are already installed in PHP dependencies)
apt update && apt upgrade -y && apt-get update && apt-get upgrade -y
apt-get -y install libpcre3 libpcre3-dev libgd-dev  # Install nginx dependencies

#sh /tmp/install-nginx.sh

# install Nginx

/ TMP /nginx-1.21.4 / TMP /nginx-1.21.4
cd/ TMP/nginx 1.21.4 /# set
./configure --prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-pcre \
--with-http_ssl_module \
--with-http_v2_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_image_filter_module \
--with-http_slice_module \
--with-mail \
--with-threads \
--with-file-aio \
--with-stream \
--with-mail_ssl_module \
--with-stream_ssl_module

# compile && install
make && make install

Register environment variables
ln -s /usr/local/nginx/sbin/nginx /usr/local/bin/nginx


#sh /tmp/install-composer.sh

# install composer
cd /tmp
php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"
php composer-setup.php
mv composer.phar /usr/local/bin/composer
# # in the source
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

#sh /tmp/config.sh


Configure the configuration file
##phpCp/TMP / - 8.1.0 / PHP. PHP ini - production/usr /local/php/php.ini
cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/ PHP/etc/PHP - FPM. D/www.conf - 8.1.0 cp/TMP/PHP/sapi/FPM/init d.p HP - FPM/etc/init. D/PHP - FPM chmod + x/etc/init. D/PHP - FPM chmod 777 /etc/init.d/php-fpm sysv-rc-conf php-fpm on# Boot automatically

##nginx
mkdir -p /usr/share/nginx/html/public
cp /tmp/index.php /usr/share/nginx/html/public/index.php
cp /tmp/default.conf /usr/local/nginx/conf/default.conf
cp /tmp/nginx.conf /usr/local/nginx/conf/nginx.conf
cp /tmp/nginx /etc/init.d/nginx
chmod +x /etc/init.d/nginx
chmod 777 /etc/init.d/nginx
sysv-rc-conf nginx on # Boot automatically
Copy the code
  • Instead of repeating the explanation, I just put the previous documents together

  • Reference article:

Use sysv-rc-conf to set NGINX to autoboot in Debian/Ubuntu.

PHP +nginx +nginx

PHP: Installation for Unix systems – Manual

Jianshu.com common extension module for PHP – Jianshu.com

Install | the Laravel 8 Chinese document 8. X “| Laravel China community (learnku.com)

· Complete Development manual of ThinkPHP5.0 · Look at the Cloud (kancloud.cn)