Mac 配置PHP环境

2018-07-10  本文已影响49人  千山万水迷了鹿

00 开发环境

Mac 10.13.5

一、启动Apache服务器

一般来说Mac自带Apache服务器,然后我们通过命令行启动

Apache的常用的几个命令
// 启动Apache服务
sudo apachectl start
// 重启Apache服务
sudo apachectl restart
// 停止Apache服务
sudo apachectl stop
// 查看Apache版本
httpd -v

打开浏览器,在地址栏中输入localhost,出现It works!字符串,就说明Apache已经成功启动。

修改Apache 的工作目录,因为原来的工作目录再系统目录下,不方便操作:
$ sudo vim /etc/apache2/httpd.conf
输入密码后修改两个地方,把 DocumentRoot 和 Directory 配置成自己创建PHP项目的父目录:

#DocumentRoot "/Library/WebServer/Documents"
#<Directory "/Library/WebServer/Documents">
DocumentRoot "/Users/liuqiang/Workspace/php"
<Directory "/Users/liuqiang/Workspace/php">

修改好后:wq保存退出,重启服务器,并在自定义的路径下放置html文件,即可访问。

二、 配置PHP环境变量

查看PHP是否按照
$ php -v
运行结果:

PHP 7.1.16 (cli) (built: Apr  1 2018 13:14:42) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2018 Zend Technologies

然后修改PHP配置文件

$ sudo vim /etc/apache2/httpd.conf

修改如下配置:
去掉下面这行的注释(把签名的# 去掉)
LoadModule php7_module libexec/apache2/libphp7.so

修改好后:wq保存退出。

注意:mac更新的时候,这一行有可能会被重新注释掉

三、 添加 xdebug 断点调试工具

这个工具的安装比较有意思,调试工具必须要适配PHP版本,所以要把PHP的信息粘贴到xdebug官网的一个工具里面然后会有步骤告诉你该如何安装。

访问https://xdebug.org/wizard.php,然后上面有说明输入框改填写啥,最简单的是控制台运行php -i 然后把输出粘贴到输入框里面,然后点击Analyse my phpinfo() output按钮就行了。里面又详细的安装步骤。这里记录一下安装中遇到错误

  1. 错误一:
    运行 phpize 报错:
liuqiangs-MBP:xdebug-2.6.0 liuqiang$ phpize
grep: /usr/include/php/main/php.h: No such file or directory
grep: /usr/include/php/Zend/zend_modules.h: No such file or directory
grep: /usr/include/php/Zend/zend_extensions.h: No such file or directory
Configuring for:
PHP Api Version:
Zend Module Api No:
Zend Extension Api No:
Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

首先这是两个错误,一个是 autoconf 这个玩意没有安装,安装方法:

$ brew install autoconf

第二个是那些 No such file or directory 的错误,是因为有些配置没有配置好,安装xcode 可以帮你自动配置,安装方法:

$ xcode-select --install

最后注意,如果想要用PHPStorm 调试的话,要在 /etc/php.ini 里面开启远程调试功能

添加如下配置:

xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.profiler_enable=1
xdebug.idekey = PHPStorm
上一篇下一篇

猜你喜欢

热点阅读