Windows10配置PHP开发环境

下载 PHP

PHP 官网: https://windows.php.net/downloads/releases/archives/

注意:下载的 PHP VC 版本不能比前面安装的 vc redist 版高。

进入 PHP 安装目录,复制php.ini-development 改名为 php.ini ,打开php.ini找到 ;extension_dir=ext ,去掉注释符,将值改为 ${PHP安装路径}\ext,内容如下

1
extension_dir = "D:\Applications\php-7.2.34-Win32-VC15-x64\ext"

php 要使能 MySQL 的话还要在 php.ini 文件中取消如下两行的注释

1
2
extension=mysqli
date.timezone = Asia/Shanghai

下载 PhpStorm

JetBrains PhpStorm: https://www.jetbrains.com/zh-cn/phpstorm/

配置 xdebug

xdebug 官网: https://xdebug.org/download/historical

xdebug 官网描述: https://xdebug.org/docs/upgrade_guide#changed-xdebug.remote_enable

编辑 php.ini ,在文件后追加以下内容:

1
2
3
4
5
6
7
8
[Xdebug]
;指定Xdebug扩展文件的绝对路径
zend_extension="D:\Applications\php-7.2.34-Win32-VC15-x64\ext\php_xdebug-3.0.0-7.2-vc15-x86_64.dll"

xdebug.mode=debug
xdebug.client_host=127.0.0.1
xdebug.client_port=9000
xdebug.output_dir="D:\Applications\php-7.2.34-Win32-VC15-x64\xdebug"

注意:配置的端口(xdebug.client_port)要和 phpstorm 上面设置的端口一致

xdebug 使用教程:https://www.bilibili.com/video/BV1Qx411f7pF?p=2

下载 Nginx

Nginx 官网: http://nginx.org/en/download.html

配置 Composer

Composer 国内官网: https://www.phpcomposer.com/ 下载composer.pharhttps://install.phpcomposer.com/composer.phar

这里用的方式是用的composer.phar免安装的配置方式,当然你也可以去官网下载完整安装包,无脑下一步即可。

Mac 或 Linux 系统

打开命令行窗口并执行如下命令将前面下载的 composer.phar 文件移动到 /usr/local/bin/ 目录下

1
sudo mv composer.phar /usr/local/bin/composer

Windows 系统

  1. 找到并进入 PHP 的安装目录(和你在命令行中执行的 php 指令应该是同一套 PHP)。
  2. composer.phar 复制到 PHP 的安装目录下面,也就是和 php.exe 在同一级目录。
  3. 在 PHP 安装目录下新建一个 composer.bat 文件,并将下列代码保存到此文件中。
1
@php "%~dp0composer.phar" %*

最后重新打开一个命令行窗口试一试执行 composer --version 看看是否正确输出版本号。

可能出现的报错

1
2
3
[Composer\Exception\NoSslException]
The openssl extension is required for SSL/TLS protection but is not available. If you can not enable the openssl ex
tension, you can disable this error, at your own risk, by setting the 'disable-tls' option to true.

解决方案:

  • 关闭 Composer 的 TLS 来修复,注意:它不安全
1
composer config -g -- disable-tls true
  • 开启 php.ini 中的 extension(推荐)
1
extension=openssl

注意: 如果设置为 true,则将使用 HTTP 尝试所有 HTTPS URL,而不执行网络级加密。这是一个安全风险,不推荐。更好的方法是在 php.ini 中启用 php_openssl 和 openssl 扩展。

修改 Composer 为国内镜像

1
2
3
4
# 全局配置
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
# 取消配置
composer config --unset repos.packagist

修改 Composer 缓存路径

[cache-dir]是 Composer 的缓存目录,默认位置是%USERPROFILE%/AppData/Local/Composer

[vendor-dir]软件包的默认安装位置,默认位置是%USERPROFILE%/vendor

php安装目录下新建Composer文件夹,在Composer文件夹内再新建cache-dirvendor-dir文件夹,输入以下命令

1
2
composer config -g cache-dir D:/Applications/php-7.2.34-Win32-VC15-x64/Composer/cache-dir
composer config -g vendor-dir D:/Applications/php-7.2.34-Win32-VC15-x64/Composer/vendor-dir # 不建议修改vendor-dir,指定工程中使用composer install也会安装到这个目录,原因未知

常用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 初始化composer.json文件
composer init
# 列出所有可用的命令
composer list
# 读取composer.json内容,解析依赖关系,安装依赖包到vendor目录下
composer install
# 更新最新的依赖关系到compsoer.lock文件,解析最新的依赖关系并且写入composer.lock文件
composer update
# 搜索包,packagename替换为你想查找的包名称
composer search packagename
# 添加对packagename的依赖,packagename可修改为你想要的包名称
composer require packagename
composer show packagename
# 更新 composer.phar文件自身
composer self-update
# 优化一下自动加载
composer dump-autoload --optimize
# 选项查看帮助信息
composer command --help

可能出现的报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Installing dependencies from lock file (including require-dev)
Verifying lock file contents can be installed on current platform.
Your lock file does not contain a compatible set of packages. Please run composer update.

Problem 1
- league/flysystem is locked to version 1.1.9 and an update of this package was not requested.
- league/flysystem 1.1.9 requires ext-fileinfo * -> it is missing from your system. Install or enable PHP's fileinfo extension.
Problem 2
- league/mime-type-detection is locked to version 1.11.0 and an update of this package was not requested.
- league/mime-type-detection 1.11.0 requires ext-fileinfo * -> it is missing from your system. Install or enable PHP's fileinfo extension.
Problem 3
- league/flysystem 1.1.9 requires ext-fileinfo * -> it is missing from your system. Install or enable PHP's fileinfo extension.
- laravel/framework v5.6.40 requires league/flysystem ^1.0.8 -> satisfiable by league/flysystem[1.1.9].
- laravel/framework is locked to version v5.6.40 and an update of this package was not requested.

To enable extensions, verify that they are enabled in your .ini files:
- D:\Applications\php-7.2.34-Win32-VC15-x64\php.ini
You can also run `php --ini` in a terminal to see which files are used by PHP in CLI mode.
Alternatively, you can run Composer with `--ignore-platform-req=ext-fileinfo` to temporarily ignore these required extensions.

解决方案:

  • 开启 php.ini 中的 extension
1
extension=fileinfo