Docker how to decorate the PHP development environment _php Skills

Source: Internet
Author: User
Tags curl fpm gpg mkdir openssl php compiler php development environment

Environmental deployment has been a major problem, both in the development and production environments, but Docker provides a consistent environment for packaging the development environment and production environment in lightweight fashion. Greatly enhance the development deployment consistency. Of course, the actual situation is not so simple, because the production environment and the development environment configuration is completely different, such as logs and other issues need to be configured separately, but at least more convenient than before, here with PHP development as an example to explain how Docker layout development environment.

In general, a PHP project will require the following tools:

    1. Web Server: Nginx/tengine
    2. Web Program: PHP-FPM
    3. Database: Mysql/postgresql
    4. Caching services: Redis/memcache

This is the simplest architecture, in the early days of Docker development, Docker was a lot of abuse, for example, a mirror to start multiple services, log collection is still in accordance with the Syslog or other old way, mirrored capacity is very large, the basic image can reach 80M, this and Docker The idea was completely different, and the Alpine Linux distribution, as a lightweight Linux environment, was ideal for Docker base mirroring, and Docker officially recommended using Alpine instead of Debian as a base image, and the future of a large number of existing official mirrors will be migrated to the Alpine. All mirrors in this article will be Alpine as the underlying mirror.

Nginx/tengine

This part of the author has in another article Docker container Nginx practice explained Tengine Docker practice, and gave Dockerfile, because of the preference Tengine, and the official has given Nginx Alpine mirror, so here Just use Tengine. The author has uploaded the image to the official Dockerhub, through

<code>docker Pull Chasontang/tengine:2.1.2_f</code>

To get the mirror, please see dockerfile.

php-fpm

Docker official has provided the 7.0.7-fpm-alpine mirroring of PHP, Dockerfile as follows:

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 ww W-data Www-data # The standard uid/gid for ' Www-data ' in Alpine # http://git.alpinelinux.org/cgit/aports/tree/main/a pache2/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 \ Sqli  Te-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"; && gpg--batch--verify "$PHP _filename.asc" "$PHP _filename" && rm-r "$GNUPGHOME" "$PHP _f Ilename.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 (since it ' s a plugin for several extensions, not a extension in itself)--enable-mysqlnd \ #--enable-mbstring-is Inc luded here because otherwise there ' s no way-to-get pecl to-use it properly  es/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-t ype f-perm +0111-exec strip--strip-all ' {} ' + | | True } \ && make clean \ && rundeps= $ (\ scanelf--needed--nobanner --recursive/usr/local \ | awk ' {gsub (/,/, "\nso:", $); Print "So:" $} ' | 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/loca L/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 this mimics PHP 7+ for con Sistency 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 \ &AMP;&Amp {\ echo ' [global] '; \ Echo ' error_log =/proc/self/fd/2 '; \ echo; \ echo ' [www] '; \ echo '; If we sen D 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 ' [WW W] '; \ echo ' Listen = [::]:9000 '; \
  } |

 Tee php-fpm.d/zz-docker.conf Expose 9000 CMD ["PHP-FPM"] ##</autogenerated>##

First, the mirror inherits from the alpine:3.4 Mirror, installs the PHP minimum dependency using the APK command, adds Www-data as the php-fpm running user, assigns the PHP configuration file to/usr/local/etc/php, and then downloads the PHP-SR c, compile the installation, here can refer to the author before writing the PHP compiler installation article. The parameters are all in the same. The installation directory is assigned to/usr/local and then uses scanelf to obtain the list of runtime dependencies that are dependent on it, and to remove other installation packages. Copy docker-php-ext-configure, docker-php-ext-enable, Docker-php-ext-install to the container, which is used for subsequent installation extensions. The php-fpm.conf is then copied to the configuration directory, Error_log and Access_log are specified to the terminal standard output, Daemonize = no indicates that the service process is not running. The expose 9000 port is used to communicate with other containers, and then CMD ["PHP-FPM"] runs PHP-FPM. and the working directory is assigned to/var/www/html.

Docker-compose

With the underlying image in order, we can configure the container using the underlying image, but it can be cumbersome to start the container by manually docker the command. But it is fortunate that the authorities have provided the Docker-compose order to arrange the container, just write a Docker-compose.yaml file, you can refer to the official documents.

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

It is very easy to understand that there are two services defined here, PHP-FPM relies on php:7.0.7-fpm-alpine mirroring, and maps the SRC folder to the/var/www/html folder, Tengine service relies on the PHP-FPM service, and link php-f PM service, which communicates with the PHP-FPM container over the network, Tengine services based on Chasontang/tengine:2.1.2_f mirroring, and maps nginx.vh.default.conf files to/etc/nginx/ conf.d/default.conf files. and see 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 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 ^ (. +?\.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 ' one # #location ~/\.ht {
  # Deny All; #}
}

Tengine mirroring actually uses two profiles, one is/etc/nginx/nginx.conf, and all the files in the/etc/nginx/conf.d/directory, because/etc/nginx/nginx.conf uses include /etc/nginx/conf.d/*.conf; Contains this directory, that is to say, you can not need to nginx other configuration, just use your own Nginx virtual host configuration instead of the default virtual host configuration, or increase the virtual host configuration on the line.

As you can see from the above, the default.conf file defines a location matching URL that contains. php, and then splits it out of the path_info parameters and passes these variables to the php-fpm:9000 PHP-FPM service.

It should be noted here that because Nginx and php-fpm are not on the same host, so Nginx only do static file processing and routing forwarding, the actual PHP file execution occurs in the PHP-FPM container. So the script_filename variable must use the directory in the PHP-FPM container, so the hard-coded designation is used here. Of course, you can have two containers share the same data volume, but I think that this is only for the convenience of container choreography, others are not good at all.

It's easy! Now we can quickly start and update the environment, but there are still a lot of places to improve.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.