Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Docker to layout Development Environment in PHP

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how to use Docker to lay out the development environment in PHP. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Docker practice developed by PHP

Environmental deployment has always been a big problem, whether it is the development environment or the production environment, but Docker packages the development environment and the production environment in a lightweight manner, providing a consistent environment. Greatly improves the consistency of development and deployment. Of course, the actual situation is not that simple, because the configuration of the production environment and the development environment is completely different, such as logs and other problems need to be configured separately, but at least it is more simple and convenient than before. Here we take PHP development as an example to explain how Docker arranges the development environment.

In general, a PHP project requires the following tools:

Web server: Nginx/Tengine

Web program: PHP-FPM

Database: MySQL/PostgreSQL

Caching service: Redis/Memcache

This is the simplest way of architecture. In the early days of the development of Docker, Docker was abused a lot. For example, multiple services are started within an image, and log collection is still based on Syslog or other old methods. The image capacity is very large, and the basic image can reach 80m, which is completely contrary to the idea put forward by Docker. As a lightweight Linux environment, Alpine Linux distribution is very suitable as a basic image of Docker. Docker officials also recommend using Alpine instead of Debian as the basic image, and a large number of existing official images will be migrated to Alpine in the future. All images in this article will be based on Alpine.

Nginx/Tengine

In this part, the author has explained the Nginx practice of Tengine in another article, the Nginx practice of Tengine container, and given Dockerfile. Because of the preference for Tengine, and the official alpine image of Nginx, Tengine is used here. The author has uploaded the image to the official DockerHub, which can be accessed through

Docker pull chasontang/tengine:2.1.2_f

Get the image. For more information, please see Dockerfile.

PHP-FPM

Docker has officially provided the 7.0.7-fpm-alpine image of PHP. The Dockerfile is as follows:

FROM alpine:3.4# persistent / runtime depsENV PHPIZE_DEPS\ autoconf\ file\ re2cRUN apk add +\ gcc\ libc-dev\ make\ pkgconf\ re2cRUN apk add-no-cache-- virtual. Persistent-deps\ ca-certificates\ curl# ensure www-data user existsRUN 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.2ENV PHP_INI_DIR/ usr/local/etc/phpRUN mkdir-p $PHP_INI_DIR/conf.d####ENV PHP_EXTRA_CONFIGURE_ARGS-enable-fpm-with-fpm-user=www-data-with-fpm-group=www-data####ENV GPG_KEYS 1A4E8B7277C42E53DBA9C7B9BCAA30EA9C0D5763ENV PHP_VERSION 7.0.7ENV PHP_FILENAME php-7.0.7.tar.xzENV PHP_SHA256 9cc64a7459242c79c10e79d74feaf5bae3541f604966ceb600c3d2e8f5fe4794RUN 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-depsCOPY docker-php-ext-* / usr/local/bin/####WORKDIR / var/www/htmlRUN 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 purchase NONEAGO 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 = [::]: 9000cycles;\} | tee php-fpm.d/zz-docker.confEXPOSE 9000CMD ["php-fpm"] #

First of all, the image inherits from the alpine:3.4 image, use the apk command to install the minimum dependency of php, and add www-data as the running user of php-fpm, specify the configuration file of php to / usr/local/etc/php, and then download php-src, compile and install. Here you can refer to the php compilation and installation article I wrote earlier. The parameters are all in order. The installation directory is assigned to / usr/local, then use scanelf to get a list of dependent runtimes, and remove other installation packages. Copy docker-php-ext-configure, docker-php-ext-enable, and docker-php-ext-install into the container, which are used for subsequent installation of the extension. Then copy the php-fpm.conf to the configuration directory, specify error_log and access_log to the terminal standard output, and daemonize = no means that it does not run as a service process. 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

Now that the basic image is done, we can use the basic image to configure the container, but it is very troublesome to start the container with the manual docker command. But fortunately, officials have provided docker-compose commands to orchestrate containers. You only need to write a docker-compose.yaml file. For more information, please refer to the official documentation.

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's easy to understand that two services are defined here, php-fpm relies on php:7.0.7-fpm-alpine images, and maps the src folder to / var/www/html folder, tengine services rely on php-fpm services, and link php-fpm services, so that they can communicate with php-fpm containers over the network, and tengine services are based on chasontang/tengine:2.1.2_f images. And map the nginx.vh.default.conf file to the / etc/nginx/conf.d/default.conf file. Then let's look at 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 proxy the PHP scripts to Apache listening on 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's one # # location ~ /\ .ht {# deny all; #}}

The tengine image actually uses two configuration files, one is / etc/nginx/nginx.conf, and all the files in the / etc/nginx/conf.d/ directory, because include / etc/nginx/conf.d/*.conf is used in / etc/nginx/nginx.conf This directory is included, that is, you don't need to worry about other nginx configurations, just replace the default virtual host configuration with your own nginx virtual host configuration, or add the virtual host configuration.

As you can see from the above, the default.conf file defines a location that matches the URL containing .php, then splits it into the PATH_INFO parameter and passes these variables to php-fpm:9000 's php-fpm service.

It should be noted here that since Nginx and PHP-FPM are not on the same host, Nginx only does static file processing and routing forwarding, which occurs in the PHP-FPM container when the actual PHP file is executed. So the SCRIPT_FILENAME variable must use the directory in the PHP-FPM container, so it is hard-coded here. Of course, it is possible for two containers to share the same data volume, but the author believes that this is only for the convenience of container orchestration, and the rest is of no benefit at all.

Thank you for reading! This is the end of this article on "how to use Docker to layout the development environment in PHP". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report