前言
在以往的容器環境部署中 運行環境 我們通常把 類似 apache nginx php 等 打包在一個鏡像中 起一個容器。 這樣做的好處是 方便 簡單。 不便的地方是 如果PHP 需要擴展新的 相關組件就麻煩了。例如筆者之前用的 apache+php 組合在一個鏡像中 需要添加 php-redis 擴展,就很麻煩 因為這個擴展需要重新編譯(常見的擴展只需要添加 或者安裝 )。 如果編譯的化 時間久依賴多 問題多。
並且 nginx php-fpm 相關軟件都是 獨立開源軟件 官方並不提供完整組合 套件 ; 放在一個容器中 拼湊起來 對新人 不太方便; 今天通過 摸索 可以實現 通過組合來搭建 php 運行環境。
php
php 官方鏡像構建
#使用國內網易鏡像可以加快構建速度
FROM hub.c.163.com/library/php:fpm-alpine
#FROM php:fpm-alpine
MAINTAINER Alu alu@xdreport.com
#國內repo源,讓本地構建速度更快。
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
#安裝GD依賴庫
RUN apk add --no-cache --virtual .build-deps \
freetype-dev \
libjpeg-turbo-dev \
libpng-dev \
libmcrypt-dev
#添加php源碼中的擴展,添加gd,mysqli,pdo-mysql,opcache,gettext,mcrypt等擴展
RUN set -ex \
&& docker-php-ext-configure gd \
--with-freetype-dir=/usr/include/freetype2/freetype \
--with-jpeg-dir=/usr/include \
--with-png-dir=/usr/include \
&& docker-php-ext-install gd bcmath zip opcache iconv mcrypt pdo pdo_mysql mysqli
#redis屬於pecl擴展,需要使用pecl命令來安裝,同時需要添加依賴的庫
RUN apk add --no-cache --virtual .phpize-deps $PHPIZE_DEPS \
&& pecl install redis-3.1.2 \
&& docker-php-ext-enable redis \
&& apk del .phpize-deps
安裝redis 擴展
curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/3.1.3.tar.gz
tar xfz /tmp/redis.tar.gz
rm -r /tmp/redis.tar.gz
mkdir -p /usr/src/php/ext
mv phpredis-3.1.3 /usr/src/php/ext/redis
docker-php-ext-install redis
nginx
# 習慣nginx alpine 這個版本 因為小 7M
docker pull nginx:alpine
nginx 配置
- /etc/nginx/conf.d/default.conf
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .php$ {
#root /usr/share/nginx/html;
root /var/www/html; # `這個配置 用 php-fmp 鏡像 容器中的 PHP 根目錄地址 切記這個不是 nginx web根目錄地址 這個問題折騰了我好久`
fastcgi_pass phpfpm:9000; # 修改這個地址為 phpfpm 容器的名稱 我的容器名稱就是 phpfmp
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
參考完整 default.conf
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;
client_max_body_size 4080m;
send_timeout 6000;
fastcgi_connect_timeout 6000;
fastcgi_send_timeout 6000;
fastcgi_read_timeout 6000;
location / {
#try_files $uri $uri/ /index.php$uri?$query_string;
root /usr/share/nginx/html;
index index.html index.php index.htm;
if ( -f $request_filename) {
break;
}
if ( !-e $request_filename) {
rewrite ^(.*)$ /index.php/$1 last;
break;
}
}
#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 /usr/share/nginx/html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
location ~ .+\.php($|/) {
root /var/www/html/public;
fastcgi_pass phpfmp:9000;
fastcgi_index index.php;
#加載Nginx默認"服務器環境變量"配置
include fastcgi.conf;
#設置PATH_INFO並改寫SCRIPT_FILENAME,SCRIPT_NAME服務器環境變量
set $fastcgi_script_name2 $fastcgi_script_name;
if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {
set $fastcgi_script_name2 $1;
set $path_info $2;
}
fastcgi_param PATH_INFO $path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
大文件配置參考
nginx的修改 /etc/nginx/conf.d/default.conf
send_timeout 60;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
client_max_body_size 30m;
php的修改 /usr/local/etc/php/ 中的配置文件 復制 為php.ini
upload_max_filesize
post_max_size
max_input_time
max_execution_time
php-fpm注意參數
request_terminate_timeout
request_slowlog_timeout
這兩個參數如果設置過小的話會導致文件傳輸了一部分后連接關閉。
組合(rancher)
phpfpm:
tty: true
image: hub.03in.com:5002/ranmufei/phpalpine-redis:v1
volumes:
- /home/www/nginx4/html:/var/www/html
stdin_open: true
nginx4:
ports:
- 7575:80/tcp
tty: true
image: nginx:alpine
links:
- phpfpm:phpfmp
volumes:
- /home/www/nginx4/html:/usr/share/nginx/html
- /home/www/nginx4/conf/default.conf:/etc/nginx/conf.d/default.conf
- /home/www/nginx4/conf/nginx.conf:/etc/nginx/nginx.conf
stdin_open: true