如何打包Docker Apache+PHP 7.4.11(可连MySQL)?

Apache

1 基础知识

以下版本基于MySQL数据库的连接,本章节需要你先掌握Linux系统的PHP 7.4.11部署,如你尚未具备此知识,请按如下方法熟悉部署,

如何编译安装Apache+PHP7.4.11?

2 最佳实践

2.1 部署前的准备

本章使用如下Docker环境部署,参阅本章建议你先搭建以下Docker集群环境并熟悉,

如何部署Docker生产环境?

2.2 创建新镜像

2.2.1 创建镜像项目文件夹

mkdir ~/imageProject-php-7.4.11

2.2.2 创建Dockerfile

vim ~/imageProject-php-7.4.11/Dockerfile

加入如下配置,

FROM centos:centos8
MAINTAINER will@cmdschool.org

# Update OS
RUN rm -rf /etc/yum.repos.d/*
RUN curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
RUN dnf update -y

# Deploy tools
RUN dnf install -y wget net-tools vim bzip2
RUN dnf install -y gcc gcc-c++ make expat-devel

# Download fiels
WORKDIR /root/
RUN wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-1.7.0.tar.gz
RUN wget https://mirrors.tuna.tsinghua.edu.cn/apache/apr/apr-util-1.6.1.tar.gz
RUN wget https://mirror.bit.edu.cn/apache/httpd/httpd-2.4.46.tar.gz
RUN wget https://www.php.net/distributions/php-7.4.11.tar.bz2

# Deploy apr
WORKDIR /root/
RUN tar -xf apr-1.7.0.tar.gz
WORKDIR /root/apr-1.7.0

RUN ./configure
RUN make
RUN make install

# Deploy apr-util
WORKDIR /root/
RUN tar -xf apr-util-1.6.1.tar.gz
WORKDIR /root/apr-util-1.6.1
RUN ./configure --with-apr=/usr/local/apr
RUN make
RUN make install

# Deploy httpd
RUN dnf install -y pcre-devel openssl-devel perl-devel
WORKDIR /root/
RUN tar -xf httpd-2.4.46.tar.gz
WORKDIR /root/httpd-2.4.46
RUN ./configure --bindir=/usr/sbin/ \
                --sbindir=/usr/sbin/ \
                --sysconfdir=/etc/httpd/ \
                --libdir=/usr/lib64/  \
                --mandir=/usr/share/man/ \
                --includedir=/usr/include/ \
                --enable-so \
                --enable-ssl \
                --enable-socache-shmcb

RUN make
RUN make install

RUN groupadd  -g 48 apache
RUN useradd -u 48 -g 48 -d /usr/share/httpd -s /sbin/nologin apache
RUN sed -i "s/User daemon/User apache/g" /etc/httpd/httpd.conf
RUN sed -i "s/Group daemon/Group apache/g" /etc/httpd/httpd.conf
RUN sed -i "s/\#ServerName www.example.com\:80/ServerName localhost\:80/g" /etc/httpd/httpd.conf
RUN sed -i "s/\#LoadModule socache\_shmcb\_module modules\/mod\_socache\_shmcb\.so/LoadModule socache\_shmcb\_module modules\/mod\_socache\_shmcb\.so/g" /etc/httpd/httpd.conf
COPY httpd-ssl.conf /etc/httpd/conf.d/httpd-ssl.conf

RUN mkdir -p /etc/httpd/conf.d
RUN echo 'Include /etc/httpd/conf.d/*.conf' >> /etc/httpd/httpd.conf

RUN mkdir /var/www
RUN ln -s /usr/local/apache2/htdocs /var/www/html
RUN ln -s /usr/local/apache2/logs /var/log/httpd

# Deploy php
RUN dnf install -y libxml2-devel sqlite-devel
WORKDIR /root
RUN tar -xf php-7.4.11.tar.bz2

WORKDIR /root/php-7.4.11
RUN ./configure --bindir=/usr/sbin/ \
                --sbindir=/usr/sbin/ \
                --sysconfdir=/etc/ \
                --libdir=/usr/lib64/  \
                --mandir=/usr/share/man/ \
                --includedir=/usr/include/ \
                --with-apxs2=/usr/sbin/apxs \
                --with-pdo-mysql

RUN make
RUN make install

RUN cp php.ini-development /etc/php.ini
RUN ln -s /etc/php.ini /usr/lib64/
RUN sed -i 's/;date.timezone =/date.timezone = Asia\/Shanghai/g' /etc/php.ini
COPY php.conf /etc/httpd/conf.d/php.conf
RUN mkdir -p /var/lib/php/session
RUN chown apache:apache /var/lib/php/session
RUN chmod 775 /var/lib/php/session
RUN httpd -t

RUN mkdir -p /var/www/html
RUN echo '' > /var/www/html/index.php

# clean cache
WORKDIR /root
RUN dnf clean all
RUN rm -rf /root/apr-*
RUN rm -rf /root/httpd-2.4.46*
RUN rm -rf /root/php-7.4.11*

# Config Start Scripts
RUN echo '#!/bin/bash' > /root/start.sh
RUN echo 'echo "Starting HTTP Server...";' >> /root/start.sh
RUN echo '/usr/sbin/httpd -f /etc/httpd/httpd.conf -D FOREGROUND' >> /root/start.sh
RUN chmod +x /root/start.sh
EXPOSE 80 443

WORKDIR /root
CMD ["/bin/bash","./start.sh"]

注:
“FROM”指令声明基于“centos:centos8”镜像打包
“COPY”指令声明复制当前目录的具体文件到打包的镜像
“ENV”指令声明Java JDK运行所需的环境变量
所以我们需要根据定义的Apache的httpd-ssl.conf配置文件,

vim ~/imageProject-php-7.4.11/httpd-ssl.conf

加入的配置如下,

LoadModule ssl_module modules/mod_ssl.so
Listen 443
SSLPassPhraseDialog builtin
SSLSessionCache shmcb:/var/cache/mod_ssl/scache(512000)
SSLSessionCacheTimeout 300
SSLRandomSeed startup file:/dev/urandom 256
SSLRandomSeed connect builtin
SSLCryptoDevice builtin

然后,我们需要根据定义的PHP的php.conf配置文件,

vim ~/imageProject-php-7.4.11/php.conf

加入的配置如下,

#
# Cause the PHP interpreter to handle files with a .php extension.
#
<FilesMatch \.php$>
SetHandler application/x-httpd-php
</FilesMatch>

#
# Allow php to handle Multiviews
#
AddType text/html .php

#
# Add index.php to the list of files that will be served as directory
# indexes.
#
DirectoryIndex index.php

#
# Uncomment the following lines to allow PHP to pretty-print .phps
# files as PHP source code:
#
#<FilesMatch \.phps$>
# SetHandler application/x-httpd-php-source
#</FilesMatch>

#
# Apache specific PHP configuration options
# those can be override in each configured vhost
#
php_value session.save_handler "files"
php_value session.save_path "/var/lib/php/session"

然后,可使用如下命令确认文件,

ls ~/imageProject-php-7.4.11

可见如下显示,

Dockerfile  httpd-ssl.conf  php.conf

2.2.3 执行打包操作

cd ~/imageProject-php-7.4.11
docker build -t build/php:7.4.11 .

以上“.”指当前目录为编译目录,编译程序会自动加载“Dockerfile”文件定义,可见如下显示,

Sending build context to Docker daemon  23.77MB
Step 1/73 : FROM centos:centos8
 ---> 0d120b6ccaa8
[...]
Successfully built ad31ce7e5fc8
Successfully tagged build/php:7.4.11

完成后,可使用如下命令查看镜像,

docker images

可见如下显示,

REPOSITORY                                               TAG                        IMAGE ID            CREATED              SIZE
build/php                                                7.4.11                     ad31ce7e5fc8        About a minute ago   1.35GB
[...]

2.2.4 测试软件运行

docker run -d --name apache-php -p 80:80 -p 443:443 build/php:7.4.11

以上运行容器环境后,我们使用以下命令登录容器虚拟机,

docker exec -it `docker container ls | grep 'apache-php' | cut -d" " -f1 ` /bin/bash

测试apache-php运行

netstat -antp

可见如下显示,

Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:443             0.0.0.0:*               LISTEN      6/httpd
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      6/httpd

另外,你也可以使用浏览器测试,
http://10.168.0.210/index.php

测试完成后可使用如下命令退出容器虚拟机,

exit

另外,你可以使用如下命令查询当前运行的container,

docker ps -a

可见如下显示,

CONTAINER ID        IMAGE                                                                  COMMAND                  CREATED              STATUS              PORTS                                      NAMES
4b48cc991ea4        build/php:7.4.11                                                       "/bin/bash ./start.sh"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, 0.0.0.0:443->443/tcp   apache-php
[...]

然后可以使用如下命令停止并删除,

docker container stop 4b48cc991ea4
docker container rm 4b48cc991ea4

参阅文档
=======================

没有评论

发表评论

Docker
如何打包Docker MAC-Telnet?

1 基础知识 一款可以使用MikroTik RouterOS MAC-Telnet协议连接Mikro …

Docker
如何部署Docker Unifi-WiFi?

1 前言 一个问题,一篇文章,一出故事。 笔者Unifi-WiFi想要迁移至docker环境,于是产 …

Docker
如何打包Docker Unifi-WiFi?

1 基础知识 正式安装前,我们建议你先熟悉以下章节, 如何基于Debian部署UBNT Unifi控 …