Avoid the pit! Use Docker to build a PHP development environment

藏色散人
Release: 2023-04-07 07:02:01
forward
12840 people have browsed it

Avoid the pit! Use Docker to build PHP development environment (Mac, Docker, Nginx, PHP-FPM, XDebug, PHPStorm, VSCode)

Because I recently changed my computer, I need to redeploy the local development environment. The virtual machine used before The solution takes up too much disk space, and I feel sorry for the small space of the SSD, so this time I adopted the Docker solution for deployment.

Regarding the principle of this deployment, my idea is to use official images as much as possible and try not to change or change the images as little as possible. Because it is a local development environment, I want to try new things and try to use higher version software to implement it.

In terms of results, I hope it can be of some help to students who also need to build an environment and avoid pitfalls. It would be best if you can follow the steps in this article and get it done in one go.

Let’s talk about the environment first:

Docker 18.09.2 Nginx 1.17.1 PHP 7.3.7 XDebug 2.7.2 PhpStorm 2019.1.3 VSCode 1.36.1
Copy after login

1. Docker installation

Here you can download Docker for Mac directly from the Docker official website. Just follow the prompts and won’t go into details here.

2. Install Nginx

Address: https://hub.docker.com/_/nginx

Directly execute docker pull nginx pull The latest image;

According to the above address, we can see some official documents provided. We can just follow the operation. The solution I personally adopt is to first copy the entire configuration directory of nginx to a local copy, and then Bind the directory to the configuration directory of the nginx container during runtime, making it more convenient to modify the configuration.

Copy the nginx configuration directory to the local:

$ docker run --name tmp-nginx -d nginx $ docker cp tmp-nginx:/etc/nginx /Users/yourname/Workspace/etc/nginx $ docker rm -f tmp-nginx
Copy after login

Instructions: /Users/yourname/Workspace/etc, this is my personal working directory, you can change it to your own according to the situation; The function of the command is to start an nginx container running in the background, copy the configuration directory, terminate the operation and delete the container.

You can run it first to see the effect:

$ docker run --name run-nginx -d -p 80:80 -v /Users/yourname/Workspace/www:/usr/share/nginx/html:ro nginx
Copy after login

Description: The -v parameter binds a local directory to the web directory in the nginx container. There is no binding configuration directory. You can Create a hello.html in the web directory, and access http://localhost/hello.html through the browser to see the effect. After that, you can delete the container first, and then start it after changing the configuration.

Deletion method:

$ docker rm -f run-nginx
Copy after login

3. Install php-fpm

Address: https://hub.docker.com/_/php

Because we need to install some PHP extensions used in development, the best way is to generate our own image based on the Dockerfile. The following is my Dockerfile. You can refer to it. You can delete the unnecessary ones according to the situation. expansion, otherwise the generated image will be larger.

The content of Dockerfile is as follows:

# 从官方基础版本构建 FROM php:7.3.7-fpm # 官方版本默认安装扩展: # Core, ctype, curl # date, dom # fileinfo, filter, ftp # hash # iconv # json # libxml # mbstring, mysqlnd # openssl # pcre, PDO, pdo_sqlite, Phar, posix # readline, Reflection, session, SimpleXML, sodium, SPL, sqlite3, standard # tokenizer # xml, xmlreader, xmlwriter # zlib # 更新为国内镜像 RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \ && echo 'deb http://mirrors.163.com/debian/ stretch main non-free contrib' > /etc/apt/sources.list \ && echo 'deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib' >> /etc/apt/sources.list \ && echo 'deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib' >> /etc/apt/sources.list \ && apt-get update # bcmath, calendar, exif, gettext, sockets, dba, # mysqli, pcntl, pdo_mysql, shmop, sysvmsg, sysvsem, sysvshm 扩展 RUN docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm iconv # GD 扩展 RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev \ && rm -r /var/lib/apt/lists/* \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) gd # imagick 扩展 RUN export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" \ && apt-get install -y --no-install-recommends libmagickwand-dev \ && rm -r /var/lib/apt/lists/* \ && pecl install imagick-3.4.4 \ && docker-php-ext-enable imagick # mcrypt 扩展 RUN apt-get install -y --no-install-recommends libmcrypt-dev \ && rm -r /var/lib/apt/lists/* \ && pecl install mcrypt-1.0.2 \ && docker-php-ext-enable mcrypt # Memcached 扩展 RUN apt-get install -y --no-install-recommends libmemcached-dev zlib1g-dev \ && rm -r /var/lib/apt/lists/* \ && pecl install memcached-3.1.3 \ && docker-php-ext-enable memcached # redis 扩展 RUN pecl install redis-5.0.0 && docker-php-ext-enable redis # opcache 扩展 RUN docker-php-ext-configure opcache --enable-opcache && docker-php-ext-install opcache # xdebug 扩展 RUN pecl install xdebug-2.7.2 && docker-php-ext-enable xdebug # swoole 扩展 RUN pecl install swoole-4.4.0 && docker-php-ext-enable swoole # 镜像信息 LABEL Author="Stone" LABEL Version="2019.7" LABEL Description="PHP 7.3.7 开发环境镜像.
Copy after login

Note: I referred to the author’s content of https://www.jianshu.com/p/20fcca06e27e for this Dockerfile and made it You can increase or decrease some adjustments according to your own situation. Because it is a development environment, it is best to keep xdebug, which we will also use later;

The following is the Dockerfile that connects RUN in series, so that the generated image can It is smaller, but still 636M. The official php:7.3.7-fpm image is 371M. If you want to optimize, you can also generate it from the official Alpine image. Let’s keep it simple and crude here.

FROM php:7.3.7-fpm RUN mv /etc/apt/sources.list /etc/apt/sources.list.bak \ && echo 'deb http://mirrors.163.com/debian/ stretch main non-free contrib' > /etc/apt/sources.list \ && echo 'deb http://mirrors.163.com/debian/ stretch-updates main non-free contrib' >> /etc/apt/sources.list \ && echo 'deb http://mirrors.163.com/debian-security/ stretch/updates main non-free contrib' >> /etc/apt/sources.list \ && apt-get update \ && export CFLAGS="$PHP_CFLAGS" CPPFLAGS="$PHP_CPPFLAGS" LDFLAGS="$PHP_LDFLAGS" \ && apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev libmagickwand-dev libmcrypt-dev libmemcached-dev zlib1g-dev \ && rm -rf /var/lib/apt/lists/* \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) bcmath calendar exif gettext sockets dba mysqli pcntl pdo_mysql shmop sysvmsg sysvsem sysvshm iconv gd \ && pecl install imagick-3.4.4 mcrypt-1.0.2 memcached-3.1.3 redis-5.0.0 xdebug-2.7.2 swoole-4.4.0\ && docker-php-ext-enable imagick mcrypt memcached redis xdebug swoole \ && docker-php-ext-configure opcache --enable-opcache && docker-php-ext-install opcache LABEL Author="Stone" LABEL Version="2019.7" LABEL Description="PHP 7.3.7 开发环境镜像. "
Copy after login

Let’s talk about the pitfalls: it is best to change the source of apt-get to a domestic source, otherwise it may get stuck when building the image; because by default the official container is based on Debian, search for the source Many of them are outdated. For example, the current Debian codename is stretch, but if you use jessie's package, you will definitely get an error, "E: Unable to correct problems, you have held broken packages."; you can directly follow what I said above. Just use the Dockerfile to generate the image, and the test passes.

Execute in the directory where the Dockerfile is located:

docker build -t my-php-fpm:2019.7 .
Copy after login

-t Parameters set the image name and label. Please name it according to your own situation. After the image is created, you can copy the nginx configuration as above, Also copy the relevant configuration of php to the local area.

$ docker run --name tmp-my-php-fpm -d my-php-fpm:2019.7 $ docker cp tmp-my-php-fpm:/usr/local/etc /Users/yourname/Workspace/etc/php $ docker rm -f tmp-my-php-fpm
Copy after login

4. Modify the configuration files of nginx, php-fpm, and xdebug

Modify the nginx configuration file and open / Users/yourname/Workspace/etc/nginx/default .conf, add the following content:

location ~ \.php$ { fastcgi_pass php-fpm-container:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html$fastcgi_script_name; fastcgi_param SCRIPT_NAME $fastcgi_script_name; include fastcgi_params; }
Copy after login

There is one point in the added content that needs to be explained, "php-fpm-container" This is the alias of the php-fpm container we created ourselves, specifically specified at run time Yes, we will mention it later.

Modify the php related configuration files, go to /Users/yourname/Workspace/etc/php, copy php.ini-development and rename it to php.ini, and modify the configuration content according to your own situation.

Modify the xdebug configuration file, /Users/yourname/Workspace/etc/php/conf.d/docker-php-ext-xdebug.ini, and add the following content

xdebug.remote_enable = On xdebug.remote_handler = dbgp xdebug.remote_host = host.docker.internal xdebug.remote_port = 9001 xdebug.remote_log = /var/log/php/xdebug.log xdebug.idekey = PHPSTOR
Copy after login

The above settings are mainly It turns on the remote debugging mode of xdebug. Because php-fpm uses port 9000, here we change the port of xdebug to 9001; "host.docker.internal" is newly added in docker 18.03 and can be parsed to obtain the host's IP address. , so you don’t have to write down the IP address.

5. Start php-fpm and nginx containers

$ docker run --name run-my-php-fpm \ -v /Users/yourname/Workspace/www:/var/www/html \ -v /Users/yourname/Workspace/etc/php:/usr/local/etc \ -v /Users/yourname/Workspace/log/php:/var/log/php \ -d my-php-fpm:2019.7 $ docker run --name run-nginx \ -p 80:80 \ --link run-my-php-fpm:php-fpm-container \ -v /Users/yourname/Workspace/www:/usr/share/nginx/html \ -v /Users/yourname/Workspace/etc/nginx:/etc/nginx \ -v /Users/yourname/Workspace/log/nginx:/var/log/nginx \ -d nginx
Copy after login

说明:我将配置目录、日志目录和 web 目录都分别进行了绑定,方便在本地环境中修改。还记得上面提到的 nginx 中有个配置填写的 “php-fpm-container” 吗?实际上就是在这里指定的,连接两个容器,并给 php-fpm 容器起了个别名,配置中通过别名访问。

在 / Users/yourname/Workspace/www 中创建一个 phpinfo.php 文件,输出 php 信息,通过 http://localhost/phpinfo.php 访问来查看。

Avoid the pit! Use Docker to build a PHP development environment

6、安装 Chrome 浏览器插件 xdebug helper

可能会被墙,请自行解决。

7、PhpStorm 调试环境配置

建立 / Users/yourname/Workspace/www/xdebug/demo.php,php 文件里面随便写点 php 代码,也可以通过 PhpStorm 新建立一个空项目,但是位置要在我们和 php-fpm 容器绑定的目录。

菜单:PhpStorm->Preferences… 进入偏好设置,按照下图进行设置。

Avoid the pit! Use Docker to build a PHP development environment

根据上图提示进入 CLI Interpreter 设置窗口,点 “+” 添加配置。

Avoid the pit! Use Docker to build a PHP development environment

Avoid the pit! Use Docker to build a PHP development environment

继续配置 Debug 相关设置,如下图:

Avoid the pit! Use Docker to build a PHP development environment

进入主界面,选择右上的 “Add Configuration...”

Avoid the pit! Use Docker to build a PHP development environment

进入 debug 配置窗口后选择 “+”,选择 “PHP Web Page“,设置名称后进入 Servers 设置,同时设置 Start URL:“/xdebug/demo.php”

Avoid the pit! Use Docker to build a PHP development environment

Avoid the pit! Use Docker to build a PHP development environment

进入主窗口,在程序中打几个断点,然后点击那个绿色的甲虫就可以了。

Avoid the pit! Use Docker to build a PHP development environment

8、VSCode 调试环境配置

首先安装 PHP Debug 插件,然后打开上面创建的 xdebug 目录。如下图所示,进入调试面板,点齿轮选择 PHP,然后会创建一个 launch.json 文件,修改这个文件加入 pathMappings 配置,这个配置参数用于设置服务器路径与本地路径的对应关系,低版本使用的是 localSourceRoot 和 serverSourceRoot,目前已经被废除。另外记得将端口改为 9001。

Avoid the pit! Use Docker to build a PHP development environment

Avoid the pit! Use Docker to build a PHP development environment

进入 demo.php,随意设置两个断点,然后点左侧的运行按钮,进入调试模式,接下来刷新 http://localhost/xdebug/demo.php 会返回到 VSCode 的调试窗口,左侧已经列出调试信息,可选择单步执行跟踪程序的运行。

Avoid the pit! Use Docker to build a PHP development environment

Avoid the pit! Use Docker to build a PHP development environment

结尾:实际上这次部署环境踩了不少坑,主要原因是网上的资料都比较陈旧,很多文章也没有后续的更新,或者方向和我不太一致,例如 hub 中直接就有 xdebug 的镜像,但是我还是想尽量用官方提供的镜像自己创建,为了避免踩坑,大家最好还是多多参考官方文档,一般都写的比较清楚了。

The above is the detailed content of Avoid the pit! Use Docker to build a PHP development environment. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:learnku.com
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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!