国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Table of Contents
Docker 如何布置PHP開發(fā)環(huán)境,docker布置php開發(fā)
Home Backend Development PHP Tutorial How Docker arranges PHP development environment, docker arranges PHP development_PHP tutorial

How Docker arranges PHP development environment, docker arranges PHP development_PHP tutorial

Jul 12, 2016 am 08:49 AM
docker development environment black truffle

Docker 如何布置PHP開發(fā)環(huán)境,docker布置php開發(fā)

環(huán)境部署一直是一個很大的問題,無論是開發(fā)環(huán)境還是生產(chǎn)環(huán)境,但是 Docker 將開發(fā)環(huán)境和生產(chǎn)環(huán)境以輕量級方式打包,提供了一致的環(huán)境。極大的提升了開發(fā)部署一致性。當然,實際情況并沒有這么簡單,因為生產(chǎn)環(huán)境和開發(fā)環(huán)境的配置是完全不同的,比如日志等的問題都需要單獨配置,但是至少比以前更加簡單方便了,這里以 PHP 開發(fā)作為例子講解 Docker 如何布置開發(fā)環(huán)境。

一般來說,一個 PHP 項目會需要以下工具:

  1. Web 服務器: Nginx/Tengine
  2. Web 程序: PHP-FPM
  3. 數(shù)據(jù)庫: MySQL/PostgreSQL
  4. 緩存服務: Redis/Memcache

這是最簡單的架構方式,在 Docker 發(fā)展早期,Docker 被大量的濫用,比如,一個鏡像內(nèi)啟動多服務,日志收集依舊是按照 Syslog 或者別的老方式,鏡像容量非常龐大,基礎鏡像就能達到 80M,這和 Docker 當初提出的思想完全南轅北轍了,而 Alpine Linux 發(fā)行版作為一個輕量級 Linux 環(huán)境,就非常適合作為 Docker 基礎鏡像,Docker 官方也推薦使用 Alpine 而不是 Debian 作為基礎鏡像,未來大量的現(xiàn)有官方鏡像也將會遷移到 Alpine 上。本文所有鏡像都將以 Alpine 作為基礎鏡像。

Nginx/Tengine

這部分筆者已經(jīng)在另一篇文章 Docker 容器的 Nginx 實踐中講解了 Tengine 的 Docker 實踐,并且給出了 Dockerfile,由于比較偏好 Tengine,而且官方已經(jīng)給出了 Nginx 的 alpine 鏡像,所以這里就用 Tengine。筆者已經(jīng)將鏡像上傳到官方 DockerHub,可以通過

docker pull chasontang/tengine:2.1.2_f

獲取鏡像,具體請看 Dockerfile。

PHP-FPM

Docker 官方已經(jīng)提供了 PHP 的 7.0.7-fpm-alpine 鏡像,Dockerfile 如下:

FROM alpine:3.4

# persistent / runtime deps
ENV PHPIZE_DEPS \
    autoconf \
    file \
    g++ \
    gcc \
    libc-dev \
    make \
    pkgconf \
    re2c
RUN apk add --no-cache --virtual .persistent-deps \
    ca-certificates \
    curl

# ensure www-data user exists
RUN set -x \
  && addgroup -g 82 -S www-data \
  && adduser -u 82 -D -S -G www-data www-data
# 82 is the standard uid/gid for "www-data" in Alpine
# http://git.alpinelinux.org/cgit/aports/tree/main/apache2/apache2.pre-install?h=v3.3.2
# http://git.alpinelinux.org/cgit/aports/tree/main/lighttpd/lighttpd.pre-install?h=v3.3.2
# http://git.alpinelinux.org/cgit/aports/tree/main/nginx-initscripts/nginx-initscripts.pre-install?h=v3.3.2

ENV PHP_INI_DIR /usr/local/etc/php
RUN mkdir -p $PHP_INI_DIR/conf.d

##<autogenerated>##
ENV PHP_EXTRA_CONFIGURE_ARGS --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data
##</autogenerated>##

ENV GPG_KEYS 1A4E8B7277C42E53DBA9C7B9BCAA30EA9C0D5763

ENV PHP_VERSION 7.0.7
ENV PHP_FILENAME php-7.0.7.tar.xz
ENV PHP_SHA256 9cc64a7459242c79c10e79d74feaf5bae3541f604966ceb600c3d2e8f5fe4794

RUN set -xe \
  && apk add --no-cache --virtual .build-deps \
    $PHPIZE_DEPS \
    curl-dev \
    gnupg \
    libedit-dev \
    libxml2-dev \
    openssl-dev \
    sqlite-dev \
  && curl -fSL "http://php.net/get/$PHP_FILENAME/from/this/mirror" -o "$PHP_FILENAME" \
  && echo "$PHP_SHA256 *$PHP_FILENAME" | sha256sum -c - \
  && curl -fSL "http://php.net/get/$PHP_FILENAME.asc/from/this/mirror" -o "$PHP_FILENAME.asc" \
  && export GNUPGHOME="$(mktemp -d)" \
  && for key in $GPG_KEYS; do \
    gpg --keyserver ha.pool.sks-keyservers.net --recv-keys "$key"; \
  done \
  && gpg --batch --verify "$PHP_FILENAME.asc" "$PHP_FILENAME" \
  && rm -r "$GNUPGHOME" "$PHP_FILENAME.asc" \
  && mkdir -p /usr/src \
  && tar -Jxf "$PHP_FILENAME" -C /usr/src \
  && mv "/usr/src/php-$PHP_VERSION" /usr/src/php \
  && rm "$PHP_FILENAME" \
  && cd /usr/src/php \
  && ./configure \
    --with-config-file-path="$PHP_INI_DIR" \
    --with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \
    $PHP_EXTRA_CONFIGURE_ARGS \
    --disable-cgi \
# --enable-mysqlnd is included here because it's harder to compile after the fact than extensions are (since it's a plugin for several extensions, not an extension in itself)
    --enable-mysqlnd \
# --enable-mbstring is included here because otherwise there's no way to get pecl to use it properly (see https://github.com/docker-library/php/issues/195)
    --enable-mbstring \
    --with-curl \
    --with-libedit \
    --with-openssl \
    --with-zlib \
  && make -j"$(getconf _NPROCESSORS_ONLN)" \
  && make install \
  && { find /usr/local/bin /usr/local/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; } \
  && make clean \
  && runDeps="$( \
    scanelf --needed --nobanner --recursive /usr/local \
      | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
      | sort -u \
      | xargs -r apk info --installed \
      | sort -u \
  )" \
  && apk add --no-cache --virtual .php-rundeps $runDeps \
  && apk del .build-deps

COPY docker-php-ext-* /usr/local/bin/

##<autogenerated>##
WORKDIR /var/www/html

RUN set -ex \
  && cd /usr/local/etc \
  && if [ -d php-fpm.d ]; then \
    # for some reason, upstream's php-fpm.conf.default has "include=NONE/etc/php-fpm.d/*.conf"
    sed 's!=NONE/!=!g' php-fpm.conf.default | tee php-fpm.conf > /dev/null; \
    cp php-fpm.d/www.conf.default php-fpm.d/www.conf; \
  else \
    # PHP 5.x don't use "include=" by default, so we'll create our own simple config that mimics PHP 7+ for consistency
    mkdir php-fpm.d; \
    cp php-fpm.conf.default php-fpm.d/www.conf; \
    { \
      echo '[global]'; \
      echo 'include=etc/php-fpm.d/*.conf'; \
    } | tee php-fpm.conf; \
  fi \
  && { \
    echo '[global]'; \
    echo 'error_log = /proc/self/fd/2'; \
    echo; \
    echo '[www]'; \
    echo '; if we send this to /proc/self/fd/1, it never appears'; \
    echo 'access.log = /proc/self/fd/2'; \
    echo; \
    echo 'clear_env = no'; \
    echo; \
    echo '; Ensure worker stdout and stderr are sent to the main error log.'; \
    echo 'catch_workers_output = yes'; \
  } | tee php-fpm.d/docker.conf \
  && { \
    echo '[global]'; \
    echo 'daemonize = no'; \
    echo; \
    echo '[www]'; \
    echo 'listen = [::]:9000'; \
  } | tee php-fpm.d/zz-docker.conf

EXPOSE 9000
CMD ["php-fpm"]
##</autogenerated>##

首先,鏡像繼承自 alpine:3.4 鏡像,使用 apk 命令安裝 php 最小依賴,同時添加 www-data 作為 php-fpm 的運行用戶,將 php 的配置文件指定到 /usr/local/etc/php,然后就是下載 php-src,編譯安裝,這里可以參考筆者之前寫的 php 編譯安裝文章。參數(shù)都中規(guī)中矩。安裝目錄被指定到 /usr/local,然后使用 scanelf 獲得所依賴的運行庫列表,并且將其他安裝包刪除。將 docker-php-ext-configure、docker-php-ext-enable、docker-php-ext-install 復制到容器中,這三個文件用于后續(xù)安裝擴展。然后將 php-fpm.conf 復制到配置目錄,將 error_log 和 access_log 指定到終端標準輸出,daemonize = no 表示不以服務進程運行。EXPOSE 9000 端口用于和其他容器通信,然后就是 CMD ["php-fpm"] 運行 php-fpm。而且工作目錄被指定到 /var/www/html。

docker-compose

已經(jīng)搞定了基礎鏡像,我們就可以使用基礎鏡像來配置容器,但是通過手工 docker 命令啟動容器會非常麻煩。但是萬幸的是官方已經(jīng)提供了 docker-compose 命令來編排容器,只需要寫一個 docker-compose.yaml 文件就行,具體可以參考官方文檔。

version: '2'
services:
 php-fpm:
  image: php:7.0.7-fpm-alpine
  volumes:
   - "./src:/var/www/html"
  restart: always

 tengine:
  depends_on:
   - php-fpm
  links:
   - php-fpm
  image: chasontang/tengine:2.1.2_f
  volumes:
   - "./nginx.vh.default.conf:/etc/nginx/conf.d/default.conf"
  ports:
   - "80:80"
  restart: always

非常容易理解,這里定義了兩個服務,php-fpm 依賴 php:7.0.7-fpm-alpine 鏡像,并且將 src 文件夾映射為 /var/www/html 文件夾,tengine 服務依賴 php-fpm 服務,并且 link php-fpm 服務,這樣就能通過網(wǎng)絡與 php-fpm 容器通信,tengine 服務基于 chasontang/tengine:2.1.2_f 鏡像,并將 nginx.vh.default.conf 文件映射為 /etc/nginx/conf.d/default.conf 文件。然后來看 nginx.vh.default.conf

server {
  listen    80;
  server_name localhost;

  #charset koi8-r;

  #access_log logs/host.access.log main;

  location / {
    root  html;
    index index.html index.htm;
  }

  #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  html;
  }

  # proxy the PHP scripts to Apache listening on 127.0.0.1:80
  #
  #location ~ \.php$ {
  #  proxy_pass  http://127.0.0.1;
  #}

  location ~ [^/]\.php(/|$) {
    fastcgi_split_path_info ^(.+&#63;\.php)(/.*)$;
    fastcgi_pass php-fpm:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
    include fastcgi_params;
  }

  # deny access to .htaccess files, if Apache's document root
  # concurs with nginx's one
  #
  #location ~ /\.ht {
  #  deny all;
  #}
}

tengine 鏡像實際上使用兩個配置文件,一個是 /etc/nginx/nginx.conf,還有就是 /etc/nginx/conf.d/ 目錄下的所有文件,因為 /etc/nginx/nginx.conf 中使用 include /etc/nginx/conf.d/*.conf; 包含了這個目錄,也就是說,可以不需要去管 nginx 其他配置,只需要用自己的 nginx 虛擬主機配置替代默認的虛擬主機配置,或者說增加虛擬主機配置就行了。

從上面可以看到,default.conf 文件定義了一個 location 匹配包含 .php 的 URL,然后將其分割出 PATH_INFO 參數(shù),將這些變量傳遞給 php-fpm:9000 的 php-fpm 服務。

這里需要注意的是,由于 Nginx 和 PHP-FPM 不在同一臺主機上,所以 Nginx 只做靜態(tài)文件處理和路由轉(zhuǎn)發(fā),實際的 PHP 文件執(zhí)行時在 PHP-FPM 容器中發(fā)生的。所以 SCRIPT_FILENAME 變量必須要使用 PHP-FPM 容器中的目錄,所以這里使用硬編碼指定。當然,也可以讓兩個容器共享同一個數(shù)據(jù)卷,但是筆者認為,這只是為了方便容器編排,其他完全沒有好處。

It’s easy! Now we can quickly start and update the environment, but there are still many areas that need improvement.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1136621.htmlTechArticleDocker How to arrange the PHP development environment, docker layout PHP development environment deployment has always been a big problem, whether it is development The environment is still the production environment, but Docker combines the development environment and production...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to exit the container by docker How to exit the container by docker Apr 15, 2025 pm 12:15 PM

Four ways to exit Docker container: Use Ctrl D in the container terminal Enter exit command in the container terminal Use docker stop &lt;container_name&gt; Command Use docker kill &lt;container_name&gt; command in the host terminal (force exit)

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] &lt;Container Path&gt; &lt;Host Path&gt;. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to restart docker How to restart docker Apr 15, 2025 pm 12:06 PM

How to restart the Docker container: get the container ID (docker ps); stop the container (docker stop &lt;container_id&gt;); start the container (docker start &lt;container_id&gt;); verify that the restart is successful (docker ps). Other methods: Docker Compose (docker-compose restart) or Docker API (see Docker documentation).

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

How to create containers for docker How to create containers for docker Apr 15, 2025 pm 12:18 PM

Create a container in Docker: 1. Pull the image: docker pull [mirror name] 2. Create a container: docker run [Options] [mirror name] [Command] 3. Start the container: docker start [Container name]

How to view logs from docker How to view logs from docker Apr 15, 2025 pm 12:24 PM

The methods to view Docker logs include: using the docker logs command, for example: docker logs CONTAINER_NAME Use the docker exec command to run /bin/sh and view the log file, for example: docker exec -it CONTAINER_NAME /bin/sh ; cat /var/log/CONTAINER_NAME.log Use the docker-compose logs command of Docker Compose, for example: docker-compose -f docker-com

See all articles