安装环境

安装 docker

安装 PHP

搜索 php

  1. $ docker search php
  2. NAME DESCRIPTION STARS OFFICIAL AUTOMATED
  3. php While designed for web development, the PHP 6569 [OK]
  4. composer Composer is a dependency manager written in 866 [OK]
  5. adminer Database management in a single PHP file. 709 [OK]
  6. phpmyadmin phpMyAdmin - A web interface for MySQL and M 534 [OK]
  7. mediawiki MediaWiki is a free software open source wik 419 [OK]
  8. php-zendserver Zend Server - the integrated PHP application 201 [OK]

安装镜像

  1. $ docker pull php:7.1-fpm
  2. ...
  3. Status: Downloaded newer image for php:7.1-fpm
  4. docker.io/library/php:7.1-fpm

重命名镜像, 挂载目录

  1. $ docker run --name php71-fpm \
  2. -v /Users/duoli/Documents/workbench:/var/www \
  3. -d php:7.1-fpm

--name php71-fpm
将容器命名为 php71-fpm。
-v /Users/duoli/Documents/workbench:/var/www
将主机中项目的目录 /Users/duoli/Documents/workbench 挂载到容器的 /var/www

安装 Nginx

nginx 配置

  1. # /Users/duoli/Documents/program/docker/php71-conf.d
  2. # default.conf
  3. server {
  4. listen 80;
  5. server_name localhost;
  6. # 根目录
  7. root /var/www;
  8. index index.php;
  9. location ~ \.php$ {
  10. # 映射 9000
  11. fastcgi_pass php71-fpm:9000;
  12. include fastcgi_params;
  13. fastcgi_param SCRIPT_NAME $fastcgi_script_name;
  14. fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  15. }
  16. }

启用映射

  1. $ docker run --name nginx-php71 -p 8071:80 \
  2. -v /Users/duoli/Documents/program/docker/php71-conf.d:/etc/nginx/conf.d:ro \
  3. --link php71-fpm \
  4. -d nginx

-p 8071:80
端口映射,把 nginx 中的 80 映射到本地的 8071 端口
-v ...
映射服务器的 conf.d 目录到本地目录, 方便配置
--link php71-fpm
php71-fpm 镜像的网络并入nginx 网络, 使用 php71-fpm便可识别到镜像服务
在目录创建

  1. <?php
  2. phpinfo();

使用 [http://localhost:8071/index.php](http://localhost:8071/index.php) 访问, 可以看到 phpinfo 信息
image.png

配置 Phpstorm 编辑器

配置 cli

打开 Preferences | PHP� 配置
image.png

运行 cli

打开 Services, 运行 Docker, 找到启动的镜像, 运行 Terminal
image.png
这样既可运行 php

  1. root@c5368c4120cc:/var/www/html# php -v
  2. PHP 7.1.33 (cli) (built: Nov 22 2019 18:34:33) ( NTS )
  3. Copyright (c) 1997-2018 The PHP Group
  4. Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

配置 composer

下载 composer.phar 到代码目录

A long-term-support version (2.2.x) still offers support for PHP 5.3.2+ in case you are stuck with a legacy PHP version

这里下载 composer 2.2 版本既可
运行 composer

  1. # 更换源
  2. $ php composer.phar config repos.packagist composer https://mirrors.cloud.tencent.com/composer/
  3. # 更新代码
  4. $ php composer.phar update
  5. Loading composer repositories with package information
  6. ....

后续根据项目需求配置 nginx 映射访问即可