安装环境
安装 docker
安装 PHP
搜索 php
$ docker search php
NAME DESCRIPTION STARS OFFICIAL AUTOMATED
php While designed for web development, the PHP … 6569 [OK]
composer Composer is a dependency manager written in … 866 [OK]
adminer Database management in a single PHP file. 709 [OK]
phpmyadmin phpMyAdmin - A web interface for MySQL and M… 534 [OK]
mediawiki MediaWiki is a free software open source wik… 419 [OK]
php-zendserver Zend Server - the integrated PHP application… 201 [OK]
安装镜像
$ docker pull php:7.1-fpm
...
Status: Downloaded newer image for php:7.1-fpm
docker.io/library/php:7.1-fpm
重命名镜像, 挂载目录
$ docker run --name php71-fpm \
-v /Users/duoli/Documents/workbench:/var/www \
-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 配置
# /Users/duoli/Documents/program/docker/php71-conf.d
# default.conf
server {
listen 80;
server_name localhost;
# 根目录
root /var/www;
index index.php;
location ~ \.php$ {
# 映射 9000
fastcgi_pass php71-fpm:9000;
include fastcgi_params;
fastcgi_param SCRIPT_NAME $fastcgi_script_name;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
启用映射
$ docker run --name nginx-php71 -p 8071:80 \
-v /Users/duoli/Documents/program/docker/php71-conf.d:/etc/nginx/conf.d:ro \
--link php71-fpm \
-d nginx
-p 8071:80
端口映射,把 nginx 中的 80 映射到本地的 8071 端口-v ...
映射服务器的 conf.d
目录到本地目录, 方便配置--link php71-fpm
将 php71-fpm
镜像的网络并入nginx 网络, 使用 php71-fpm
便可识别到镜像服务
在目录创建
<?php
phpinfo();
使用 [http://localhost:8071/index.php](http://localhost:8071/index.php)
访问, 可以看到 phpinfo 信息
配置 Phpstorm 编辑器
配置 cli
运行 cli
打开 Services
, 运行 Docker, 找到启动的镜像, 运行 Terminal
这样既可运行 php
root@c5368c4120cc:/var/www/html# php -v
PHP 7.1.33 (cli) (built: Nov 22 2019 18:34:33) ( NTS )
Copyright (c) 1997-2018 The PHP Group
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
# 更换源
$ php composer.phar config repos.packagist composer https://mirrors.cloud.tencent.com/composer/
# 更新代码
$ php composer.phar update
Loading composer repositories with package information
....
后续根据项目需求配置 nginx 映射访问即可