亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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)境部署一直是一個(gè)很大的問(wèn)題,無(wú)論是開發(fā)環(huán)境還是生產(chǎn)環(huán)境,但是 Docker 將開發(fā)環(huán)境和生產(chǎn)環(huán)境以輕量級(jí)方式打包,提供了一致的環(huán)境。極大的提升了開發(fā)部署一致性。當(dāng)然,實(shí)際情況并沒(méi)有這么簡(jiǎn)單,因?yàn)樯a(chǎn)環(huán)境和開發(fā)環(huán)境的配置是完全不同的,比如日志等的問(wèn)題都需要單獨(dú)配置,但是至少比以前更加簡(jiǎn)單方便了,這里以 PHP 開發(fā)作為例子講解 Docker 如何布置開發(fā)環(huán)境。

一般來(lái)說(shuō),一個(gè) PHP 項(xiàng)目會(huì)需要以下工具:

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

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

Nginx/Tengine

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

docker pull chasontang/tengine:2.1.2_f

獲取鏡像,具體請(qǐng)看 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 最小依賴,同時(shí)添加 www-data 作為 php-fpm 的運(yùn)行用戶,將 php 的配置文件指定到 /usr/local/etc/php,然后就是下載 php-src,編譯安裝,這里可以參考筆者之前寫的 php 編譯安裝文章。參數(shù)都中規(guī)中矩。安裝目錄被指定到 /usr/local,然后使用 scanelf 獲得所依賴的運(yùn)行庫(kù)列表,并且將其他安裝包刪除。將 docker-php-ext-configure、docker-php-ext-enable、docker-php-ext-install 復(fù)制到容器中,這三個(gè)文件用于后續(xù)安裝擴(kuò)展。然后將 php-fpm.conf 復(fù)制到配置目錄,將 error_log 和 access_log 指定到終端標(biāo)準(zhǔn)輸出,daemonize = no 表示不以服務(wù)進(jìn)程運(yùn)行。EXPOSE 9000 端口用于和其他容器通信,然后就是 CMD ["php-fpm"] 運(yùn)行 php-fpm。而且工作目錄被指定到 /var/www/html。

docker-compose

已經(jīng)搞定了基礎(chǔ)鏡像,我們就可以使用基礎(chǔ)鏡像來(lái)配置容器,但是通過(guò)手工 docker 命令啟動(dòng)容器會(huì)非常麻煩。但是萬(wàn)幸的是官方已經(jīng)提供了 docker-compose 命令來(lái)編排容器,只需要寫一個(gè) 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

非常容易理解,這里定義了兩個(gè)服務(wù),php-fpm 依賴 php:7.0.7-fpm-alpine 鏡像,并且將 src 文件夾映射為 /var/www/html 文件夾,tengine 服務(wù)依賴 php-fpm 服務(wù),并且 link php-fpm 服務(wù),這樣就能通過(guò)網(wǎng)絡(luò)與 php-fpm 容器通信,tengine 服務(wù)基于 chasontang/tengine:2.1.2_f 鏡像,并將 nginx.vh.default.conf 文件映射為 /etc/nginx/conf.d/default.conf 文件。然后來(lái)看 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 鏡像實(shí)際上使用兩個(gè)配置文件,一個(gè)是 /etc/nginx/nginx.conf,還有就是 /etc/nginx/conf.d/ 目錄下的所有文件,因?yàn)?/etc/nginx/nginx.conf 中使用 include /etc/nginx/conf.d/*.conf; 包含了這個(gè)目錄,也就是說(shuō),可以不需要去管 nginx 其他配置,只需要用自己的 nginx 虛擬主機(jī)配置替代默認(rèn)的虛擬主機(jī)配置,或者說(shuō)增加虛擬主機(jī)配置就行了。

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

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

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)

.NET Core Quick Start Tutorial 1. The beginning: Talking about .NET Core .NET Core Quick Start Tutorial 1. The beginning: Talking about .NET Core May 07, 2025 pm 04:54 PM

1. The Origin of .NETCore When talking about .NETCore, we must not mention its predecessor .NET. Java was in the limelight at that time, and Microsoft also favored Java. The Java virtual machine on the Windows platform was developed by Microsoft based on JVM standards. It is said to be the best performance Java virtual machine at that time. However, Microsoft has its own little abacus, trying to bundle Java with the Windows platform and add some Windows-specific features. Sun's dissatisfaction with this led to a breakdown of the relationship between the two parties, and Microsoft then launched .NET. .NET has borrowed many features of Java since its inception and gradually surpassed Java in language features and form development. Java in version 1.6

What are the cross-platform development environments in C? What are the cross-platform development environments in C? Apr 28, 2025 pm 09:24 PM

Cross-platform development in C is recommended to use VisualStudioCode, CLion and QtCreator. 1. VisualStudioCode is lightweight and flexible, suitable for multi-platform development, but the initial configuration is more complicated. 2. CLion integrates CMake, suitable for cross-platform projects, but licenses are expensive. 3.QtCreator supports cross-platform development, with built-in Qt library, but the learning curve is steep.

How to develop a complete Python Web application? How to develop a complete Python Web application? May 23, 2025 pm 10:39 PM

To develop a complete Python Web application, follow these steps: 1. Choose the appropriate framework, such as Django or Flask. 2. Integrate databases and use ORMs such as SQLAlchemy. 3. Design the front-end and use Vue or React. 4. Perform the test, use pytest or unittest. 5. Deploy applications, use Docker and platforms such as Heroku or AWS. Through these steps, powerful and efficient web applications can be built.

Docker vs. Kubernetes: Key Differences and Synergies Docker vs. Kubernetes: Key Differences and Synergies May 01, 2025 am 12:09 AM

Docker and Kubernetes are leaders in containerization and orchestration. Docker focuses on container lifecycle management and is suitable for small projects; Kubernetes is good at container orchestration and is suitable for large-scale production environments. The combination of the two can improve development and deployment efficiency.

What is cross-compilation in C? What is cross-compilation in C? Apr 28, 2025 pm 08:21 PM

Cross-compilation in C refers to compiling an executable file or library that can run on another platform on one platform. 1) Cross-compilation requires the use of a special cross-compiler, such as GCC or Clang variants. 2) Setting up a cross-compilation environment can use Docker to manage toolchains to improve repeatability and portability. 3) When cross-compiling, pay attention to code optimization options, such as -O2, -O3 or -Os, to balance performance and file size.

How to view process information inside Docker container How to view process information inside Docker container May 19, 2025 pm 09:06 PM

There are three ways to view the process information inside the Docker container: 1. Use the dockertop command to list all processes in the container and display PID, user, command and other information; 2. Use dockerexec to enter the container, and then use the ps or top command to view detailed process information; 3. Use the dockerstats command to display the usage of container resources in real time, and combine dockertop to fully understand the performance of the container.

How to deploy a PyTorch app on Ubuntu How to deploy a PyTorch app on Ubuntu May 29, 2025 pm 11:18 PM

Deploying a PyTorch application on Ubuntu can be done by following the steps: 1. Install Python and pip First, make sure that Python and pip are already installed on your system. You can install them using the following command: sudoaptupdatesudoaptinstallpython3python3-pip2. Create a virtual environment (optional) To isolate your project environment, it is recommended to create a virtual environment: python3-mvenvmyenvsourcemyenv/bin/activatet

Performance Tuning of Jenkins Deployment on Debian Performance Tuning of Jenkins Deployment on Debian May 28, 2025 pm 04:51 PM

Deploying and tuning Jenkins on Debian is a process involving multiple steps, including installation, configuration, plug-in management, and performance optimization. Here is a detailed guide to help you achieve efficient Jenkins deployment. Installing Jenkins First, make sure your system has a Java environment installed. Jenkins requires a Java runtime environment (JRE) to run properly. sudoaptupdatesudoaptininstallopenjdk-11-jdk Verify that Java installation is successful: java-version Next, add J

See all articles