docker php-fpm連線sybase(sqlanywhere)資料庫

同前一篇設定

此次改用docker來做php-fpm然後利用apache proxy來執行php程式

Docker 環境

debian
PHP 7.4

參考此Dockerfile

https://github.com/yappabe/docker-php

DockerFile

FROM debian:buster


RUN \
apt-get update && \
apt-get install -y \
curl \
wget \
apt-transport-https \
lsb-release \
ca-certificates \
gnupg2 \
git


RUN wget -O- https://packages.sury.org/php/apt.gpg | apt-key add - && \
echo "deb https://packages.sury.org/php/ $(lsb_release -sc) main" > /etc/apt/sources.list.d/php.list


RUN \
apt-get update && \
apt-get install -y \
vim \
locales \
php7.4-fpm \
php7.4-mysql \
php7.4-gd \
php7.4-imagick \
php7.4-dev \
php7.4-curl \
php7.4-opcache \
php7.4-cli \
php7.4-sqlite \
php7.4-intl \
php7.4-tidy \
php7.4-imap \
php7.4-json \
php7.4-pspell \
php7.4-common \
php7.4-sybase \
php7.4-sqlite3 \
php7.4-bz2 \
php7.4-common \
php7.4-apcu-bc \
php7.4-memcached \
php7.4-redis \
php7.4-xml \
php7.4-shmop \
php7.4-mbstring \
php7.4-zip \
php7.4-bcmath \
php7.4-soap \
php7.4-sybase


RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer && \
composer global require hirak/prestissimo && \
wget http://robo.li/robo.phar && \
chmod +x robo.phar && mv robo.phar /usr/bin/robo


RUN echo Asia/Taipei > /etc/timezone && dpkg-reconfigure --frontend noninteractive tzdata


RUN echo 'en_US ISO-8859-1\n\
en_US.ISO-8859-15 ISO-8859-15\n\
en_US.UTF-8 UTF-8\n'\
>> /etc/locale.gen && \
usr/sbin/locale-gen


RUN usermod -u 1000 www-data
RUN mkdir "/run/php"


ENV ENVIRONMENT dev
ENV PHP_FPM_USER www-data
ENV PHP_FPM_PORT 9000
ENV PHP_ERROR_REPORTING "E_ALL \& ~E_NOTICE \& ~E_STRICT \& ~E_DEPRECATED"
ENV PATH "/root/.composer/vendor/bin:$PATH"
ENV COMPOSER_ALLOW_SUPERUSER 1


COPY php.ini /etc/php/7.4/fpm/conf.d/
COPY php.ini /etc/php/7.4/cli/conf.d/
COPY www.conf /etc/php/7.4/fpm/pool.d/
COPY freetds.conf /etc/freetds/
ADD run.sh /run.sh


COPY run.sh /run.sh


ENTRYPOINT ["/bin/bash", "/run.sh"]

freetds.conf

建議可以把freetds.conf放到一個資料夾與docker共享,如此相對比較有彈性

# $Id: freetds.conf,v 1.12 2007-12-25 06:02:36 jklowden Exp $
#
# This file is installed by FreeTDS if no file by the same
# name is found in the installation directory.
#
# For information about the layout of this file and its settings,
# see the freetds.conf manpage "man freetds.conf".


# Global settings are overridden by those in a database
# server specific section
[global]
# TDS protocol version
tds version = auto


# Whether to write a TDSDUMP file for diagnostic purposes
# (setting this to /tmp is insecure on a multi-user system)
; dump file = /tmp/freetds.log
; debug flags = 0xffff


# Command and connection timeouts
; timeout = 10
; connect timeout = 10
# If you get out-of-memory errors, it may mean that your client
# is trying to allocate a huge buffer for a TEXT field.
# Try setting 'text size' to a more reasonable limit
text size = 64512


# If you experience TLS handshake errors and are using openssl,
# try adjusting the cipher list (don't surround in double or single quotes)
# openssl ciphers = HIGH:!SSLv2:!aNULL:-DH
client charset = UTF-8
charset = utf8





[sybase_db]
host = 192.168.0.137
port = 2638
tds version = 5.0
#client charset = UTF-8

php.ini

date.timezone = Asia/Taipei
include_path = ".:/usr/share/php"
realpath_cache_size = 4096k
realpath_cache_ttl = 7200
upload_max_filesize = 40M
post_max_size = 40M
apc.enable_cli = 1
apc.shm_size=32M
apc.ttl=7200
apc.enable_cli=1
phar.readonly = Off

run.sh

#!/bin/bash


variables=( "PHP_FPM_USER" "PHP_FPM_PORT" "PHP_ERROR_REPORTING" "ENVIRONMENT" )


for var in "${variables[@]}"
do
:
sed -i "s|%$var%|${!var}|g" /etc/php/7.4/fpm/pool.d/www.conf
done


/usr/sbin/php-fpm7.4 --allow-to-run-as-root -c /etc/php/7.4/fpm --nodaemonize

www.conf

[global]


error_log = /proc/self/fd/2
daemonize = no


[wwww]


listen = 0.0.0.0:%PHP_FPM_PORT%


listen.owner = %PHP_FPM_USER%
listen.group = %PHP_FPM_USER%


listen.mode = 0666


clear_env = no


pm = ondemand
pm.max_children = 25
pm.process_idle_timeout = 10s
pm.max_requests = 200


chdir = /


user = %PHP_FPM_USER%
group = %PHP_FPM_USER%


php_flag[log_errors] = True
php_value[display_errors] = False
php_value[error_log] = /var/log/error.log
php_value[memory_limit] = 2048M
php_value[error_reporting] = %PHP_ERROR_REPORTING%
env[ENVIRONMENT] = %ENVIRONMENT%

docker-compose.yml

php:
build: .
working_dir: /var/www/html
environment:
PHP_FPM_USER: root
PHP_FPM_PORT: 9000
PHP_ERROR_REPORTING: E_ALL
ENVIRONMENT: staging
volumes:
- /var/www/:/var/www/
- /etc/freetds/:/etc/freetds/
ports:
- "127.0.0.1:9001:9000"

執行docker

sudo docker-compose up -d --build

apache2 需要使用到的VirtualHost上加上

ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9001/var/www/html/$1

Github Soruce

https://github.com/catyku/docker-php-fpm7.4-sybase

發表迴響