nginx+php-fpm docker鏡像合二為一


一、概述

在上一篇文章介紹了nginx+php-fpm,鏈接如下:

https://www.cnblogs.com/xiao987334176/p/12918413.html

 

nginx和php-fpm是2個獨立的鏡像,在實際環境部署過程中,發現配置比較麻煩,排錯比較耗費實際。

因此,需要將nginx和php-fpm 這2個鏡像合並為一個。

 

二、nginx+php-fpm封裝

目錄結構

由於crunchgeek/php-fpm:7.3-r7 鏡像比較大,有1.08GB。

因此需要使用alpine:3.11重新封裝才行。

 

在dockerhub上面,php已經有官方的鏡像了,php:7.3-fpm-alpine3.11。

由於項目php7cms依賴於組件mysqli,因此需要額外安裝才行。

 

新建目錄/opt/alpine_nginx_php7.3,結構如下:

./
├── default.conf
├── dockerfile
├── index.html
├── php.ini
├── repositories
└── run.sh

 

default.conf

server {
  listen 80;
  server_name localhost;

  root /var/www/html;
  index index.html index.htm index.nginx-debian.html;

  location / {
    try_files $uri $uri/ =404;
  }
  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

 

dockerfile

FROM php:7.3-fpm-alpine3.11
ADD repositories /etc/apk/repositories
ADD default.conf /
ADD index.html /
ADD run.sh /
ADD php.ini /usr/local/etc/php/
RUN apk update && apk add nginx && \
    apk add m4 autoconf make gcc g++ linux-headers && \
    docker-php-ext-install pdo_mysql opcache mysqli && \
    mkdir /run/nginx && \
    mv /default.conf /etc/nginx/conf.d && \
    mv /index.html /var/www/html && \
    touch /run/nginx/nginx.pid && \
    chmod 755 /run.sh && \
    apk del m4 autoconf make gcc g++ linux-headers

EXPOSE 80
EXPOSE 9000

ENTRYPOINT ["/run.sh"]

 

注意:這里我額外安裝了pdo_mysql,因為某些php項目用的是這個模塊。opcache是用來做性能加速的。

 

index.html

<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>

<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>

<p><em>Thank you for using nginx.</em></p>
</body>
</html>

 

php.ini

[PHP]
engine = On
short_open_tag = Off
precision = 14
output_buffering = 4096
zlib.output_compression = Off
implicit_flush = Off
unserialize_callback_func =
serialize_precision = -1
disable_functions =
disable_classes =
zend.enable_gc = On
expose_php = On
max_execution_time = 30
max_input_time = 60
memory_limit = 128M
error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
display_startup_errors = Off
log_errors = On
log_errors_max_len = 1024
ignore_repeated_errors = Off
ignore_repeated_source = Off
report_memleaks = On
html_errors = On
variables_order = "GPCS"
request_order = "GP"
register_argc_argv = Off
auto_globals_jit = On
post_max_size = 8M
auto_prepend_file =
auto_append_file =
default_mimetype = "text/html"
default_charset = "UTF-8"
doc_root =
user_dir =
enable_dl = Off
cgi.fix_pathinfo=0
file_uploads = On
upload_max_filesize = 2M
max_file_uploads = 20
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
[CLI Server]
cli_server.color = On
[Date]
[filter]
[iconv]
[imap]
[intl]
[sqlite3]
[Pcre]
[Pdo]
[Pdo_mysql]
pdo_mysql.default_socket=
[Phar]
[mail function]
SMTP = localhost
smtp_port = 25
mail.add_x_header = Off
[ODBC]
odbc.allow_persistent = On
odbc.check_persistent = On
odbc.max_persistent = -1
odbc.max_links = -1
odbc.defaultlrl = 4096
odbc.defaultbinmode = 1
[Interbase]
ibase.allow_persistent = 1
ibase.max_persistent = -1
ibase.max_links = -1
ibase.timestampformat = "%Y-%m-%d %H:%M:%S"
ibase.dateformat = "%Y-%m-%d"
ibase.timeformat = "%H:%M:%S"
[MySQLi]
mysqli.max_persistent = -1
mysqli.allow_persistent = On
mysqli.max_links = -1
mysqli.default_port = 3306
mysqli.default_socket =
mysqli.default_host =
mysqli.default_user =
mysqli.default_pw =
mysqli.reconnect = Off
[mysqlnd]
mysqlnd.collect_statistics = On
mysqlnd.collect_memory_statistics = Off
[OCI8]
[PostgreSQL]
pgsql.allow_persistent = On
pgsql.auto_reset_persistent = Off
pgsql.max_persistent = -1
pgsql.max_links = -1
pgsql.ignore_notice = 0
pgsql.log_notice = 0
[bcmath]
bcmath.scale = 0
[browscap]
[Session]
session.save_handler = files
session.use_strict_mode = 0
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.cookie_httponly =
session.cookie_samesite =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 1000
session.gc_maxlifetime = 1440
session.referer_check =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0
session.sid_length = 26
session.trans_sid_tags = "a=href,area=href,frame=src,form="
session.sid_bits_per_character = 5
[Assertion]
zend.assertions = -1
[COM]
[mbstring]
[gd]
[exif]
[Tidy]
tidy.clean_output = Off
[soap]
soap.wsdl_cache_enabled=1
soap.wsdl_cache_dir="/tmp"
soap.wsdl_cache_ttl=86400
soap.wsdl_cache_limit = 5
[sysvshm]
[ldap]
ldap.max_links = -1
[dba]
[opcache]
[curl]
[openssl]
View Code

此文件是從容器里面copy出來的,路徑為:/usr/local/etc/php/php.ini-production

去除了注釋和多余的空行。

此配置文件,修改了cgi.fix_pathinfo=0

如果需要更改其他配置,修改此文件即可。

 

repositories

https://mirrors.aliyun.com/alpine/v3.11/main/
https://mirrors.aliyun.com/alpine/v3.11/community/

這個是阿里雲的alpine更新源

 

run.sh

#!/bin/sh

# 后台啟動
php-fpm -D
# 關閉后台啟動,hold住進程
nginx -g 'daemon off;'

 

封裝鏡像

cd /opt/alpine_nginx_php7.3
docker build -t alpine_nginx_php7.3:1 .

 

查看鏡像大小

# docker images|grep alpine_nginx_php7.3
alpine_nginx_php7.3       1                    927ddfbdd027        14 minutes ago      78.4MB

可以看到這個鏡像只有78.4MB。

 

運行鏡像

docker run -it  --name alpine_nginx_php7.3 -p 80:80 alpine_nginx_php7.3:1 .

 

訪問首頁

http://ip地址/

效果如下:

 

phpinfo頁面

新建test.php

cd /opt/alpine_nginx_php7.3
vi test.php

 

內容如下:

<?php
   phpinfo();
?>

 

拷貝到容器中

docker cp test.php alpine_nginx_php7.3:/var/www/html/

 

訪問test.php

http://ip地址/test.php

效果如下:

 

三、運行PHP7CMS

下載源代碼

源代碼下載地址:

http://down.chinaz.com/soft/38829.htm

 

下載完成后,在windows10電腦中解壓。

進入linux系統,創建空目錄/opt/php7cms,將解壓文件夾PHP7CMS的所有內容上傳到/opt/php7cms中。

此時/opt/php7cms目錄結構如下:

# tree -L 1
.
├── admin.php
├── api
├── cache
├── config
├── index.php
├── install.php
├── LICENSE
├── php7cms
├── README.md
├── static
├── template
├── uploadfile
├── 安裝方法.txt
└── 安裝環境.docx

-L 參數表示控制深度,這里只展示第一層。

 

在此目錄新建dockerfile

FROM alpine_nginx_php7.3:1
ADD default.conf /etc/nginx/conf.d
ADD . /var/www/html/PHP7CMS
RUN chown www-data:www-data -R /var/www/html

 

在此目錄新建default.conf

server {
  listen 80;
  server_name localhost;
  root /var/www/html/PHP7CMS;
  index index.php index.html index.htm;

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }
}

這個文件用來將nginx默認的配置覆蓋掉

 

封裝鏡像

cd /opt/php7cms
docker build -t php7cms:1 .

 

運行docker

先將之前運行的nginx_php刪除掉,再運行php7cms。否則會端口沖突

docker rm -f alpine_nginx_php7.3
docker run -d -it --restart=always --name php7cms -p 80:80 php7cms:1

 

由於php7cms依賴於mysql,還得運行一個mysql才行。

mkdir -p /data/mysql/data
docker run -d --name mysql5.7 --restart=always -e MYSQL_ROOT_PASSWORD=abcd@1234  -p 3306:3306 -v /data/mysql/data:/var/lib/mysql mysql:5.7 --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci

 

新建空的數據庫cms

# docker exec -it mysql5.7 /bin/bash
# mysql -u root -pabcd@1234
mysql> create database cms default character set utf8mb4 collate utf8mb4_unicode_ci;

 

安裝向導

我的服務器ip地址為:10.212.20.213

訪問安裝頁面

http://10.212.20.213/install.php

 

輸入數據庫連接信息

 

點擊下一步后,提示安裝完成。

 

 

 

登錄后台頁面

 

默認用戶名和密碼都是admin

 

登錄成功后,效果如下:

 

 

訪問首頁

http://10.212.20.213/

效果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM