redhat PHP 部署

1.通过catalog部署php过程:

(1)填写catalog表单生成builds,deployment,service等一系列对象,貌似等价于执行oc new-app;

(2)PHP builds容器启动 执行S2I build;

(3)从GitHub上下载代码;

(4)运行assemble脚本将应用程序源放置在所需的位置;

(5)设置run脚本为CMD 并commit为新的容器镜像到项目的imagestream;

(6)dc拉起生成的镜像实现应用部署 。

2.应用端注意事项:

(1)自定义环境变量 :代码指定目录.s2i/environment 

源生自带的环境变量:

HOSTNAME=41c549462611
PHP_DEFAULT_INCLUDE_PATH=/opt/rh/rh-php72/root/usr/share/pear
APP_ROOT=/opt/app-root
PHP_VER_SHORT=72
TERM=xterm
APP_DATA=/opt/app-root/src
DESCRIPTION=PHP 7.2 available as container is a base platform for building and running various PHP 7.2 applications and frameworks. PHP is an HTML-embedded scripting language. PHP attempts to make it easy for developers to write dynamically generated web pages. PHP also offers built-in database integration for several commercial and non-commercial database management systems, so writing a database-enabled webpage with PHP is fairly simple. The most common use of PHP coding is probably as a replacement for CGI scripts.
NODEJS_SCL=rh-nodejs10
HTTPD_VAR_PATH=/opt/rh/httpd24/root/var
NAME=php
HTTPD_CONFIGURATION_PATH=/opt/app-root/etc/conf.d
PATH=/opt/app-root/src/bin:/opt/app-root/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/opt/rh/rh-php72/root/usr/bin
HTTPD_VAR_RUN=/var/run/httpd
STI_SCRIPTS_URL=image:///usr/libexec/s2i
HTTPD_MAIN_CONF_PATH=/etc/httpd/conf
PWD=/opt/app-root/src
STI_SCRIPTS_PATH=/usr/libexec/s2i
SUMMARY=Platform for building and running PHP 7.2 applications
PLATFORM=el7
HTTPD_DATA_PATH=/var/www
HOME=/opt/app-root/src
HTTPD_DATA_ORIG_PATH=/opt/rh/httpd24/root/var/www
SHLVL=1
PHP_SYSCONF_PATH=/etc/opt/rh/rh-php72
SCL_ENABLED=rh-php72
PHP_HTTPD_CONF_FILE=rh-php72-php.conf
PHP_CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/php/
HTTPD_MAIN_CONF_D_PATH=/etc/httpd/conf.d
PHP_VERSION=7.2
_=/usr/bin/env

(2)适配脚本:如需修改覆盖源镜像脚本即可

assemble 脚本:

#!/bin/bash

set -e

source ${PHP_CONTAINER_SCRIPTS_PATH}/common.sh

shopt -s dotglob
echo "---> Installing application source..."
mv /tmp/src/* ./

# Fix source directory permissions
fix-permissions ./
fix-permissions ${HTTPD_CONFIGURATION_PATH}

# Change the npm registry mirror if provided
if [ -n "$NPM_MIRROR" ]; then
    npm config set registry $NPM_MIRROR
fi

if [ -f composer.json ]; then
  echo "Found ‘composer.json‘, installing dependencies using composer.phar... "

  # Install Composer
  TEMPFILE=$(mktemp)
  RETRIES=6
  for ((i=0; i<$RETRIES; i++)); do

    if [ -z "$COMPOSER_INSTALLER" ]; then
      export COMPOSER_INSTALLER="https://getcomposer.org/installer"
    fi

    echo "Downloading $COMPOSER_INSTALLER, attempt $((i+1))/$RETRIES"
    curl -o $TEMPFILE $COMPOSER_INSTALLER && break
    sleep 10
  done
  if [[ $i == $RETRIES ]]; then
    echo "Download failed, giving up."
    exit 1
  fi
  php <$TEMPFILE

  if [ "$(ls -a /tmp/artifacts/ 2>/dev/null)" ]; then
    echo "Restoring build artifacts"
    mv /tmp/artifacts/* $HOME/
  fi

  # Change the repo mirror if provided
  if [ -n "$COMPOSER_MIRROR" ]; then
    ./composer.phar config -g repositories.packagist composer $COMPOSER_MIRROR
  fi

  # Install App dependencies using Composer
  ./composer.phar install --no-interaction --no-ansi --optimize-autoloader $COMPOSER_ARGS

  if [ ! -f composer.lock ]; then
    echo -e "\nConsider adding a ‘composer.lock‘ file into your source repository.\n"
  fi
fi

# post-assemble files
process_extending_files ./php-post-assemble/ ${PHP_CONTAINER_SCRIPTS_PATH}/post-assemble/

# Fix source directory permissions
fix-permissions ./
fix-permissions ${HTTPD_CONFIGURATION_PATH}

run脚本:(启动应用程序以及Apache服务器)

sh-4.2$ cat run
#!/bin/bash

source ${PHP_CONTAINER_SCRIPTS_PATH}/common.sh

export_vars=$(cgroup-limits); export $export_vars
export DOCUMENTROOT=${DOCUMENTROOT:-/}

# Default php.ini configuration values, all taken
# from php defaults.
export ERROR_REPORTING=${ERROR_REPORTING:-E_ALL & ~E_NOTICE}
export DISPLAY_ERRORS=${DISPLAY_ERRORS:-ON}
export DISPLAY_STARTUP_ERRORS=${DISPLAY_STARTUP_ERRORS:-OFF}
export TRACK_ERRORS=${TRACK_ERRORS:-OFF}
export HTML_ERRORS=${HTML_ERRORS:-ON}
export INCLUDE_PATH=${INCLUDE_PATH:-.:/opt/app-root/src:${PHP_DEFAULT_INCLUDE_PATH}}
export PHP_MEMORY_LIMIT=${PHP_MEMORY_LIMIT:-128M}
export SESSION_NAME=${SESSION_NAME:-PHPSESSID}
export SESSION_HANDLER=${SESSION_HANDLER:-files}
export SESSION_PATH=${SESSION_PATH:-/tmp/sessions}
export SESSION_COOKIE_DOMAIN=${SESSION_COOKIE_DOMAIN:-}
export SESSION_COOKIE_HTTPONLY=${SESSION_COOKIE_HTTPONLY:-}
export SESSION_COOKIE_SECURE=${SESSION_COOKIE_SECURE:-0}
export SHORT_OPEN_TAG=${SHORT_OPEN_TAG:-OFF}

# TODO should be dynamically calculated based on container memory limit/16
export OPCACHE_MEMORY_CONSUMPTION=${OPCACHE_MEMORY_CONSUMPTION:-128}

export OPCACHE_REVALIDATE_FREQ=${OPCACHE_REVALIDATE_FREQ:-2}

export PHPRC=${PHPRC:-${PHP_SYSCONF_PATH}/php.ini}
export PHP_INI_SCAN_DIR=${PHP_INI_SCAN_DIR:-${PHP_SYSCONF_PATH}/php.d}

envsubst < /opt/app-root/etc/php.ini.template > ${PHP_SYSCONF_PATH}/php.ini
envsubst < /opt/app-root/etc/php.d/10-opcache.ini.template > ${PHP_SYSCONF_PATH}/php.d/10-opcache.ini

export HTTPD_START_SERVERS=${HTTPD_START_SERVERS:-8}
export HTTPD_MAX_SPARE_SERVERS=$((HTTPD_START_SERVERS+10))

if [ -n "${NO_MEMORY_LIMIT:-}" -o -z "${MEMORY_LIMIT_IN_BYTES:-}" ]; then
  #
  export HTTPD_MAX_REQUEST_WORKERS=${HTTPD_MAX_REQUEST_WORKERS:-256}
else
  # A simple calculation for MaxRequestWorkers would be: Total Memory / Size Per Apache process.
  # The total memory is determined from the Cgroups and the average size for the
  # Apache process is estimated to 15MB.
  max_clients_computed=$((MEMORY_LIMIT_IN_BYTES/1024/1024/15))
  # The MaxClients should never be lower than StartServers, which is set to 5.
  # In case the container has memory limit set to <64M we pin the MaxClients to 4.
  [[ $max_clients_computed -le 4 ]] && max_clients_computed=4
  export HTTPD_MAX_REQUEST_WORKERS=${HTTPD_MAX_REQUEST_WORKERS:-$max_clients_computed}
  echo "-> Cgroups memory limit is set, using HTTPD_MAX_REQUEST_WORKERS=${HTTPD_MAX_REQUEST_WORKERS}"
fi

# pre-start files
process_extending_files ${APP_DATA}/php-pre-start/ ${PHP_CONTAINER_SCRIPTS_PATH}/pre-start/

exec httpd -D FOREGROUND

3.php源生镜像:

FROM centos/s2i-base-centos7

# This image provides an Apache+PHP environment for running PHP
# applications.

EXPOSE 8080
EXPOSE 8443

# Description
# This image provides an Apache 2.4 + PHP 7.2 environment for running PHP applications.
# Exposed ports:
# * 8080 - alternative port for http

ENV PHP_VERSION=7.2     PHP_VER_SHORT=72     NAME=php     PATH=$PATH:/opt/rh/rh-php72/root/usr/bin

ENV SUMMARY="Platform for building and running PHP $PHP_VERSION applications"     DESCRIPTION="PHP $PHP_VERSION available as container is a base platform for building and running various PHP $PHP_VERSION applications and frameworks. PHP is an HTML-embedded scripting language. PHP attempts to make it easy for developers to write dynamically generated web pages. PHP also offers built-in database integration for several commercial and non-commercial database management systems, so writing a database-enabled webpage with PHP is fairly simple. The most common use of PHP coding is probably as a replacement for CGI scripts."

LABEL summary="${SUMMARY}"       description="${DESCRIPTION}"       io.k8s.description="${DESCRIPTION}"       io.k8s.display-name="Apache 2.4 with PHP ${PHP_VERSION}"       io.openshift.expose-services="8080:http"       io.openshift.tags="builder,${NAME},${NAME}${PHP_VER_SHORT},rh-${NAME}${PHP_VER_SHORT}"       io.openshift.s2i.scripts-url="image:///usr/libexec/s2i"       io.s2i.scripts-url="image:///usr/libexec/s2i"       name="centos/${NAME}-${PHP_VER_SHORT}-centos7"       com.redhat.component="rh-${NAME}${PHP_VER_SHORT}-container"       version="${PHP_VERSION}"       help="For more information visit https://github.com/sclorg/s2i-${NAME}-container"       usage="s2i build https://github.com/sclorg/s2i-php-container.git --context-dir=${PHP_VERSION}/test/test-app centos/${NAME}-${PHP_VER_SHORT}-centos7 sample-server"       maintainer="SoftwareCollections.org <>"

# Install Apache httpd and PHP
RUN yum install -y centos-release-scl &&     INSTALL_PKGS="rh-php72 rh-php72-php rh-php72-php-mysqlnd rh-php72-php-pgsql rh-php72-php-bcmath                   rh-php72-php-gd rh-php72-php-intl rh-php72-php-ldap rh-php72-php-mbstring rh-php72-php-pdo                   rh-php72-php-process rh-php72-php-soap rh-php72-php-opcache rh-php72-php-xml                   rh-php72-php-gmp rh-php72-php-pecl-apcu httpd24-mod_ssl" &&     yum install -y --setopt=tsflags=nodocs $INSTALL_PKGS --nogpgcheck &&     rpm -V $INSTALL_PKGS &&     yum -y clean all --enablerepo=‘*‘

ENV PHP_CONTAINER_SCRIPTS_PATH=/usr/share/container-scripts/php/     APP_DATA=${APP_ROOT}/src     PHP_DEFAULT_INCLUDE_PATH=/opt/rh/rh-php72/root/usr/share/pear     PHP_SYSCONF_PATH=/etc/opt/rh/rh-php72     PHP_HTTPD_CONF_FILE=rh-php72-php.conf     HTTPD_CONFIGURATION_PATH=${APP_ROOT}/etc/conf.d     HTTPD_MAIN_CONF_PATH=/etc/httpd/conf     HTTPD_MAIN_CONF_D_PATH=/etc/httpd/conf.d     HTTPD_VAR_RUN=/var/run/httpd     HTTPD_DATA_PATH=/var/www     HTTPD_DATA_ORIG_PATH=/opt/rh/httpd24/root/var/www     HTTPD_VAR_PATH=/opt/rh/httpd24/root/var     SCL_ENABLED=rh-php72

# Copy the S2I scripts from the specific language image to $STI_SCRIPTS_PATH
COPY ./s2i/bin/ $STI_SCRIPTS_PATH

# Copy extra files to the image.
COPY ./root/ /

# Reset permissions of filesystem to default values
RUN /usr/libexec/container-setup && rpm-file-permissions

USER 1001

# Set the default CMD to print the usage of the language image
CMD $STI_SCRIPTS_PATH/usage

https://github.com/sclorg/s2i-php-container/tree/master/7.2

相关推荐